Public Group

# Accurate timing in the game loop

This topic is 3709 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
This is the common way to do fixed time-step:

void Update (float dt){    accumUpdate += dt;    while (accumUpdate > timeStep)    {        accumUpdate -= timeStep;        DoStuff();    }}

##### Share on other sites
Thank you. How do you determine what time step to use?

##### Share on other sites
the timeStep is 10ms in your case. The function calling "update" must track how much time passed since the previous call to "update"

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by davidspornthe 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 ValManIf 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)?

##### Share on other sites
Quote:
Original post by Gage64
Quote:
 Original post by davidspornthe 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.

##### Share on other sites
Quote:
 Original post by MJPYou 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.

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by KylotanI 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.

• 18
• 11
• 16
• 9
• 51
• ### Forum Statistics

• Total Topics
631396
• Total Posts
2999790
×