Cancel an applied force

Background:

I have a moving physics object being accelerated by an applyForce function called every 15ms.

I attempt to stop the physics object by:

setLinearVelocity( 0,0 ) and no longer calling the applyForce funtion.

The Problem:

Sometimes the physics object keeps moving at a slow pace.

My understanding:

applyImpulse:  Instantaneously changes the velocity of a physics object due to a calculated “force”

applyForce: Accelerates a physic object over a brief period of time due to a calculated “force”

So my understanding is that applyForce is just like applyImpulse except rather than being instant it happens over a brief period of time.

What I think the fix is:

I think the applyForce is still finishing being applied for a very brief period of time after the object was stopped.

Is there a function to terminate the applyForce command?

Your description is pretty much accurate…

applyImpulse imparts a burst of energy into the object like being hit by a mallet. The effect is that all the energy is transferred into the object at once.

applyForce transfers energy as an addition to the existing velocities. The effect is as of thrusters being fired.

Box2D is a realistic physics engine and there are many forces being implemented. Not all of these dissipate just because the linear velocity is nulled. Remember, there is also angular velocity. I assume there are numerous other values being processed in the background as well.

There is no function to terminate the applyForce, I’m afraid. What you could try is to attach a touch joint to the object you want to stop and simply keep the touch joint in the same place. You could also try simply applying the setLinearVelocity and setAngularVelocity to 0 repeatedly every frame for half a second.

If you want to bring something to an absolute stop one way is to join it to an off-screen static object with a weld joint.

I’m not saying I’ve got the answer, as I’ve battled this issue myself in the past and you just have to come up with a solution which works for your situation.

Hi @DCbball4life,

In Box2D, sometimes the most simple solution is to “wait 10-50 milliseconds” before attempting to perform another physics operation, so Box2D can finish working out its internal math before moving on to the next task. Have you tried setting the linear velocity to 0,0 after a very short timer? I can’t guarantee this will work, but it’s worth a try.

You could also possibly make the body inactive, then forcibly re-activate it after 10-20 milliseconds, and (I would expect) the applied force should not carry over.

Best regards,

Brent

Your description is pretty much accurate…

applyImpulse imparts a burst of energy into the object like being hit by a mallet. The effect is that all the energy is transferred into the object at once.

applyForce transfers energy as an addition to the existing velocities. The effect is as of thrusters being fired.

Box2D is a realistic physics engine and there are many forces being implemented. Not all of these dissipate just because the linear velocity is nulled. Remember, there is also angular velocity. I assume there are numerous other values being processed in the background as well.

There is no function to terminate the applyForce, I’m afraid. What you could try is to attach a touch joint to the object you want to stop and simply keep the touch joint in the same place. You could also try simply applying the setLinearVelocity and setAngularVelocity to 0 repeatedly every frame for half a second.

If you want to bring something to an absolute stop one way is to join it to an off-screen static object with a weld joint.

I’m not saying I’ve got the answer, as I’ve battled this issue myself in the past and you just have to come up with a solution which works for your situation.

Hi @DCbball4life,

In Box2D, sometimes the most simple solution is to “wait 10-50 milliseconds” before attempting to perform another physics operation, so Box2D can finish working out its internal math before moving on to the next task. Have you tried setting the linear velocity to 0,0 after a very short timer? I can’t guarantee this will work, but it’s worth a try.

You could also possibly make the body inactive, then forcibly re-activate it after 10-20 milliseconds, and (I would expect) the applied force should not carry over.

Best regards,

Brent