Working out a force vector

Topics: Developer Forum
Oct 31, 2007 at 9:38 AM
This is probably a really silly problem.

I have rectangular bodies, and I am trying to apply a force in the direction they are facing.

However when I apply rotation as well, they follow a non-circular path.

If the code doesn't obviously show what I am doing wrong, I can upload an image and solution later on.


private void AccelerateUnit(Body b1, bool fwd)
{
float force = 80000;


if (fwd)
{

}
else
{
force = -force;
}

float x = (float)(Math.Sin(-b1.Rotation) * force);
float y = (float)(Math.Cos(-b1.Rotation) * force);


b1.ApplyForce(new Vector2(x, y));

}

The units motion is like a rounded off square, with the corners being on the axis.

Cheers all!
Coordinator
Oct 31, 2007 at 11:27 AM
you might try using Body.XVectorInWorldCoordinates

This will return a unit world vector pointing the local x-direction of your body.

So, if +X represents your bodies "forward facing" direction then you would apply the following force.

body.ApplyForce(body.XVectorInWorldCoordinates * 8000) 'where 8000 is the magnitude of the force you are applying.
Oct 31, 2007 at 11:30 AM
Edited Oct 31, 2007 at 11:34 AM
EDIT : I just tried using the xVectorInWorldCoords and I still got the same response...


I adapted the quickstart to demonstrate this.

Here is the code for the game1.cs (if you press left and fwd at the same time, you will see it leave a roundysquare trail behind it)

http://pastebin.com/m54c8d55c

All you need to do is paste this over the normal game.cs and you should be good to go.


Thanks very much.

JB..

ps.. I have done lots of other integration work on farseer, to do with predicting future locations, and integrating with the ai, I haven't just spent the last couple of weeks trying to make a rectangle move!

Oct 31, 2007 at 7:21 PM
Hello, I have uploaded an image of the effect to here...

http://ftw.shared.hosting.zen.co.uk/download/FarseerSample/wonky.png

and an adapted version of the quickstart project here... (including crashlanders suggested change)

http://ftw.shared.hosting.zen.co.uk/download/FarseerSample/FarseerPhysicsQuickStart2.zip

If anyone could hit me with the clue stick as to what I am doing wrong, I would be most greatful.


Cheers all,

JB
(I am really enjoying using farseer, on my next project I hope to delve a little deeper into it's functionality)
Nov 1, 2007 at 2:44 PM
I did some more experimenting with this on the train today.

The effect seems to be more pronounced when I am using greater values for the LinearDragCoefficient.

(No idea why though)
Coordinator
Nov 1, 2007 at 4:29 PM
what if you set linear drag to zero? just curious.

Also, try using an a shape that is symetrical like a square or a circle.
Nov 1, 2007 at 9:55 PM
Edited Nov 1, 2007 at 9:56 PM
Hello again.

I have made a little experimenty program that allows easy tweaking of values to see this effect.

http://ftw.shared.hosting.zen.co.uk/download/FarseerSample/Farseerdebug2.zip

The test buttons at the top set up the variables in various example configurations.

I think I could be doing something odd when I am applying the force?
Coordinator
Nov 1, 2007 at 11:24 PM
Interesting. definetly seem odd that it traces out a square with rounded corners... I'll look at it and see if I can find an explanation.
Nov 2, 2007 at 9:46 AM
Hello!

If you want me to try out anything else just give me a shout.

I am working round my problem, by not applying linear friction, and instead applying my own declerating forces.
(Which is nice, as it allows me to apply a greater deceleration force against motion perpendicular to the heading of the body)

(I can now continue on to play with wind and collision with land, yay!)