Skip to main content
deleted 180 characters in body
Source Link
Zerro97
  • 53
  • 1
  • 2
  • 7

I'm quite new to java.

I was watching 2 video tutorials on how to make java game and read a couple articles about game loop. However, I'm still confused about how game loop actually works.

Out of these three sets of codes, which game loop would be the most optimal to use? I feel like the first set of code is too simple that it might not be as efficient as other two (not that it actually is, but just a feeling that I get).

I'm quite new to java.

I was watching 2 video tutorials on how to make java game and read a couple articles about game loop. However, I'm still confused about how game loop actually works.

Out of these three sets of codes, which game loop would be the most optimal to use? I feel like the first set of code is too simple that it might not be as efficient as other two (not that it actually is, but just a feeling that I get).

I was watching 2 video tutorials on how to make java game and read a couple articles about game loop. However, I'm still confused about how game loop actually works.

Out of these three sets of codes, which game loop would be the most optimal to use?

Tweeted twitter.com/StackGameDev/status/1019412863906140160
deleted 3 characters in body
Source Link
Zerro97
  • 53
  • 1
  • 2
  • 7
/* * Game Loop of the game. It uses variablefixed timestep. * Optimal time represents time needed to update one frame. * Update time represents time actually taken to update one frame. * * By calculating the difference between optimal and actual time, * we can let Thread to sleep for the exact time we are aiming for, which is 60 FPS. */ public void run() { long now; long updateTime; long wait; final int TARGET_FPS = 60; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; while (isRunning) { now = System.nanoTime(); update(); render(); updateTime = System.nanoTime() - now; wait = (OPTIMAL_TIME - updateTime) / 1000000; try { Thread.sleep(wait); } catch (Exception e) { e.printStackTrace(); } } } 
/* * Game Loop of the game. It uses variable timestep. * Optimal time represents time needed to update one frame. * Update time represents time actually taken to update one frame. * * By calculating the difference between optimal and actual time, * we can let Thread to sleep for the exact time we are aiming for, which is 60 FPS. */ public void run() { long now; long updateTime; long wait; final int TARGET_FPS = 60; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; while (isRunning) { now = System.nanoTime(); update(); render(); updateTime = System.nanoTime() - now; wait = (OPTIMAL_TIME - updateTime) / 1000000; try { Thread.sleep(wait); } catch (Exception e) { e.printStackTrace(); } } } 
/* * Game Loop of the game. It uses fixed timestep. * Optimal time represents time needed to update one frame. * Update time represents time actually taken to update one frame. * * By calculating the difference between optimal and actual time, * we can let Thread to sleep for the exact time we are aiming for, which is 60 FPS. */ public void run() { long now; long updateTime; long wait; final int TARGET_FPS = 60; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; while (isRunning) { now = System.nanoTime(); update(); render(); updateTime = System.nanoTime() - now; wait = (OPTIMAL_TIME - updateTime) / 1000000; try { Thread.sleep(wait); } catch (Exception e) { e.printStackTrace(); } } } 
Source Link
Zerro97
  • 53
  • 1
  • 2
  • 7

Java Game Loop Efficiency

I'm quite new to java.

I was watching 2 video tutorials on how to make java game and read a couple articles about game loop. However, I'm still confused about how game loop actually works.

The first tutorial used code that looked like this (I modified some codes from the tutorial and commented on top):

/* * Game Loop of the game. It uses variable timestep. * Optimal time represents time needed to update one frame. * Update time represents time actually taken to update one frame. * * By calculating the difference between optimal and actual time, * we can let Thread to sleep for the exact time we are aiming for, which is 60 FPS. */ public void run() { long now; long updateTime; long wait; final int TARGET_FPS = 60; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; while (isRunning) { now = System.nanoTime(); update(); render(); updateTime = System.nanoTime() - now; wait = (OPTIMAL_TIME - updateTime) / 1000000; try { Thread.sleep(wait); } catch (Exception e) { e.printStackTrace(); } } } 

This is quite easy to understand and I have no problem with using this game loop. However, the second tutorial used more complex game loop that I had a hard time understanding it:

public 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) { long now = System.nanoTime(); delta += (now - lastTime) / ns; lastTime = now; while (delta >= 1) { tick(); delta--; } if(running) render(); frames++; if(System.currentTimeMillis() - timer > 1000) { timer += 1000; System.out.println("FPS: " + frames); frames = 0; } } stop(); } 

I do not understand the use of timer and frames variable and how delta is being used. Is timer and frames simply used for printing out the FPS on the console screen? Also, help me clarify the use of delta variable in this code.

The use of delta variable is quite different from the other game loop that I know of. From the articles that I read, I've seen a different use of delta. Here is what I tried to come up with on my own after reading the articles:

/* * Delta is ratio of how fast computer is running. * It is achieved by calculating: * (actual time) / (optimal time) * * This is passed to update method to calculate time taken */ public void run() { long lastLoopTime = System.nanoTime(); final int TARGET_FPS = 60; final long OPTIMAL_TIME = 1000000000 / TARGET_FPS; while (isRunning) { long now = System.nanoTime(); long updateTime = now - lastLoopTime; lastLoopTime = now; double delta = updateTime / (double) OPTIMAL_TIME; update(delta); render(); } } 

Will the above code be fine as it is?

Out of these three sets of codes, which game loop would be the most optimal to use? I feel like the first set of code is too simple that it might not be as efficient as other two (not that it actually is, but just a feeling that I get).