How to create per-body damping

Official forum for the Chipmunk2D Physics Library.

How to create per-body damping

Postby alf » Thu Sep 11, 2014 3:31 am


I'm trying to have per-body damping rather than a global value and as far as I can tell, the way to do this is to override each body's "velocity_func". I've done this and pointed it at my own function, which is just a mirror copy of the built-in, default function except I modify the 'damping' parameter at the top of the function.

Code: Select all
void customVelocityUpdate(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
   // Set custom damping value
   damping = 0.95;

   // Below is the code from the built-in Chipmunk function, verbatim
   body->v = cpvclamp(cpvadd(cpvmult(body->v, damping), cpvmult(cpvadd(gravity, cpvmult(body->f, body->m_inv)), dt)), body->v_limit);
   cpFloat w_limit = body->w_limit;
   body->w = cpfclamp(body->w*damping + body->t*body->i_inv*dt, -w_limit, w_limit);

Unfortunately this doesn't work as expected, but I'm not sure why. So I went back to a global value of 0.95 thusly in my "init" function:

Code: Select all
   simSpace->damping = 0.95f;

Putting a breakpoint in my custom function shows that the 'damping' parameter is 0.44 when the function is entered -- not 0.95 as I'd expect from having set it to that in the "init" code.

So the questions are:

1. What causes the global 0.95 value to become 0.44 by the time it hits the update function?
2. What do I need to do to duplicate that translation?
3. Doesn't 0.44 seem like a strange number given the 0.95 starting point?
4. Am I correct in that this is how one should override the damping on a per-body basis?

Posts: 7
Joined: Thu Sep 11, 2014 3:16 am

Re: How to create per-body damping

Postby slembcke » Thu Sep 11, 2014 11:55 am

It's not really well explained. The space damping factor is per second. A value of 0.9 means that bodies will lose 10% of it’s velocity per second. The value passed to the velocity integration function is just a simple coefficient though. It's calculated here: ... tep.c#L398

I wouldn't recommend copy/pasting the implementation of cpBodyUpdateVelocity(), just call it from your function with the new values instead.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! ->
User avatar
Site Admin
Posts: 4164
Joined: Tue Aug 14, 2007 7:13 pm

Re: How to create per-body damping

Postby alf » Thu Sep 11, 2014 5:13 pm

That was it, thanks! I've seen your answer in other similar posts but didn't quite understand until this one for some reason. Much appreciated.
Posts: 7
Joined: Thu Sep 11, 2014 3:16 am

Return to Chipmunk2D Physics

Who is online

Users browsing this forum: No registered users and 3 guests