3
\$\begingroup\$

If I set linearDamping on a body, it's velocity will be decreased slowly, until it stops. The documentation says, that the units are 1/time, which is unclear for me.

If the velocity of a body is 10 m/s, and the linearDamping is 0.5, then how can I calculate that when will the velocity be decreased to 1 m/s ?

\$\endgroup\$
7
\$\begingroup\$

I worked out a closed formula to calculate the velocity v' after some elapsed time t with a given linearDamping d and original velocity v:

v' = v*exp(-d*t)

To calculate how much time is needed to reach a given velocity v':

t = ln(v' / v) / (-d)

Using the parameters from the question:

t = ln(1 / 10) / (-0.5) = 4.605

The object needs 4,6 seconds to decelerate from 10 m/s to 1 m/s

Note:

The original source code to calculate new velocity inside the step function:

v *= b2Clamp(1.0f - h * b->m_linearDamping, 0.0f, 1.0f);
\$\endgroup\$
2
  • \$\begingroup\$ Please correct this answer. You need to use ln instead log in formula. Instead t=log(v' / v) / (-d) ->t = ln(v' / v) / (-d)! \$\endgroup\$
    – MatejC
    Aug 27 '19 at 8:04
  • \$\begingroup\$ Thank you, I corrected the answer \$\endgroup\$
    – Iter Ator
    Aug 27 '19 at 9:31
4
\$\begingroup\$

I looked at the Box2D source code, where it applies damping:

v *= b2Clamp(1.0f - h * b->m_linearDamping, 0.0f, 1.0f);

So the loss of velocity is certainly not a fixed number of units per second, despite what the manual says.

Your example of 10m/s to 1m/s would also slightly depend on timestep. Let's assume 120Hz simulation step...

#!/usr/bin/python

v = 10
step = 0
h = 1/120.0
time = 0.0

while v > 1 :
    v *= ( 1 - h * 0.5 )
    step += 1
    time += h
    print( "step %d, time %f, v %f" % ( step, time, v ) )

Then it would take 4.6 seconds to slow down by that much.

...
step 551, time 4.591667, v 1.001957
step 552, time 4.600000, v 0.997783

I checked with 1/60 timestep as well, and the result is almost the same, btw.

I am not sure if you can determine this time delay in a single expression, though, without stepping through it.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your help! In the meantime, I was able to work out a closed formula, which produces a result almost the same as your python script \$\endgroup\$
    – Iter Ator
    Jun 23 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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