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.