Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a custom polygon attached to a body, which looks like this: Custom Polygon Attached To Body 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?

share|improve this question
1  
possible duplicate of Move sprite in the direction it is facing? –  Byte56 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
1  
Bummer. OK we'll I've cleaned up my unuseful comments. Good luck! –  Byte56 Oct 10 '12 at 15:57
show 4 more comments

3 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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):

enter image description here

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

share|improve this answer
add comment
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:http://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 :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.