- Online Community Forums » Programming Questions » Help me understand this code?

This thread is locked; no one can reply to it. rss feed Print
Help me understand this code?
Member #11,955
May 2010

Hello everybody!

This is another post about delta time and you know separating the physics from the drawing.

I haven't programmed anything related to videogames in a long time. And I have completely forgotten how the fixed timestamp and that stuff works...

For my game, I will be using a lot of libraries, and even applications, I think I'll have some time and for that reason I'm on board again, I hope... :-[

For physics I'll be using Chimpmunk, for networking Enet and Protobuf, for animations Spine (I discovered it reading one thread here these days), for storing data SQLite, and for the rest, of course, Allegro 5.

Incredibly I'm using Linux :o yhea, no Windows here. :o:o

I have already used this tools separately and made some test, but the thing is that the documentation of Chimpmunk and Spine points to a document about Fixed Timestep, I know it's a pretty popular document and I have read it before, but didn't went too far to the point I want to go now, because my previous game didn't need it.

Ok, so... :P I'm seeing this code:

1 double t = 0.0; 2 const double dt = 0.01; 3 4 double currentTime = hires_time_in_seconds(); 5 double accumulator = 0.0; 6 7 State previous; 8 State current; 9 10 while ( !quit ) 11 { 12 double newTime = time(); 13 double frameTime = newTime - currentTime; 14 if ( frameTime > 0.25 ) 15 frameTime = 0.25; // note: max frame time to avoid spiral of death 16 currentTime = newTime; 17 18 accumulator += frameTime; 19 20 while ( accumulator >= dt ) 21 { 22 previousState = currentState; 23 integrate( currentState, t, dt ); 24 t += dt; 25 accumulator -= dt; 26 } 27 28 const double alpha = accumulator / dt; 29 30 State state = currentState*alpha + previousState * ( 1.0 - alpha ); 31 32 render( state ); 33 }

I have to admit that I'm really bad with pseudo code, because I think that over there is pseudo code, isn't it?

I don't know hot to interpreter it, really, I would prefer if it was real code... What's is State? and why it's not define previousState and currentState?

It has a function called hires_time_in_seconds(); and then lower in the code another one called time(), then a function called integrate(), really I can't understand what it's happening. I'm right now reading this because I wanted to test Chimpmunk with this technique but I'm stuck in that ┬┐simple? chunk of code...

Can someone, someway 'transform it' to real code? real cclassesand function? or explain me what's happening there? Thanks :)

Member #14,461
July 2012

Hey there, I'll try and explain the pseudocode but I'm having trouble with my own gameloop so take what I say with a grain of salt.

hires_time_in_seconds() and time() mean the same thing in this case and Allegro offers the function al_get_time() which returns time as a double.

If you read read Glenn's previous article on physics, he explains what integrate() does. It essentially advances the game so if there is an object then we work out by how much it should move forward while keeping in mind velocity and acceleration. We store both the previous state and the old state so that we can interpolate between then, it's up to you how you store the two states but Glenn's just abstracting the problem and using a State type to store states.

You can read my code if that helps you understand the pseudocode but as I've said, it doesn't work properly. Hope this helps you a bit! :)

Go to: