2014-04-11 33 views
0

我一直在閱讀遊戲循環,並且很難理解插值的概念。從我目前看到的情況來看,高級遊戲循環設計應該看起來像下面的示例。瞭解插值

假設我們希望我們的LOOP採取50 TICKS

while(true){ 
    beginTime = System.currentTimeMillis(); 
    update(); 
    render(); 
    cycleTime = System.currentTimeMillis() - beginTime; 

    //if processing is quicker than we need, let the thread take a nap 
    if(cycleTime < 50) 
    Thread.sleep(cycleTime); 
) 

    //if processing time is taking too long, update until we are caught up 
    if(cycleTime > 50){ 
    update(); 
    //handle max update loops here... 
    } 
} 

讓我們假設更新()和render()都只拿1個滴答,而讓我們49只蜱睡覺。雖然這對我們的目標節拍率非常有用,但由於睡眠時間太長,它仍然會導致「顫抖」的動畫。爲了適應這種情況,我會假設某種渲染應該在第一種情況下進行。我只是發現大多數代碼示例傳遞一個插值到這樣的渲染方法...

while(true){ 
    beginTime = System.currentTimeMillis(); 
    update(); 
    render(interpolationValue); 
    cycleTime = System.currentTimeMillis() - beginTime; 

    //if processing is quicker than we need, let the thread take a nap 
    if(cycleTime < 50) 
    Thread.sleep(cycleTime); 
) 

    //if processing time is taking too long, update until we are caught up 
    if(cycleTime > 50){ 
    update(); 
    //handle max update loops here... 
    } 

    interpolationValue = calculateSomeRenderValue(); 
} 

我只是不明白這是如何工作的,由於在49刻度的睡眠時間?如果有人知道一篇文章或樣本,我可以檢查出來,請讓我知道,因爲我不知道什麼是插值的最佳方法是...

回答

0

我知道它有點晚,但希望這篇文章將幫助 http://gameprogrammingpatterns.com/game-loop.html

它很好地解釋了遊戲時間安排。我認爲你有點困惑的主要原因是因爲將渲染函數傳遞給當前流逝的時間。哦,當然這取決於你使用的是哪個系統,但是傳統上渲染器不會以任何方式修改場景,它只會繪製它,因此它不需要知道已經過了多少時間。

然而,更新調用修改場景中的對象,併爲了保持它們及時(例如播放動畫,Lerps等...),那麼更新功能需要知道已經過了多長時間或者全局,或者自上次更新以來。

無論如何,沒有人指出我會公平地對待它......這篇文章非常有用。

希望這會有所幫助