Framerate independence is a concept in game development where the game's behavior is consistent regardless of the frame rate. This is important because different hardware can run the game at different speeds, and the game should behave the same way on all devices.
To make the calculations easier, let's pretend our game runs at 1 frame per second.
We have this pixel, moving toward the right:
🮄
The calculation for its movement is simple. Every frame, the game calls this hypothetical advance()
function, which adds 1 pixel to its position.
func advance():
position.x += 1
On second later, it is here:
🠦🮄
And so on. Now, let's take this game, and run it on a much faster computer, which runs at a whopping 2 frames per second. Now, each second, the game runs advance
twice! On this computer, a second later, our pixel is here:
🠦🠦🮄
It's going faster! Old games worked this way: that's why a lot of older games run too fast to play on modern computers without artificially slowing the processor.
Now let's look at what happens if we introduce a frame_rate
variable:
var frame_rate = 1
func advance():
position.x += 1 / frame_rate
On the first computer, frame_rate
is equal to 1
, so the result doesn't change. On the faster computer, running at 2 FPS, it's 2
, so the pixel moves at 1 / 2
pixels per frame: it'll take 2 frames to cover one pixel.
Now, the game runs at the same speed regardless of the hardware! There's only one complication: if the frame rate drops, then this value will become incorrect. For that reason, Godot determines that value every frame, and gives it as a parameter to _physics_process()
and _process()
.
"delta" is often used in mathematics to represent a difference between two values. In this case, it represents the difference in time between the current frame and the previous frame.
The delta time is the time that passed since the last frame. If the game runs at 60 FPS, the delta time is 1/60
seconds, or about 0.0166666666666667
seconds.
If the game runs at 2 FPS, the delta time is 1/2
seconds, or 0.5
seconds.
Multiplying by this delta value is similar to dividing by the frame rate.
1/2
is equal to 1 * 0.5
1/60
is approximately equal to 1 * 0.0166666666666667
So, by multiplying by the delta time, you're effectively dividing by the frame rate.See Also
Related terms in the Glossary