I am trying to implement the Game Loop interpolated found in this website http://www.gameprogblog.com/generic-game-loop/. Here is my implementation:
long frameRate = 50000000;
int maxUpdates = 3;
boolean yield = true;
private long time =0;
private long lastTime;
private long currentTime;
public static float interpolation;
public void gameLoop() {
long nanoElapsed = tick();
System.out.println(time + "t Framerate: " + frameRate + "t"
+ "Nanoelapsed: " + nanoElapsed + "t Interpolation: "
+ interpolation);
time += nanoElapsed;
int updateCount = 0;
while (time >= frameRate && updateCount < maxUpdates) {
game.update();
updateCount++;
time -= frameRate;
}
interpolation = getInterpolation();
game.render();
}
long tick() {
lastTime = currentTime;
currentTime = TimeUtils.nanoTime();
return currentTime - lastTime;
}
float getInterpolation() {
return (float) ((double) time / (double) frameRate);
}
However I cannot figure out the problem I am having. I am logging most of the variables and for some reason the time variable is very big and does not stay in an acceptable range. It also decreases over time. I have spent a long time trying to figure out why and I’m now stuck. Any suggestions and help would be appreciated. Thanks