This project has moved and is read-only. For the latest updates, please go here.

Moving a body in a specific direction using XNA with Farseer Physics

Topics: Developer Forum, User Forum
Oct 10, 2012 at 5:45 PM
Edited Oct 10, 2012 at 9:18 PM
     direction.X = (float)Math.Cos(MathHelper.ToRadians(body.Rotation));
     direction.Y = (float)Math.Sin(MathHelper.ToRadians(body.Rotation));
     if (direction.Length() > 0)
     velocity.X = direction.X * 3;
     velocity.Y = direction.Y * 3;
     if (ks.IsKeyDown(Keys.Up))
         body.ApplyLinearImpulse(velocity * 20, new Vector2(0,0));
     if (ks.IsKeyDown(Keys.Left))
     if (ks.IsKeyDown(Keys.Right))

When I press the up-key it rotates instead of traveling in a straight line in accordance to the body's tip - can anyone tell me what I'm doing wrong and how to fix it?

If it helps this is how my body looks like(custom polygon attached to it): Custom Polygon Attached To Body

Oct 10, 2012 at 9:43 PM
Edited Oct 10, 2012 at 9:44 PM


Why are you applying the impulse at 0,0? First the function needs world coordinates (so that would be body.GetWorldPoint(Vector2.Zero)). But then again 0,0 in local space does not look like the center of mass of your body since it's not symmetric with respect to a point so probably it will produce torque. I'd just call body.ApplyLinearImpulse(velocity*20).

P.S: the direction could also be obtained from body.GetWorldVector(x_tip_local, y_tip_local) like body.GetWorldVector(0,1) if the tip is pointing up in the local frame (when the body is created).