1

我已经在我的游戏循环中实现了增量时间,因此帧速率的任何波动都不再重要,但是游戏不会在更快的机器上运行得更快而在慢的机器上运行得更慢吗?

我的印象是大多数游戏以固定的速率(每秒 60 次)更新逻辑,然后执行尽可能多的渲染。如何使更新每秒循环多次?

4

1 回答 1

1

只需使用累加器……每次游戏循环运行时,+= 所经过的时间增量。一旦该时间大于 16.6666667 秒,运行您的更新逻辑并从您的时间中减去 60 秒。继续运行更新方法,直到累加器小于 60 秒 :-)

伪代码!

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;
}

这种技术意味着如果 draw 方法花费的时间比更新频率长,它就会赶上。当然,您必须小心,因为如果您的 draw 方法通常花费超过 1/60 秒,那么您将遇到麻烦,因为您将始终必须在两次 draw 通道之间调用 update 多次,这可能导致渲染中的打嗝。

于 2011-09-21T10:51:18.440 回答