Note that there are some explanatory texts on larger screens.

plurals
  1. POFixed timestep loop in C++
    primarykey
    data
    text
    <p>I am trying to implement a fixed timestep loop so that the game refreshes at a constant rate. I found a great article at <a href="http://gafferongames.com/game-physics/fix-your-timestep/" rel="nofollow">http://gafferongames.com/game-physics/fix-your-timestep/</a> but am having trouble translating that into my own 2d engine.</p> <p>The specific place I am referring to is the function in the last part "The Final Touch" which is what most people recommend. This is his function:</p> <pre><code>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 &gt; 0.25 ) frameTime = 0.25; // note: max frame time to avoid spiral of death currentTime = newTime; accumulator += frameTime; while ( accumulator &gt;= 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 ); } </code></pre> <p>For myself, I am just moving a player across the screen keeping track of an x and y location as well as velocity rather than doing calculus integration. **I am confused as to what I would apply to the updating of the player's location (dt or t?). Can someone break this down and explain it further?</p> <p>The second part is the interpolation which I understand as the formula provided makes sense and I could simply interpolate between the current and previous x, y player positions.</p> <p>Also, I realize I need to get a more accurate timer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload