Skip to main content

Game Loop

Game loops handle the physics and rendering of a game engine. In most game engines, these are two different loops, with the physics being calculated at a fixed rate and the rendering being done at a dynamic rate. Godot does this.

If we donโ€™t have multiple game loops and are doing our physics and rendering in the same loop, then there are a few options.

  • A fixed delta time approach is fine if the physics loop takes the same amount of time each time, but this isnโ€™t realistic. This doesnโ€™t work if thereโ€™s VSYNC, running on a slow computer, or inconsistent physics processing time.
  • Variable delta time works around the variable refresh rate issue that a fixed delta time approach does. Due to floating point math and the inherent inaccuracies of it on computers, this results in physics that can vary slightly over time.
  • A semi-fixed timestep keeps track of how much time has passed and will calculate multiple physics frame per render, handling the scenario where physics frames start running behind. This can however result in the spiral of death.

    What is the spiral of death? Itโ€™s what happens when your physics simulation canโ€™t keep up with the steps itโ€™s asked to take. For example, if your simulation is told: โ€œOK, please simulate X seconds worth of physicsโ€ and if it takes Y seconds of real time to do so where Y > X, then it doesnโ€™t take Einstein to realize that over time your simulation falls behind. Itโ€™s called the spiral of death because being behind causes your update to simulate more steps to catch up, which causes you to fall further behind, which causes you to simulate more stepsโ€ฆ

    โ€” Glenn Fiedler, Fix Your Timestep!

  • An alternative is to advance the physics at a fixed rate when sufficient delta time has passed, but only in set increments. There will always be a โ€œremainderโ€ of time that will get passed to the next loop, but thatโ€™s okay.
  • One step further would be to interpolate between frames using that remainder of time. The example that Fiedler gives is when the display rate is 60Hz but the physics rate is 50Hz. Without interpolation, a rendered frame would occasionally have two physics frames.

Sourcesโ€‹