Time?

Started by
5 comments, last by ElPeque2 15 years, 2 months ago
I am confused about how time works in a game, the frame rate, the game loop iterations, some engines working with different units of time measurement etc. How it all comes together. Any info would be appreciated. Thanks.
Advertisement
The frame rate is the number of times a second the display is rendered. this is usually around 30 frames per second (FPS). That is, this is the number people tend to aim for and lock the display rate to.

Game loop iterations are just cycles of the game loop. Since your rendering is usually part of the game loop, FPS and game loop iterations will often be the same value (ie number of iterations a second = number of frames a second). Sometimes you might skip updating the render if your game is struggling with a lot of other work like updating a lot of physics, or processing a big file load, etc. Despite the emphasis placed on graphics, it sits pretty near the bottom of the priority list for updates. Also if your application runs several threads then rendering and game loop may be totally independant.




The frame rate can be calculated in a number of ways, but the easiest is to take the time since the last render, and divide 1 by it. So if the last render was 0.5s ago, then 1/0.5s = 2 -> The application has a frame rate of 2fps.
Framerate (per second)= how many times your game runs through your game loop each second

Now, because you don't want various things in your game to run alot faster on a higher spec pc, than on an average pc, you need to control certain things using time.

The engine you make, or the engine you are using will store time. in order to keep different spec pc's from giving different results with regards to speed (eg, animations, running speeds, attack rate etc) You need to incorporate the time into many of those functions by multiplying specific variables in those functions by the time.elapsed (since the last loop interation), to take time into account. That way, all of those things depend on time, and not on your Frame rate, and different pc's should synch and show the same results.
time as in the time returned by the timeGetTime() function? Right now I have it setup so that

time = timeGetTime();

if((time - time_since_last) > cooldown)
{
time_since_last = time;
//do stuff
}
It depends whether you are using an engine, or whether you are trying to make an engine. If you are using an existing engine, then it probably provides a function that returns the time elasped, if it doesn't then i guess you do need to add it. Google for the most efficient ways of doing this, also keep in mind that it needs to be fairly accurate (milliseconds). See what others are using with the same technology you are using.

If you have a function you use, that returns the elapsed time, like for the engine im using, then only call that function once per loop (at the top), save it in another variable, and reference that variable in your calculations, not the function. If you keep calling the function, it will give you inaccurate timing because it works out elapsed time from its last call.

Looks right.. you are also throttling the game loop to run at no more than x fps. I have never done that, so I dunno if thats good or bad.

[Edited by - HumanoidTyphoon on January 20, 2009 6:09:44 AM]
Now all you have to do is multiply anything that should rely on time for its speed calculations, to the time variable.

Im not sure if you can monitor certain things when throttling your loop like that. Lets say you are throttling your fps to 60. Your fps potential could be 150. Now if you add an overly complex mesh, or an inefficient algorythm, and your fps drops down to 75 based on this 1 thing you added, how are you going to know? How are you going to gauge that you may need to make the mesh lower poly, or only run the algorythm once every x iterations of the loop instead of everytime?
http://gafferongames.wordpress.com/game-physics/

:)
http://www.teatromagico.net

This topic is closed to new replies.

Advertisement