#1 Members - Reputation: 184
Posted 25 June 2012 - 10:30 AM
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!
#2 Moderators - Reputation: 2485
Posted 25 June 2012 - 10:35 AM
#3 Members - Reputation: 3716
Posted 25 June 2012 - 11:25 AM
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)
The voices in my head may not be real, but they have some good ideas!
#4 Members - Reputation: 123
Posted 25 June 2012 - 07:46 PM
http://dewitters.koonsolo.com/gameloop.html
#5 Members - Reputation: 184
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
Many thanks for all the replies! This article's explanation is perfect!
#6 Members - Reputation: 184
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
Many thanks for all the replies! This article's explanation is perfect!
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?
#8 Members - Reputation: 3716
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).
The voices in my head may not be real, but they have some good ideas!






