Game Loop too fast

Hello everybody,

this is a very nice forum.

I have got a problem with my run - game loop. The first 1-2 seconds it has more than 1800 FPS and after that it is about 300, althoug I set the target FPS to 60.
I tested this loop and animated a polygon which moves from x:0 y:0 diagonal down. After the FPS break from about 1800 to 300 it runs with full speed out of sight :smiley:

These are the deltas at that special moment:

0.15598080623923224
1.5791460031658402

	public void run() {
		final int MAX_FRAME_SKIPS = 5;
		final int NO_DELAYS_PER_YIELD = 16;
		final int TARGET_FPS = 60;
		final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;

		long beforeTime, afterTime, timeDiff, sleepTime;
		long overSleepTime = 0L;
		long excess = 0L;
		long period = 1000 / TARGET_FPS;

		int noDelays = 0;
		int fps = 0;
		int lastFpsTime = 0;
		
		double delta = 1.0;
		
		beforeTime = System.nanoTime();
		isRunning = true;

		while (isRunning) {
			update(delta);
			render();
			paintScreen();

			afterTime = System.nanoTime();
			timeDiff = afterTime - beforeTime;
			sleepTime = (period - timeDiff) - overSleepTime;
			
			delta = timeDiff / ((double) OPTIMAL_TIME);
			
			lastFpsTime += timeDiff;
			fps++;

			if (lastFpsTime >= 1000000000) {
				printFps = fps;
				lastFpsTime = 0;
				fps = 0;
			}

			if (sleepTime > 0) {
				try {
					Thread.sleep(sleepTime / 1000000L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
				
			} else {
				excess -= sleepTime;
				overSleepTime = 0L;

				if (++noDelays >= NO_DELAYS_PER_YIELD) {
					Thread.yield();
					noDelays = 0;
				}
			}

			beforeTime = System.nanoTime();

			int skips = 0;

			while ((excess > period) && (skips < MAX_FRAME_SKIPS)) {
				excess -= period;
				update(delta);
				skips++;
			}
		}

	}

I really hope, that somebody could help me. Maybe to improve my loop as well.
Thank you very much in advance,
Best regards, Kronos.

edit:

embarassing moment :persecutioncomplex:

I used myPolygon.translate(x, y);
I don’t know what this does exactly, but not what I wanted it to do. The loop seems to be okay, beside that it does not reach my target FPS which is set to 60. :frowning:

Ah, it’s from “Killer Game Programming in Java”. :slight_smile:

Maybe I have something:


      long period = 1000 / TARGET_FPS;

     [...]

      while (isRunning) {
         update(delta);
         render();
         paintScreen();

         afterTime = System.nanoTime();
         timeDiff = afterTime - beforeTime;
         sleepTime = (period - timeDiff) - overSleepTime;

You have time values in nanosecond-precision. But the period is only in milliseconds. Isn’t it? Haven’t tested it.

If you’re using nanoseconds you have to append:


update(delta / 1000000);

for it to return the correct delta.

Delta update with no division:
16666666ms.

Delta update divided by 1000000:
16ms (Correct).

Hope it helps mate.

Thanks guys. I have changed the period variable to: long period = 1000000000 / TARGET_FPS;

no I can use a more “normal” speed value -> 1.75 e.g. before it was 0.025 I think :smiley:
But the FPS still shows about 280.

A delta value about 0.2 to 1.2 is pretty normal, isn’t it?

p.s.: it is too obvious that this is the source code from the killer gaming book :stuck_out_tongue:

P.s.s: What does Polygon’s translate do? I wrote the coordinates to the console, and x an y were only at about 20 and the thing flew around like fire :smiley:

If I had a delta of 0.2 to 1.2 in my Game I’d be in trouble lol.


x += 0.35f * delta;

It wouldn’t return >= 1 so my player wouldn’t be able to move (Coordinates are in Integers, not floats).
(Would produce choppiness) Or so I’d presume O.O.

Might be different for you, depending on how you’re going to be using the delta.

It is not the law, but usually delta values are in milliseconds. Or at least this is what most people do. So with a FPS of 100 a delta of 10 would be quite normal. But with correct methods it could be completely alright to do it any other way, I think.

Hello, thanks for your answers.

Is this game Loop good for 4K Contest? What would you improve or leave out? Thanks :slight_smile: