|See what's going on with flipcode!|
Timing In Games
Question submitted by (02 September 1999)
|Return to The Archives|
|How can i make an in-game animations framerate-independent?|
Let's take a really simple example of a moving box in a situation where
your frame rate might fluctuate between 10 and 30 FPS.
If you simply move the box 7 units forward each frame, it will be tied to the frame rate (moving faster with higher frame rates.) No magic needed here, the solution to this is quite simple.
Rather than specifying your movement in units, specify it in units per second. Consider a situation running at 30Hz. You time each of the frames, and you end up with 0.0333 seconds per frame. You simply multiply this number by the number of units to move per second (in this case, 7) and you end up with the proper number of units to move (0.2333 units.)
Be sure to time each frame, so you know exactly how much time has gone by. This allows you to maintain the time-based animations during highly fluctuating frame rates.
Response provided by Paul Nettle
This article was originally an entry in flipCode's Fountain of Knowledge, an open Question and Answer column that no longer exists.