#### Archived

This topic is now archived and is closed to further replies.

# Racing game timer

This topic is 5691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, i´m from Brazil. New on this forum. And would like to know how do i make the timer routine for my 3D racing game (super mario kart style). I´ve tried the time base movement from gametutorials, but it makes the cars controled by computer behave diferently depending of the frame rate. I think the best way could be to lock the fps at a certain rate and call the function renderscene(); to render the cars multiplying their coords by a timerfactor. And if the render function exceeds the time then return must be called to keep the framerate. Is this the right way? if not can anyone help me? Thanks a lot!

##### Share on other sites
just use something like this:

  long movementTimer=0;long speedOfGame=100; // The bigger the number the slower the game//--------------------------MainLoop() {  if ((long)timeGetTime()>(movementTimer+speedOfGame)) {    movementTimer=(long)timeGetTime();    updateGame();  }  RenderScene();}

May not be pure, but you should get the idea.

##### Share on other sites
that idea is horrible! imho.

do something more like this:
every time you update the game stuff, store the timeGetTime() in a variable. then, when u update the game next time, see how much time has elapsed and scale your movements and stuff accordingly.

##### Share on other sites
Won''t that still lead to differences in behavior? If your AI updates every frame, I don''t think you can get uniform behavior independant of frame rates. Perhaps you could look at updating your cars'' AI based upon the total amount of motion that has occurred in the game?

##### Share on other sites
It certainly will lead to inconsistencies in behaviour not to mention the extra amount of logic and calculations you'll have to put in there to make sure every thing happens exactly the same way.

But then again, everyone has there own way of doing things and one way is not necessarily the best way. It depends exactly what you want to achieve.

Also remember that no-one is forcing you to put everything inside that timer, you can choose what you put in! Just a hint

[edited by - redgenie on October 22, 2002 2:53:39 PM]

##### Share on other sites
static float last_time = GetTime();float cur_time = GetTime(); // current time in secondsfloat delta_time = cur_time - last_time;const float step = 0.01f; // movement every 10msif(delta_time>0.0f){    while(delte_time>step)    {        Update(step);        delta_time -= step;    }    Update(delta_time);}last_time = cur_time;

This is how I do it in my 3d racing. like

##### Share on other sites
For every game you make, you should try make a releastic universe which means it must have these SI units:
m - Space
kg - Mass , not so important
s - time very important
C - electric charge, no need for this one

ok, the one your intrested in is m & s, for you racing game. You need define what a meter is in your 3D world, this is just a bit of trial and improvement. Once you have it:
define Meter value

Then for every car you set its velocity & accelrating in meters per second, as some sort of const. If you were doing realstic physics model you would mass to come to conclusion about there velocity constants etc.

Anyways, every time the physics functions are called they are passed a variable(delta) which is the change in time since the last physics call in seconds. Since all your velocity units and accelartion units are in meters per second. Just multiply the const velocity speed for the car by the chage in time to get how far the car has come, e.g.:

const int MaxVelocity = 60,000 ms(60 Km)

void Physics(int delta)
{
int MyPosition+=delta*MaxVelocity
}

lets say you are getting 2 fps, therfore the physics function is being called every 1/2 a second:
=0.5*60,000
=30,000

so this model, is actully frame rate dependant wheither the computer is going 700 fps to 3 seconds a frame the physics will work.

hope this helped: Iain

##### Share on other sites
That will work if you move straight.
But think about 2fps when you are turning to right. 50fps your car goes along the road, but 2fps you find yourself bottom the ditch.
You need to split that 0.5s into smaller steps like I showed above

##### Share on other sites
Did you use Direct X for the graphics?

##### Share on other sites
What has the rendering API have to do with the physics?

All you must do is make the logic run at a constant rate independent of the framerate.

It runs every X milliseconds.

If it hasn''t been x millis, skip it. Render.
x millis, run it. Render.
4x millis, run it four times. Render.

Get the idea?

• 9
• 13
• 41
• 15
• 13
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!