Archived

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

Xeee

framerate independent game speed

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
"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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites