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

Making a body stop in its tracks

Topics: Developer Forum, User Forum
Nov 2, 2009 at 8:08 AM

Hello everyone! I am using the Farseer physics engine with XNA, and I would like to know how can I stop a moving body instantly? No slowing down, but just stop bang right in one spot while it is moving. Can this be done? Please let me know.



Nov 2, 2009 at 5:06 PM

I haven't been able to use this engine in a while unfortunately, so I'm not certain this will work, but give it a shot.

body.ApplyImpulse(body.Velocity * -body.Mass);

It may have negative effects if called more than once per update.  Let me know if it works, since I can't test it myself yet.

Nov 2, 2009 at 9:44 PM

Thanks a whole lot Yota! It works greeeeeeeeeeeat! Just one thing. In the latest release of Farseer, a body doesn't have a Velocity field. You have to change that to LinearVelocity. Also, it works fine with multiple calls per update. It doesn't have negative effects because the velocity is low so it doesn't shoot backwards or anything.


Thanks again

Jun 3, 2010 at 2:06 AM
Edited Jun 3, 2010 at 2:12 AM

How do you apply just enough (negative) torque to stop a rotating body?

I attempted the following but it does not seem to do the trick. The body can still be rotated, just less:

body.ApplyAngularImpulse(body.AngularVelocity * -body.MomentOfInertia);

Jun 3, 2010 at 7:54 AM

The body can still be rotated because its moment of inertia, the question above I think it was to make a body stop rotating at a certain time, but not avoid to rotate it any more (I'm not sure if I'm making clear the difference). If you want to avoid a body to rotate at all, always no matter how big is the force you apply to it, you have to set 

body.MomentOfInertia = float.PositiveInfinity
Jun 3, 2010 at 6:24 PM
float.PositiveInfinity was causing me trouble on a WeldJoint because FS engine internally takes the reciprocal of MomentofInteria. The reciprocal of infinity is NaN, therefore, this caused an unhandled exception to get raised in FS.
To solve this problem, I instead set the MomentOfIntertia to the largest possible float value (<infinity)
body.MomentOfInertia = float.MaxValue;      // make object non-rotatable

Now it works! Thank you for your help. :)