Aller au contenu principal

Delta time

The movement speed of our games is determined by how fast the game runs (how many frames are generated)

Really slow computer ------------------------> Really fast computer

Huge differences, this is a massive problem !

How framerates influence the game

Speed (pixels per frame)Frames per secondTotal movement (per second)
10 px3010 * 30 = 300
10 px6010 * 60 = 600
10 px12010 * 120 = 1200

The game will run inconsistently on different systems

Since some scenes are harder to render than others it might even run inconsistently on the same system!

The solution is to use delta time

Delta time measures how long it took to create one frame For example, if the frame is 60fps, delta time is 1/60th of a second (0.0166) or about 17 milliseconds

This information we can use to keep the same at a constant speed regardless of framerate

We essentially multiply any movement with the delta time

Speed (pixels per frame)Frames per secondTotal movement (per second)Delta timeTotal movement (per second)
10 px3010 * 30 = 3001/30 = 0.03310 * 30 * 0.033 = 9.9
10 px6010 * 60 = 6001/60 = 0.016610 * 60 * 0.0166 = 9.96
10 px12010 * 120 = 12001/120 = 0.008310 * 120 * 0.0083 = 9.96

With delta accounted, the game runs at the same speed at any framerate !

Delta is only used for movement, not for rotation or scaling or setting position