Upcoming Events
Unite 2010
11/10 - 11/12 @ Montréal, Canada

GDC China
12/5 - 12/7 @ Shanghai, China

Asia Game Show 2010
12/24 - 12/27  

GDC 2011
2/28 - 3/4 @ San Francisco, CA

More events...
Quick Stats
38 people currently visiting GDNet.
2406 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!
Link to us Events 4 Gamers
Intel sponsors gamedev.net search:

Contents
 Limiting Movements
 Determining the
 Difference

 Moving the
 Character


 Printable version
 Discuss this article
 Source code

Introduction

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:

while (GetCurrentTime()-starttime

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.

Limiting Movements

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.

//while(TimerGetTime()

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.

desireddistance=2.0f;

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();




Next : Determining the Differences