Accurate timing in the game loop

Started by
21 comments, last by Gage64 15 years, 12 months ago
Suppose my game runs constantly at 60 fps. That means that each frame lasts about 16 ms. Now suppose that I want to perform an action every 10 ms. The obvious way to achieve this is to check each frame if 10 ms have passed, and if it has, perform the action. Unfortunately this doesn't work. Using this method, the action will be performed at most once per-frame, meaning that it will really be performed every 16 ms. How can I solve this? Thanks in advance.
Advertisement
This is the common way to do fixed time-step:

void Update (float dt){    accumUpdate += dt;    while (accumUpdate > timeStep)    {        accumUpdate -= timeStep;        DoStuff();    }}
Thank you. How do you determine what time step to use?
the timeStep is 10ms in your case. The function calling "update" must track how much time passed since the previous call to "update"
The short answer is that you can't (with your present game loop setup).

The long answer is that you will probably need multi-threading. If one iteration of your game loop takes 16 MS, it's either because you fixed it to that interval (see MJP's post), because you are CPU bound, or because you are GPU bound (including Present()'s waiting for vsync). Either way, your main game loop will be busy doing something. The only solution, then, is to use another thread to check continuously if the interval has elapsed.
Quote:Original post by davidsporn
the timeStep is 10ms in your case.


That can't be right. I can have many actions, some of which will be performed every 5 ms, some every 50 ms, etc, so the time step has to be based on something else.

Quote:Original post by ValMan
If one iteration of your game loop takes 16 MS, it's either because you fixed it to that interval (see MJP's post), because you are CPU bound, or because you are GPU bound (including Present()'s waiting for vsync). Either way, your main game loop will be busy doing something.


The 16 ms was just for the sake of discussion. Besides, I think pretty much every game supports this in some way. Are you saying that they all use multi-threading (even the old ones)?
Quote:Original post by Gage64
Quote:Original post by davidsporn
the timeStep is 10ms in your case.


That can't be right. I can have many actions, some of which will be performed every 5 ms, some every 50 ms, etc, so the time step has to be based on something else.


You can implement this for every action if you want, with timeStep being a member variable for that particular class. This won't allow you to update things at the exact moment the time step elapses, but the end result will be the same over a long period of time.

Quote:Original post by MJP
You can implement this for every action if you want, with timeStep being a member variable for that particular class.


If you mean what I think you mean, this won't work properly. If you'll read a couple of posts starting from here, you'll see what I mean.
I would argue that it would be best to give up on the idea of getting anything to happen every 5 milliseconds. That sort of resolution isn't really reliable without a lot of work.
Quote:Original post by Kylotan
I would argue that it would be best to give up on the idea of getting anything to happen every 5 milliseconds. That sort of resolution isn't really reliable without a lot of work.


Again, it was just an example. The question is, how do I do this for a frequency that's higher than the game loop's. For example, if it's running at 30 fps, each frame lasts about 33 ms, and suppose I want something to happen every 20 ms.

This topic is closed to new replies.

Advertisement