Here’s a basic algorithm for how a video game might work:
Do the following things repeatedly forever:
Read user input from the keyboard/controller/whatever
Update the state of the world one time (move bullets, turn player a bit, whatever)
Draw the screen;
This works quite well. We call each run of this loop a “tick,” and we can describe how fast things move in terms of ticks. A user might turn 2 degrees to the right per tick. An enemy might move 10 in-game distance units per tick.
But you see how nothing about this involves an absolute notion of time? That’s okay on old machines because “Draw the screen” is very slow and is also very predictable. It’ll happen some known number of times per second.
But then you stick the game on a modern PC which can both draw to the screen instantly in the background and also refresh the screen 60 or 120 or 240 times per second, and you also make the other steps of the loop much much faster, and the game is now going WAY faster than was ever intended, and the game itself won’t notice that anything is wrong.
Here’s a slightly more modern game loop:
Do the following things forever:
Set “time_delta” to current clock time – previous “time_delta”.
Update the state of the world by time_delta steps.
Draw the screen
Works great! Now if we’re weirdly fast, we just adjust the world less when we update. Very nice.
Latest Answers