Frame Rate Independent
Documenting the Frame Rate Independent concept.
What is Game speed ?
Should add a howto debug the Game Loop in Dual displays Linux debugging
Consider what will happen if the display framerate is 55fps and the physics is running at 60fps. Basically the time accumulator will have to alternate between taking one and two physics steps each display update to make sure that it keeps up. This irregular amount of physics steps taken per update causes a subtle but visually unpleasant stuttering of the physics simulation on the screen known as temporal aliasing.
Even worse, if you setup a slow motion mode (1/10th speed) your effective display framerate would be 600fps if your display is actually updating at 60fps. Assuming the physics is also running at 60fps the accumulator would only do one physics step every 10 display updates. Objects would animate in discrete jarring steps every 1/6th of a second. This looks pretty dodgy but the good news is that there is a way to make sure that everything runs smoothly all of the time.
The solution is to interpolate between the previous physics state and the current state based on how much time is left in the accumulator. This will add a latency of up to dt to your physics simulation, but the visual results are definitely worth it. Here is how to implement it:
float t = 0.0f; const float dt = 0.01f; float currentTime = time(); float accumulator = 0.0f; State previous; State current; while (!quit) { float newTime = time(); float deltaTime = newTime - currentTime; currentTime = newTime; accumulator += deltaTime; while (accumulator>=dt) { previousState = currentState; integrate(currentState, t, dt); t += dt; accumulator -= dt; } const float alpha = accumulator / dt; State state = currentState*alpha + previousState*(1.0f-alpha); render(state); }
