framerate independent game speed

Started by
8 comments, last by Xeee 21 years, 11 months ago
Sorry to open such old topics , but i''ve searched and i didn''t find what i want , isn''t there another way to achieve framerate independent game speed other that the way that you multiply all the displacements in the game with some value(that value gets smaller as your computer gets faster so the game speed is constant)?? i mean is there a way to skip/waste the current cycle or something like that? ...Xeee
xee..
Advertisement
You have to multiply your objects speed by the amount of time elapsed between frames. Don''t think of your objects speed as X. Think of it as X per second.

So during your timetick code, pass a parameter that is the tick count of the amount of time elapsed. Works very nice. The only problem it if you get very low framerates, your objects will "warp."

Skipping ticks in your engine due to high frame rate is not a good solution.
"Skipping ticks in your engine due to high frame rate is not a good solution"

...unless you have other important threads running, such as a garbage collector, or possibly physics or ai in a separate thread. But generally its not a good idea.
If you have something running in a separate thread you have no control over the running of it anyway. That is unless you call Sleep.

A better solution is to "divy" up AI routines while rendering frames. Quake did this by giving a certain number of bots an AI tick after rendering a frame.

Don''t get me wrong. I like threading. The problem is that MOST people have single processor systems and multi-threading only adds additional processor time in changing contexts. It will be much more efficient if you handle timing of other game engine routines during your rendering routine.
quote:Original post by Xeee
i mean is there a way to skip/waste the current cycle or something like that?


Bad. What if the game runs too slowly on someone''s computer?
quote:Original post by glassJAw
Bad. What if the game runs too slowly on someone''s computer?

Then they shouldn''t be playing your game in the first place!



CEO Plunder Studios
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
I swear this comes up every freaking day...

On Windows use WaitForSingleEvent() or timerSetEvent() to create an event loop which gets called exactly 100 times per second (or 50 or 25 or 10 if that''s all you need).
so you''re all telling me not to use ticks/cycles skipping , right!!
and the other way is more reliable and common , ok , i''ll go on with the other way
actually i don''t like it coz it''s gonna make me change in the game itself but the skipping is just a statement here and it''s done!
xee..
I prefer locking the frame rate, then skipping frame drawing if the game runs too slow. It''s easier than working with floats and worrying about rounding errors. This is good for 2D games because you can guarantee that everything moves a set amount of pixels.

First, I calculate how many ticks per frame, then I sit in a loop that counts and accumulates the ticks, checks how many frames need to be done, processes movement and everything for each frame (no drawing though), then draws the latest frame only:

while(runflag){  cur_tick = GetTicks(); // Whatever function you use  tick_count += (cur_tick - last_tick); // Accumulate ticks  last_tick = cur_tick;  if(tick_count > ticks_per_frame)  {    while(tick_count > ticks_per_frame)    {      ProcessNextFrame(); // Movement, etc.      tick_count -= ticks_per_frame;    }    DrawCurrentFrame(); // Draw current game state  }  else  {    Sleep(0); // For windowed programs, doesn''t hog the CPU  }} 


You can also set animation speeds in frames/cell, which is a simple conversion of ((frames/sec) / (cells/sec)). Of course it''s better if it divides evenly.

I''ve used this skipping method many times and it works great. I see no reason to have the game run at 200fps. 40fps looks fine to me.
Sorry, I realize my post is a little off topic, since it isn''t a way to achieve "framerate independent" speed. It''s still dependent on framerate, but it''s a way to handle slowdown on slower computers. I''ll leave it there as informational anyway.

This topic is closed to new replies.

Advertisement