Fixed time step loop with varying frame rate

Adrian M

Newcomer
Hopefully this is the right forum to post :D.
So i'm using this loop in an opengl game i'm developing:

0. numTotalUpdates = 0;
1. fixedDt = 1.0f / 400.0f; // how small is enough?
2. while (IsRunning())
{
3. DispatchEvents();

4. static float accumulator = 0.0f;
5. numUpdatesThisFrame = 0;
6. accumulator += GetFrameTime() ; // time since last
update

7. while (accumulator >= fixedDt)
{
8. UpdateSimulation(fixedDt);
9. accumulator -= accumulator;
10. numUpdatesThisFrame ++;

}
11. numTotalUpdates++;
12. renderInterpolator = accumulator / fixedDt; // safe
to say it's in 0..1
13. Render(renderInterpolator);
}

The thing is the fps varies sometimes so in this case numTotalUpdates varies to which implies that the simulation would be forwarded in time quite a bit and in consequence this would cause visible stuttering on the screen.

This effect is most notable with VSync on. What is a good way to deal with fixed time step loops and varying frame rate?
 
Back
Top