• Create Account

## Seperating game logic from game loop?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

7 replies to this topic

### #1OmarShehata  Members

Posted 25 June 2012 - 10:30 AM

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!

### #2Promit  Senior Moderators

Posted 25 June 2012 - 10:35 AM

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 | Shark Eaters for iOS | Ventspace Blog | Twitter | Proud supporter of diversity and inclusiveness in game development

### #3SimonForsman  Members

Posted 25 June 2012 - 11:25 AM

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)
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!

### #4enunes  Members

Posted 25 June 2012 - 07:46 PM

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

### #5OmarShehata  Members

Posted 26 June 2012 - 01:45 AM

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

### #6OmarShehata  Members

Posted 05 July 2012 - 12:45 AM

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

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)

### #7mark ds  Members

Posted 05 July 2012 - 12:48 AM

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

### #8SimonForsman  Members

Posted 05 July 2012 - 01:42 AM

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).
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!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.