fixed game update cycles [closed]
开发者_高级运维
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this questionI've implemented delta time in my game loop so that any fluctuations in frame rate don't matter any more, however won't the game still run faster on faster machines and slower on slow ones?
I was under the impression that most games update the logic at a fixed rate (60 times a second) and then perform as many renders as they can. How do I make the updates loop as many times as I want per second?
Just use an accumulator ... every time your game loop runs, += the delta of time that passed. Once that time is greater than 16.6666667 seconds, run your update logic and subtract 60 seconds from your time. Keep running the update method until the accumulator is < 60 seconds :-)
pseudo-code!
const float updateFrequency = 16.6666667f;
float accumulator = updateFrequency;
public void GameLoop(float delta)
{
// this gets called as fast as the CPU will allow.
// assuming delta is milliseconds
if (accumulator >= updateFrequency)
{
while (accumulator >= updateFrequency)
{
Update();
accumulator -= updateFrequency;
}
}
// you can call this as many times as you want
Draw();
accumulator += delta;
}
This technique means that if the draw method takes longer than the update frequency, it will catch up. Of course, you have to be careful because if your draw method routinely takes more than 1/60 of a second, then you'll be in trouble because you will always have to be calling update several times in between draw passes, which could cause hiccups in the rendering.
精彩评论