After installing NetBeans for its debugger, I've found that delta was a negative number. As it turns out, it was a simple typographical error.
delta+=(now-lastTime)-ns;
should be delta+=(now-lastTime)/ns;
Pregunta
I have the following code:
public void run(){
long lastTime=System.nanoTime();
final double amountOfTicks=60.0;
double ns=1000000000/amountOfTicks;
double delta=0;
int updates=0;
int frames=0;
long timer=System.currentTimeMillis();
while(running){
long now=System.nanoTime();
delta+=(now-lastTime)-ns;
lastTime=now;
if(delta>=1){
tick();
updates++;
delta--;
}
render();
frames++;
if((System.currentTimeMillis()-timer)>1000){
timer+=1000;
System.out.println(updates+" Ticks, FPS "+frames);
updates=0;
frames=0;
}
}
stop();
}
This should display "60 Ticks, FPS 9000000", but instead it displays "0 Ticks, FPS 9000000". Why is this and how can I fix it?
Solución
After installing NetBeans for its debugger, I've found that delta was a negative number. As it turns out, it was a simple typographical error.
delta+=(now-lastTime)-ns;
should be delta+=(now-lastTime)/ns;