# How to obtain frame rate independence??

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
Genjix    100
while not done:    timetaken = GetTime()    dostuff()    timetaken = GetTime() - time    if timetaken > 0:        Pause(pausetime - timetaken)

##### Share on other sites
ToohrVyk    1596
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.

##### Share on other sites
Guest Anonymous Poster
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

##### Share on other sites
tolaris    288
Quote:
 Original post by paradisePlease 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.

##### Share on other sites
_the_phantom_    11250
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).

##### Share on other sites
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 ??

##### Share on other sites
bytecoder    100
Quote:
Original post by tolaris
Quote:
 Original post by paradisePlease 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.

##### Share on other sites
markr    1692
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