What is better for games when developing game loops, fixed time steps or variable time steps? What type of games are better with one or the other?
Variable time steps:
With variable time step, I mean physics updates will take in some sort of "time elapsed since last update" argument and hence dependent on framerate. This may mean doing calculations as position = position + distancePerSecond*timeElapsed.
- pro: smooth
- pro: easier to to code
- con: hard to record/replay actions as time steps vary
- con: weird physics errors that are hard to predict with very small or large time steps
eg (from dewitters):
while( game_is_running ) {
prev_frame_tick = curr_frame_tick;
curr_frame_tick = GetTickCount();
update( curr_frame_tick - prev_frame_tick );
render();
}
Fixed time steps:
With fixed time steps, the update method may not even accept a "time elapsed" as it assumes that each update is for a fixed time period. Calculations may be done as position = position + distancePerUpdate. The example includes an interpolation during render.
- pro: physics are very predictable
- pro: easier to record actions per time step as they are fixed
- pro: possibly easier to sync up with other players over network?
- pro: don't have to confuse all calculations with timeElapsed variable everywhere
- con: it will never sync up vertical refresh so you either have jittery graphics or you have to always interpolate.
- con: maximum frame rate is limited unless you interpolate
- con: hard to work within frameworks that assume variable time steps (like pyglet or flixel)
eg (from dewitters):
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 );
}
Notes:
- Gaffer on games: Fix your timestep!
- deWitter's game loop article
- Quake 3's fps affects jump physics Allegedly this is one reason why Doom 3 is frame locked at 60fps?
- Flixel requires a variable timestep (I think this is determined by Flash) whereas Flashpunk allows you fixed and variable timesteps.
- Box2d docs seems to suggest that it likes constant time steps. See Simulating the World of Box2D.