pymunk

How many steps per update in pymunk and pyglet/pygame?


I have read that advancing the pymunk space by several steps per screen update can lead to a smoother simulation and help prevent objects tunneling through each other, e.g. How to prevent fast moving objects passing through statics when calculating pi with colliding blocks. The example suggests this:

def update(dt):
    for _ in range(10):
        space.step(dt/10)

To help me understand how the step function works, what would be the effect of advancing the space by even more steps per screen update. What, for instance, would be the downsides to doing this:

def update(dt):
    for _ in range(10_000):
        space.step(dt/10_000)

Solution

  • The big downside is performance. It is much more expensive to call step 10000 times than 1 time for a given period of time.

    I imagine that sooner or later there will be other effects as well, for example around the floating point precision when the dt becomes very small. E.g if an object has a very low velocity it will move a tiny distance, and if that number is very small it might not be possible to accurately store it as a floating point number (64 bit double in the case of pymunk), so that errors accumulate and the end result is a worse simulation than calling step only once.