Upcoming Events
Southwest Gaming Expo
11/20 - 11/22 @ Dallas, TX

Workshop on Network and Systems Support for Games (NetGames 2009)
11/23 - 11/25 @ Paris, France

ICIDS 2009 Interactive Storytelling
12/9 - 12/11 @ Guimarães, Portugal

Global Game Jam
1/29 - 1/31  

More events...


Quick Stats
7370 people currently visiting GDNet.
2341 articles in the reference section.

Help us fight cancer!
Join SETI Team GDNet!



Link to us

Link to us

  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