3

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

SteveWight
  • 75
  • 2
  • 8

1 Answers1

8

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;  
    }  
}
jrtc27
  • 8,496
  • 3
  • 36
  • 68
Jordaan Mylonas
  • 1,261
  • 1
  • 11
  • 24
  • Ok, Thanks Jordaan. This is the easiest explanation i've heard so far. So in my init method when I call: [self schedule: @selector(tick:)] will I need to include additional paramaters? Also, will this implementation create a consistent (near identical, if not identical) simulation across different devices that could be displaying at different FPS (i.e. iphone 3gs and iphone 4)? – SteveWight Mar 29 '11 at 01:10
  • Yes, essentially. It all hinges on how well you pick the maximumStep value. Experiment around, and check box2d's documentation to see if they suggest a value. Also note, however, that setting the value too low can lower performance across the board, as the physics system has to work more often. – Jordaan Mylonas Mar 29 '11 at 01:20
  • Great. I've implemented this solution and everything seems to be running more consistently. I'll need to test it some more but thanks again Jordaan, this is what I was looking for. – SteveWight Mar 29 '11 at 01:41