Jump to content
  • Advertisement
Sign in to follow this  
Gage64

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.

If you intended to correct an error in the post then please contact us.

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 this post


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


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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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)?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!