I often come across the common question, “How do I allow for movement speed in games that is unaffected by frame rate?” While pondering this same question, I recognized the same problem in some games I have written where the object movement has always been too fast when the frame rates were really high. This article is aimed at showing a simple way around this problem. There may be plenty of examples and articles like this already available, but in this article I will offer my own approach to the solution.
In solving the movement problem, there are different methods you can use. One such common approach has been to implement delays to use up extra game cycles on faster computers. An example would be:
This code would loop without doing anything until the difference between the last time taken and the current time is equal to how long you want to delay for. This method can in fact slow down your game, but you would still see some speed differences on faster computers. Also, it can cause games to become choppy on certain machines.
A better approach is to change the movement based on how many frames are being rendered every second. To demonstrate this solution, I have modified NeHe’s Tutorial Number 23, which I hope many of you will be familiar with. This particular NeHe tutorial already uses a function for determining time, so we won’t need to perform a large number of modifications to the code.
The basic idea is to determine how much time has elapsed between frames and then change how much we want to move accordingly. As an example, in NeHe Lesson 23 you are walking around a small structure. To move you may want the character to move 2 blocks every second. To move those 2 blocks, all the movements for all the frames you are rendering in one second would have to add up to two. Thus, you must determine how much time is passing between your frames to get the frame rate.
First you must remove the old timer code from the program.
The next step is to define some variables in the main function. These are to be put at the top of the WinMain function.
float timer; //used to check the current time float timerdiff; //used to determine the time elapsed float secsperframe; //used to hold the value for how many seconds have elapsed between frames float desireddistance; //desired distance to move in a second float movementvalue; //value to move by each frame
Next, set the desired distance value to move over the course of a second. I found 2 to be a good number.
If you are getting 80 frames a second then you would want your character to move .025 units every frame since 80*.025 = 2. What you first must do is figure out how much time has elapsed since the last frame. To do this, take the current time before the main loop starts for the first time.
timer = TimerGetTime();