Search code examples
cocos2d-iphonebox2d-iphonephysics-engine

How do I implement better time step (fixed or semi-fixed) in box2d?


I was wondering if someone could help me understanding fixing my time step within box2d. Im trying to improve a game I just released Bounce Z Ballz, by implementing a better time step (I currently use the built in variable time step). I have been reading some helpful information, but I can't really grasp my head around turning something similar to the built in cocos2d/box2d template to be something like Glenn Fiedler's solution.

TL;DR (I want to change my cocos2d/box2d game time step from the built in variable time step to something more consistent)

Thanks,

Steve


Solution

  • In order to implement this, you'll need to know the maximum time-step that your physics engine can step while still performing adequately. For argument's sake, let's say it is 0.03 seconds.

    Essentially, during your update loop, instead of just passing the delta time to the step time, you'll want to break it into segments of that maximum size. So, if the delta time of the current loop cycle is 0.08 seconds, then you'll want to run the update loop 3 times, with time steps of 0.03 s, 0.03 s, 0.02 s. The physics system has still progressed the 0.08 seconds, but it will have done so in small enough steps to perform properly.

    Update:(float)deltaTime  
    {  
        float maximumStep = 0.03;  
        float progress = 0.0;  
        while (progress < deltaTime)  
        {  
            float step = min((deltaTime-progress), maxStep);  
            **PHYSICS STEP(step)**  
            progress += step;  
        }  
    }