Game Loop

Hello.

I’ve thinking about game loops and the most popular approach it seems is getting the elapsed time since the last frame and using
that for the next frame’s update method. I’ve treid this approach and it’s not pretty, the game loop trys to run as fast as possible and
then the result is a slightly choppy version of what the game could be.

My idea is to calculate at runtime the average fps of the game and then use that as a frame cap, the problem with this idea is what
if it’s going 70 fps then it’s going 30 fps is it going to be stuck at 50 fps? If you could have a approach that keeps the fps and a certain
level under the choppy fps level that would give you a very smooth game rate and you could still use the elapsed time since last frame.

So I’m thinking how I could implement it myself or use find code that has implemented it.

Thanks.

I wouldn’t cap on the average fps. Here’s an excellent article on game loops. The approach it recommends is a good one.

Hello Aldacron.

Thank you for the article, it seems to be the best approach I’ve seen so far and
I’ll see if I can implement that. :slight_smile:

The interpolation idea is nice, but isn’t it a bit difficult to do except for trivial cases? His example position=position+speed*interpol; is fine. But what if the car in this example would have hit something (a wall, an opponent…) at 10.1? In the example, he would move the car into the obstacle (and maybe vice versa) until the next game tick, where he would detect the collision and resolve it. So the car and the opponent start to jump back to where they actually belong.

Yes. Frankly, I don’t see how the interpolation idea could actually work. If I wanted to do something like that, I would just make the update phase happen more often.

I think his idea is that the error would be small, but I don’t think it’s worth the effort.

That’s what i was thinking too. What he basically does, is to take a part of the game logic (because moving an entity is game logic) and put that (in a simplified(?) form) into the actual rendering. The next step would be to take care of errors that the interpolation introduces and so on and so forth…and finally, he ends up with two timed game logic parts, one with a finer granularity but a coarser logic and one that is the other way round. The one with the finer granularity will also suffer from what he described in his other example (bad accuracy if time slices are getting tiny). It just doesn’t matter, because he corrects it with the coarser one…strange…