0

I have been reading up on game loops and am having a hard time understanding the concept of interpolation. From what I seen so far, a high level game loop design should look something like the sample below.

ASSUME WE WANT OUR LOOP TO TAKE 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...
  }
}

Lets assume that update() and render() both take only 1 tick to complete, leaving us with 49 ticks to sleep. While this is great for our target tick rate, it still results in a 'twitchy' animation due to so much sleep time. To adjust for this, instead of sleeping, I would assume that some kind of rendering should be going on within the first if condition. Most code samples I have found simply pass an interpolated value into the render method like this...

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

I just don't see how this can work due to the 49 tick sleep time? If anyone knows of an article or sample I can check out please let me know as I am not really sure what the best approach to interpolation is...

Jason
  • 1,371
  • 4
  • 21
  • 44

1 Answers1

0

I know its a bit late, but hopefully this article will help http://gameprogrammingpatterns.com/game-loop.html

It explains game time scheduling very well. I think the main reason you are a bit confused is because of passing the render function the current elapsed time. Oh course this depending on which system you are using but conventionally the render doesn't modify the Scene in any way, it only draws it, therefore it doesn't need to know how much time has passed.

However the update call modifies the objects in the scene, and in order to keep them in time (e.g. playing animations, Lerps ect...) then the update function needs to know how much time has passed either globally, or since the last update.

Anyway no point me going to fair into it.... that article is very useful.

Hope this helps

LordSquall
  • 161
  • 7