Strange Physics Behaviour

Topics: Developer Forum
Feb 19, 2012 at 7:18 AM

Hi everyone,

So I just started out with FPE, and XNA I should put in, and so far have been having some troubles.

I have been making my own wrapping class to easily do the quite repetive set up work of creating a rectangle.

Here are the two classes:

public class Sprite : DrawableGameComponent
	{
		public SpriteBatch spriteBatch;
		public Texture2D Texture;
		public Vector2 Position;
		public float ScaleW, ScaleH, Rotation;
		public string Name;
		Game g;

		public Sprite(Vector2 Position, float R, string Name, Game game, float ScaleW = 1, float ScaleH = 1)
			: base(game)
		{
			this.Name = Name;
			this.Position = Position;
			this.ScaleW = ScaleW;
			this.ScaleH = ScaleH;
			Rotation = R;
			g = game;
		}

		public void Load()
		{
			base.Initialize();
			spriteBatch = new SpriteBatch(GraphicsDevice);
			Texture = g.Content.Load<Texture2D>(Name);
		}

		public override void Draw(GameTime gameTime)
		{
			spriteBatch.Begin();
			spriteBatch.Draw(Texture, new Rectangle((int)(Position.X * Config.Ratio), (int)(Config.SimDomain.Height * Config.Ratio - (Position.Y * Config.Ratio) ), (int)(Texture.Width * ScaleW), (int)(Texture.Height * ScaleH)), null, Color.White, Config.DegreesToRadians(Rotation), new Vector2(Texture.Width/2, Texture.Height/2), SpriteEffects.None, 1); 
			spriteBatch.End();
		}
	}

and the physics object:

public class BasicBox : Sprite
	{
		public Body body;
		float Width, Height;

		public BasicBox(Vector2 Position, float R, float W, float H, float Rest, float F, string Name, Game game, ref World _world, float ScaleW = 1, float ScaleH = 1)
			: base(Position, R, Name, game, ScaleH, ScaleW)
		{
			body = BodyFactory.CreateRectangle(_world, W, H, 1);
			body.Position = Position;
			body.LocalCenter = new Vector2(W / 2, H / 2);
			body.Rotation = R;
			body.BodyType = BodyType.Dynamic;
			body.Restitution = Rest;
			body.Friction = F;
			Width = W;
			Height = H;
		}

		public void Update()
		{
			Position = body.Position;
			Rotation = body.Rotation;
		}
	}

and when I create the two classes of BasicBox,

World = new FarseerPhysics.Dynamics.World(new Vector2(0,-9.8f));


			ground = new Engine.BasicBox(new Vector2(Config.SimDomain.Width / 2, 0.5f), 0, Config.SimDomain.Width, 1, 0.2f, 0.4f, "GroundStrip", game, ref World, 1, 1);
			ground.body.BodyType = FarseerPhysics.Dynamics.BodyType.Static;
			ground.Load();



			rect = new Engine.BasicBox(new Vector2(3, 12), 40, 1, 1, 0.1f, 0.1f, "Crate", game, ref World);
			rect.Load();

Anyway, so the box doesnt rotate more than a couple of degrees ( I have checked using Console.WriteLine(), so its not rendering), and then proceeds to dip under the ground, but that may be rendering.

Any help would be greatly appreciated.

Feb 20, 2012 at 4:49 PM

Have you tried using the debug view for Farseer? Maybe there is some disconnect between how the textures are being rendered vs. the physics bodies.

Feb 26, 2012 at 5:30 AM

Sorry for the late reply.

I have outputted the position and rotation to the console, and it still doesnt work.

Feb 28, 2012 at 4:53 PM

I was suggesting that you use the debug view of the Farseer engine because that way you can see your physics bodies and your textures on screen to see how they are truly interacting. Using the console sounds like you are creating a lot more work for yourself. I would check out this discussion to see how to activate the debug view.

http://farseerphysics.codeplex.com/discussions/275582

Also, where are you calling the update method for updating the body positions/rotations?