Seperating game logic from game loop?

Started by
6 comments, last by SimonForsman 11 years, 9 months ago
Strange title, but I didn't know how else to word it.

Basically what I want to do is to have my game events time based instead of frame based. This is to ensure that, if I want something to move across the screen in 2 seconds, whether the game plays at 1000 fps or at 10 fps, it takes 10 seconds.

I've been thinking about this but I'm not too sure how I would structure my game and whatnot. I'm not asking about implementing this in a specific language, but just how I would go about creating my game like this?

Thanks!
Advertisement
Typically the easiest (but not best) way to handle this is to compute the interval between frames and assign all your values in a fixed time unit (seconds or milliseconds usually). This requires getting an accurate system timer (QueryPerformanceCounter on Windows), storing the time between frames, and multiplying all your values by the elapsed time in order to get the correct numbers.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
a fairly simple method to get a fixed timestep is:
const float timeStep = 0.016667; //60 logic updates per second)
float currentTime = lastFrameTime = getTimeInSecondsAccuratlyFromTheOs();

while(!done) {
currentTime = getTimeAccuratlyFromTheOs();
while(currentTime > lastFrameTime + timeStep) {
update(timeStep);
lastFrameTime+=timeStep;
}
}

this gives you a stable simulation(the results should be the same regardless of the actual framerate) and also allows you to change the size of the timestep if needed (you shouldn't change it at runtime though).

the update method should then update the gamestate based on the timestep you pass to it, i.e to move a unit you do position+=velocity*timeStep.

To get an even better result you can store 2 gamestates, one current and one old and have the update method read from the old state and write to the new state (and on the following update switch them around), this lets you interpolate between the states when rendering and also makes paralellization easier (multiple components/entities/whateveryou'reusing can read from the old state at the same time and write to their own part of the new state without any synchronization)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
This is a nice article related to your question, in case you haven't read it yet:
http://dewitters.koonsolo.com/gameloop.html

This is a nice article related to your question, in case you haven't read it yet:
http://dewitters.koo...m/gameloop.html


Many thanks for all the replies! This article's explanation is perfect!

[quote name='enunes' timestamp='1340675173' post='4952860']
This is a nice article related to your question, in case you haven't read it yet:
http://dewitters.koo...m/gameloop.html


Many thanks for all the replies! This article's explanation is perfect!
[/quote]

Okay, follow up question:

So the whole rendering as fast as possible but keeping the actual game loop at constant speed seemed like a brilliant idea to me. It would go smoother on faster PC's and choppier, but still fast paced, on slow PC's, right?

There's just a teency problem with that.

On a slow PC, the game won't be able to render as fast, so since it's trying to render as fast as possible, the time taken for each frame fluctuates, even if ever so slightly, which causes the update in the game/physics step to look jumpy. So if one frame takes a few milliseconds longer, the character moves more than in the other frames and this is very noticeable.

How do other people deal with this problem?

I thought of giving the player the option of either choosing the FPS they want to run the game at, so higher FPS means the game itself will run slow, but smooth if they're computer can't handle it, but a slower FPS would mean the correct game speed but choppier movements. And also have an "auto fps" which simply goes down to the lowest fps. (So it doesn't go up and down depending on computer speed, just goes down to minimize fluctuations)

Comments/ideas/tips?
Here are all the answers!

http://gafferongames.com/game-physics/fix-your-timestep/

On a slow PC, the game won't be able to render as fast, so since it's trying to render as fast as possible, the time taken for each frame fluctuates, even if ever so slightly, which causes the update in the game/physics step to look jumpy. So if one frame takes a few milliseconds longer, the character moves more than in the other frames and this is very noticeable.

How do other people deal with this problem?


i mentioned this in my post earlier, store two (or three can be a good idea for multithreaded engines) copies of the game state, the old state and the "current" state , then interpolate between them based on the exact time when you render (Thus you will let rendering lag slightly behind instead). (so if logic frame 2 is at 605.02 and logic frame 3 is at 605.04 and you have frame1(from 605.0 and frame2 stored), the loop comes around, you grab the current time and it says 605.035 now rather than rendering frame 2 (The last logic frame) we interpolate between frame1 and frame2 (we're 75% of the way to the next logic frame (timestep 0.02 and we've done 0.015, 0.015/0.02 = 0.75)
Thus, for linear movement we just take the positions(as vectors) in frame 1 and scale them by 0.75 and add the positions from frame2(scaled by 0.25) and voila, we get smooth movement. (For rotations its probably easiest to slerp quaternions (you could just get a library for that).
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement