Guest User

TimeStep

a guest
Jan 14th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  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.     }
Advertisement
Add Comment
Please, Sign In to add comment