First, obligatory read if you didn't read already:
http://www.koonsolo.com/news/dewitters-gameloop/
Your loop is one of many designs. To be honest, not my favorite (and I think there's a mistake), but here we go:
You grab the amount of time (in nanoseconds) that one frame should have in a 60FPS case:
final double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
In the first step of your loop, you compute the elapsed time between this and the last frame. Since it's the first frame, it grabs the elapsed time since of the first call of 'System.nanoTime();', your lastTime:
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
Then, you update the game AS LONG as
Then you update, but if the delta time since last time was too long (more than 1 second), update again:
if(delta >= 1) {
tick();
updates++;
delta--;
}
I believe that should be:
[removed]
Here you update ('tick()') as long as the frame should take. So, in a single frame (16 milliseconds), you might have more than one update. After that, you render.
Your last step prints the information after 1 ellapsed second:
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("Ticks: " + updates + ", Frames: " + frames + "\n");
updates = 0;
frames = 0;
}
It updates 'timer' for when will be the next second (and print of information), then you print the total of updates (ticks) and frames inside that second. Last, you reset the count of updates and frames inside that second.
Here's what I don't like. You update as long as you can. First, it's CPU intensive, it doens't give time for the CPU to rest. Second, that update (tick()) will be called during all the expected 16 milliseconds of each frame, and THEN the render is called. So, your renderization will be called AFTER 16 milliseconds, which means your frame will take more than 16 milliseconds, necessarily your FPS will be under 60.
[Post corrected]