i've been following a series on youtube on how to create a 2d game using java, and i cant seem to wrap my head around how this piece of code works, if anyone can elaborate how this code works id be very gratefull.
public void run() {
long lastTime = System.nanoTime();
final double clock = 1000000000.0 / 60.0;
double delta = 0;
while (running) {
long now = System.nanoTime();
delta +=(now - lastTime) / clock;
System.out.println(now-lastTime);
lastTime = now;
while (delta >= 1){
update();
delta--;
}
render();
}
}
What i have understood from this piece of code is that the clock variable contains how much time one frame should take if our game were to run 60 frames per second,but i cant seem to wrap my head around the now-lastTime variables since they seem to vary all the time, but we get the same time taken in every execution of the loop, thus getting 60 fps. I understand the purpose of the loop, what i actually dont understand is how it mathematically comes down together so that delta >= 1 happens 60 times a second.