Fixed vs. variable frame rates in games: what is best, and when?

Pedro picture Pedro · Sep 10, 2008 · Viewed 13.3k times · Source

After working for a while developing games, I've been exposed to both variable frame rates (where you work out how much time has passed since the last tick and update actor movement accordingly) and fixed frame rates (where you work out how much time has passed and choose either to tick a fixed amount of time or sleep until the next window comes).

Which method works best for specific situations? Please consider:

  • Catering to different system specifications;
  • Ease of development/maintenance;
  • Ease of porting;
  • Final performance.

Answer

Paul picture Paul · Sep 12, 2008

I lean towards a variable framerate model, but internally some systems are ticked on a fixed timestep. This is quite easy to do by using a time accumulator. Physics is one system which is best run on a fixed timestep, and ticked multiple times per frame if necessary to avoid a loss in stability and keep the simulation smooth.

A bit of code to demonstrate the use of an accumulator:

const float STEP = 60.f / 1000.f;
float accumulator = 0.f;

void Update(float delta)
{
    accumulator += delta;

    while(accumulator > STEP)
    {
        Simulate(STEP);
        accumulator -= STEP;
    }
}

This is not perfect by any means but presents the basic idea - there are many ways to improve on this model. Obviously there are issues to be sorted out when the input framerate is obscenely slow. However, the big advantage is that no matter how fast or slow the delta is, the simulation is moving at a smooth rate in "player time" - which is where any problems will be perceived by the user.

Generally I don't get into the graphics & audio side of things, but I don't think they are affected as much as Physics, input and network code.