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

Determining Time Differences

After rendering the scene, compare the current time with the one you last took. We will store this in the secsperframe variable.

//first determine how many seconds elapsed
timerdiff = TimerGetTime()-timer;

//the function reads in milliseconds so convert to seconds by dividing by 1000
secsperframe=(float)(timerdiff/1000.0f);

Let's say the number comes out to be .0125. To get the frame rate, simply divide 1 second by that .0125. This gives you an answer of 80, since 80 times that .0125 is equal to 1 second. To determine how much to move, you simply take the desired distance and divide by the frames per second.

Movement = (desired distance)/(frames a second)

In this example, that would be 1 divided by 80 which would give you .0125 as stated above. The equation can be changed, though, if we substitute in the equation for frames per second. Now it reads:

Movement = (distance desired)/(1/seconds per frame)

This can be further simplified to:

Movement = (distance desired)*(seconds per frame)

I find this equation to be the easiest to use. In NeHe Lesson 23, this looks like:

//now compute the movement value
movementval = (float)(desireddistance*secsperframe);

//get the new time
timer = TimerGetTime();




Next : Moving the Character