As you probably know, animation is achieved by moving objects small amounts each frame, which then blend together into what appears to be smooth motion.
If you call "MoveEntity player, 0, 0, .1" every frame, then you're moving your player at a rate of .1 units per frame.
What many beginners fail to consider is that frames do not always progress consistently. Depending on the power of your computer, and the complexity of what's currently being displayed, and what's running in your computer's background, the frame time may change. Consequently, this means that the speed of your game objects will also change if you don't take this into account.
Correcting for the inconsistent frame times is really very simple. Using a simple formula, you can convert your inconsistent units-per-frame movement into units-per-second movement, which really makes a lot more sense. The easiest way to do this is usually to make a simple function library:
Global lastTime
Global timeScale#
Function UpdateSpeeds()
'Calculate the time ellapsed since last frame
currentTime = MilliSecs()
deltaTime = currentTime - lastTime
lastTime = currentTime
'And calculate a scale value to convert -per-second values to -per-frame values
timeScale = deltaTime / 1000.0
End Function
Function Eq#(unitsPerSecond#)
'Convert the given units-per-second value to units-per-frame
Return unitsPerSecond * timeScale
End Function
Simply call UpdateSpeeds() every loop and use Eq() to calculate the speeds used in the program.
The way it works is simple: Instead of moving the object a constant amount each frame, it moves it based on the amount of time the frame takes to complete. So if you're running at 100 frames-per-second, and you want your object to move at 1 unit-per-second, the object should move 1/100th of a unit each frame. If the frame rate jumps down to 30 FPS, to maintain the same real-world speed, the Eq() function will automatically adapt and start moving the object at 1/30th of a unit per frame.