How to obtain frame rate independence??

Started by
8 comments, last by markr 18 years, 10 months ago
Hi , I am making a car game and I need to get frame rate independence to get some restrictions done in the game. Please tell me as many ways to obtain frame rate independence in the game loop . Thank You
Advertisement
You want your rendering/rasterization to be independant of all the calculations that need to be done with your AI/Physics? If this is the case you could multi-thread your app so that people w/ dual-core processors or multi-processor systems could do that.
while not done:    timetaken = GetTime()    dostuff()    timetaken = GetTime() - time    if timetaken > 0:        Pause(pausetime - timetaken)
Get the time elapsed since the beginning of the game (or the level, or whatever arbitrary origin of time you choose). Divide this time by the duration of a logical step: this will give you the amount of steps that should have been performed since then.

Now, count the number of steps you actually did. If it is smaller than the one you computed, perform some additional steps so you keep up. If it is equal, wait a little (or spend some time rendering or doing some background task). It will never be greater by definition.

So in the end, you have framerate-independent logical steps.
Have you tried using threads? ie. create the main vehcile dynamics in a seperate thread and update your position and rotational matrices in a shared structure which your game loop (probably at a slower rate) can use when it needs to update the eyepoint / geometry.

Mr. Creamy
Quote:Original post by paradise
Please tell me as many ways to obtain frame rate independence in the game loop .

fix your timestep. Not abso-exact-lutely the same thing, but the final code leads to what you want, plus the explanations provided are very nice.
The duel threads method mentioned a couple of times here is a good way to handle this, I've waffled a bit about my own implimentation (inspired by something Superpig mentioned a while bacl) which gives quiet acceptable results for 8 spining cubes at least [wink]
Once I get some free time I'm going to try and improve it so the structure is better (currently a hack job) and have it handling more complex cases to see how things scale on a single CPU system (I currently dont have access to a duel core system for testing, which is a shame).
Thank you all for the replies.


and genjix,could you please explain what are "time" and "pause time" variable ??


while not done:
timetaken = GetTime()

dostuff()

timetaken = GetTime() - time

if timetaken > 0:
Pause(pausetime - timetaken)


I'm not understanding how this is leading to frame rate independence ??
Quote:Original post by tolaris
Quote:Original post by paradise
Please tell me as many ways to obtain frame rate independence in the game loop .

fix your timestep. Not abso-exact-lutely the same thing, but the final code leads to what you want, plus the explanations provided are very nice.

Seconded.
Just have a timestep size, greater than you're likely to have FPS. Like 100 or something.

Then do

do {  while (ticks done < ticks passed to date) {   Do a tick;  }  Render();} while (true)


Of course you might want to put in a maximum number of ticks between frames, so that if the FPS becomes too low, you don't end up doing too many ticks (slow the game down instead). Essentially limit the number of frame skips to 10 or something.

If your rendering ends up happening faster than 1 tick, you'll simply render the same frame twice.

Or you could use interpolation, but that is more complicated.

Don't use threads unless you realise all the implications of doing so and are prepared to live with them.

Mark

This topic is closed to new replies.

Advertisement