So far I have this implementation of "FYTS".
However I am facing several problems.
final int TICKS_PER_S = 60;
double accumulator = 0.0;
//The timestep
final double dt = 1/(double)TICKS_PER_S;
long previousTime = System.currentTimeMillis();
//State previous;
//State current;
while(running){
long currentTime = System.currentTimeMillis();
double frameTime = (currentTime - previousTime)/1000;
if(frameTime > 0.25){
frameTime = 0.25;
}
previousTime = currentTime;
accumulator += frameTime;
while(accumulator >= dt){
//previousState = currentState;
this.gamePanel.update(dt);
accumulator -= dt;
}
//Interpolation
// final double alpha = currentState * alpha +
// previousState * (1.0 - alpha);
//This is the draw call. Not yet using interpolation
this.gamePanel.postInvalidate();
}
Problem 1:
frameTime is always 0 meaning that the game is never updating. It seems obvious: In the beginning it is 0, but that should change, shouldn't it?
Problem 2:
Let's assume that Problem 1 wouldn't exist. I understand that you take an arbitrary frameTime and split it up into discrete timesteps and then update the game. But what's the point of updating in a separate nested loop anyways? Most of the time you are left with a little remainder at the end of finishing the inner update loop. That remainder is how much of the simulation is left to complete until the running loop starts again and takes a new frameTime on which a part of the simulation has to be performed on. But what does the interpolation do that's worth to even consider doing it Isn't the inner update loop just a waste of time?
Problem 3:
The outer loop is running without any limitations at max speed. Isn't that just wasting clock cycles (and draining the battery life on mobile devices)? Is there anything I could do to counteract that?