Created
December 18, 2012 13:54
-
-
Save pawel-dubiel/4328156 to your computer and use it in GitHub Desktop.
fixed time step - game loop - http://gafferongames.com/game-physics/fix-your-timestep/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
double t = 0.0; | |
const double dt = 0.01; | |
double currentTime = hires_time_in_seconds(); | |
double accumulator = 0.0; | |
State previous; | |
State current; | |
while ( !quit ) | |
{ | |
double newTime = time(); | |
double frameTime = newTime - currentTime; | |
if ( frameTime > 0.25 ) | |
frameTime = 0.25; // note: max frame time to avoid spiral of death | |
currentTime = newTime; | |
accumulator += frameTime; | |
while ( accumulator >= dt ) | |
{ | |
previousState = currentState; | |
integrate( currentState, t, dt ); | |
t += dt; | |
accumulator -= dt; | |
} | |
const double alpha = accumulator / dt; | |
State state = currentState*alpha + previousState * ( 1.0 - alpha ); | |
render( state ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment