improving game loop

hello guys, i am using below game loop for a long time now, i want to now if this is a correct approach or if i can improve a little bit more,
i would like to add a delta to update method but i dont know how that will impact


public final void run()
{

long lastTime= System.nanoTime();
double amountOfTicks=60.0;
double ns= 1000000000 / amountOfTicks;
double delta=0;
long timer= System.currentTimeMillis();

// int frames=0;
while(running)
{

    if(!pause)
    {
    
            long now= System.nanoTime();
            delta+=(now - lastTime)/ns;
            lastTime = now;
            while(delta >= 1)
            {
                
            update();
            //render();
            delta--;
            
            }
            
            if(running)
                render();
            
            //codigo para checar los frames
            frames++;
            if(System.currentTimeMillis() - timer > 1000)
            {
            timer +=1000;
            //System.out.println("FPS: "+frames);
            frames=0; 
            }
         
            
    }//
           
}//uail

  1. update with time step and not once per tick/frame
  2. don’t use System.currTimeMillis() if want to be good accuracy. In my experience, it leaves to being off by 1 or 2 frames per second.
  3. i would change !paused variable to playing variable
  4. if(running) render() is weird. Rendering is going to stop at the end of the iteration anyway
  5. sleep?!?
  6. Try not to worry too much about optimization. Although in this case, not sleeping is probably killing your CPU.

Here is my general game loop. Its not too different from yours. Copy if you want.


long start, end = System.nanoTime(), period = (long)(1.0e9 / TARGET_FPS);
		
while(running)
{
	start = System.nanoTime();
	
	client.update((start - end) / 1.0e9);
			
	end = System.nanoTime();
		
	try 
	{
		long remaining = period - (end - start),
		       sleep_ms = remaining / 1000000;
				
		int sleep_ns = (int)(remaining - 1000000 * sleep_ms);
				
		if(sleep_ms > 0)
			Thread.sleep(sleep_ms, sleep_ns);
	} 
	catch (InterruptedException e) {}
}

thank you i will try to implement that,

delta is inside client.update( /HERE/)?

and render is after update?

I recommend using the AnimationTimer from JavaFX. It reliably defaults to 60 fps, afaik, and is easy to use.

As an alternative (if not otherwise using JavaFX) using a ScheduledThreadPool with the game loop being contained in a Runnable. Goetz “Java Concurrency in Practice” (pg. 123). A util.Timer is not unreasonable as an alternative–and has an advantage of being possible to schedule to run TimerTasks at absolute time intervals, not just relative time intervals.

There is an interesting alternative structure for a game loop in a comment by @beeaware at the end of the JavaFX tutorial (which itself has a basic example). I would link to it directly but there doesn’t seem to be a way to do this, so you will have to scroll down. beeaware makes use of Timelines, and integer counters, and seems to work fine with the game that beeaware showed recently in the showcase.