Search code examples
pythonpygamesimulationpymunk

Does Per-Body Damping Compound With Space Damping in Pymunk?


Making some simulations for a project.

My question is does the damping value of a body compound with the damping value of the pymunk space (if there is any)?

For example, if I have a pymunk space, SPACE, with SPACE.damping = 0.9 and then I have a body, BODY, where I set its BODY.update_velocity() method to take a damping value of 0.5, will BODY's damping be 0.9*0.5 (or some other composition) or will it be 0.5?

Let me know if I can make my question more clear and thank you in advance.


Solution

  • The damping is used like this:

    1. When stepping the space (space.step), the damping that will be used is calculated by damping = pow(space.damping, dt)
    2. Next it will call the velocity function of each body in the space, passing in the calculated damping from step 1 (just below the damping calculation in step 1)
    3. The default velocity function does this calculation to set the velocity: body.velocity = body.velocity * damping + (gravity + body.force / body.mass) * dt
    4. And this to set the angular velocity: body.angular_velocity = body.angular_velocity*damping + body.torque / body.moment * dt

    You can find the actual code for step 1&2 here: https://github.com/viblo/pymunk/blob/master/chipmunk_src/src/cpSpaceStep.c#L399

    The code for step 3 & 4 here: https://github.com/viblo/pymunk/blob/master/chipmunk_src/src/cpBody.c#L494 (I translated the c code in the source to python/pymunk terms in my answer above)