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

I have a custom polygon attached to a body, which looks like this: What I am trying to accomplish is getting the body to move according to wherever the tip of the body is. So far this is what I've tried:

        if (ks.IsKeyDown(Keys.Up))
{
body.ApplyForce(new Vector2(0, -20),body.GetLocalPoint(new Vector2(0,0)));

}
if (ks.IsKeyDown(Keys.Left))
{
body.ApplyTorque(-500);
}
if (ks.IsKeyDown(Keys.Right))
{
body.ApplyTorque(500);
}


The body rotates fine - but when I try making the body accelerate according to the tip of the body - assuming I have specified the tip correctly(I am pretty sure I haven't), it just spins around, as if I have applied Torque to it. Can anyone point me in the right direction of how to fix this problem?

• possible duplicate of Move sprite in the direction it is facing? – MichaelHouse Oct 9 '12 at 23:59
• Hmm..I don't think there is anything wrong with the torque because as far as I know, the ApplyTorque function requires large values. – Grey Oct 10 '12 at 0:20
• I implemented your solution - but it seems I took a wrong turn somewhere: codepaste.net/ttrdvi When I press the up-key it rotates very fast. Instead of moving in a straight line according to the tip of the body :/ – Grey Oct 10 '12 at 1:23
• Rotates in place and sort of moves down diagonally. – Grey Oct 10 '12 at 4:33
• Bummer. OK we'll I've cleaned up my unuseful comments. Good luck! – MichaelHouse Oct 10 '12 at 15:57

## 3 Answers

Apply the force to the objects center as suggested by Gajoo. Conviently WorldCenter is a property of the body class so.

Body.ApplyForce(forceDirection, Body.WorldCenter);


Also if you want to move the body in a specific direction you need to apply the force in that direction so "new Vector2(0, -20)" doesn't make any sense. To create a the unit vector that faces int the same direction as your body do the below.

var forceDirection = Vector2.UnitY;
var test = Matrix.CreateFromAxisAngle(new Vector3(0, 0, 1), Body.Rotation);
forceDirection = Vector2.Transform(facing, test);


Now simply scale the forceDirection vector to alter the speed.

ApplyForce does not work like that. The problem is you are always applying a force to the left down corner of your shape, with always same direction. here is a picture explaining it (I might be wrong about the corner the force is applied to but the idea is the same):

to fix this issue you should always apply the force to the center of your ship, and the rest is the same as explained in byte56 suggested. It means you need to rotate force vector yourself and then pass it to farseer

direction = body.GetWorldVector(new Vector2(0,1));


When using Farseer Physics calculating the direction isn't so hard. If your body starts off with it's tip point upwards you can find the direction by simply updating direction(as shown above) in your update loop, and then when the user presses the up-key simply apply(after calculating the velocity's as shown in this questions answered by Byte56:https://gamedev.stackexchange.com/questions/37623/move-sprite-in-the-direction-it-is-facing)

body.ApplyForce(velocity * -100);


Play with the values to get the desired output :)