Should a game loop be based on fixed or variable time steps? Is one always superior, or does the right choice vary by game?
Variable time step
Physics updates are passed a "time elapsed since last update" argument and are hence framerate-dependent. This may mean doing calculations as position += distancePerSecond * timeElapsed
.
Pros: smooth, easier to to code
Cons: non-deterministic, unpredictable at very small or large steps
deWiTTERS example:
while( game_is_running ) {
prev_frame_tick = curr_frame_tick;
curr_frame_tick = GetTickCount();
update( curr_frame_tick - prev_frame_tick );
render();
}
Fixed time step
Updates might not even accept a "time elapsed", as they assume each update is for a fixed time period. Calculations may be done as position += distancePerUpdate
. The example includes an interpolation during render.
Pros: predictable, deterministic (easier to network sync?), clearer calculation code
Cons: not synced to monitor v-sync (causes jittery graphics unless you interpolate), limited max frame rate (unless you interpolate), hard to work within frameworks that assume variable time steps (like Pyglet or Flixel)
deWiTTERS example:
while( game_is_running ) {
while( GetTickCount() > next_game_tick ) {
update();
next_game_tick += SKIP_TICKS;
}
interpolation = float( GetTickCount() + SKIP_TICKS - next_game_tick )
/ float( SKIP_TICKS );
render( interpolation );
}
Some resources
- Gaffer on games: Fix your timestep!
- deWitter's game loop article
- Quake 3's FPS affects jump physics—allegedly a reason Doom 3 is frame-locked to 60fps?
- Flixel requires a variable timestep (I think this is determined by Flash) whereas Flashpunk allows both types.
- Box2D's manual § Simulating the World of Box2D suggests it uses constant time steps.