Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Game loop not accurate. How can I make a better one ?  (Read 2451 times)
0 Members and 1 Guest are viewing this topic.
Offline RKJ

Senior Newbie





« Posted 2015-03-28 15:08:58 »

This game loop that I have implemented is not constant. I want my program to run at 60 frames per second. Basically I need to make the Thread sleep for 17 milliseconds after each frame, but sometimes it sleeps more that 17 milliseconds, this makes my game jittery. What should I do to make a more efficient game loop?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class GamePanel extends JPanel implements Runnable {

   
   @Override
   public void run() {
      currentTime = System.currentTimeMillis();
      prevTime = currentTime;
      while(true) {
         repaint();
         upd();
         try {
            Thread.sleep(17);
         } catch(InterruptedException e) {
            e.printStackTrace();
         }
         prevTime = currentTime;
         currentTime = System.currentTimeMillis();
         delta = currentTime - prevTime;
         System.out.println(delta);
      }
   }
}
Offline Elsealabs
« Reply #1 - Posted 2015-03-28 15:10:13 »

Check out this guide! http://www.java-gaming.org/index.php?topic=24220.0  Smiley

Offline skys0ldier

Senior Newbie


Exp: 3 years



« Reply #2 - Posted 2015-03-28 15:48:16 »

actually it is okay, but you have to factor the delta value in your update() method.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline skys0ldier

Senior Newbie


Exp: 3 years



« Reply #3 - Posted 2015-03-28 15:50:06 »

just divide delta through 17 and you have your update factor.
For example:

player speed is 10. if delta is 17 (optimum) you calc:

player.position += 10 * 17 / 17

so plus ten. if your game sleeps longer for example delta = 34,
delta / 17 is 2 and your player would move twice as fast in this frame.
Offline teletubo
Global Moderator

JGO Wizard


Medals: 75
Projects: 4
Exp: 8 years



« Reply #4 - Posted 2015-03-28 23:18:48 »

actually it is okay, but you have to factor the delta value in your update() method.

Actually it is not ok.

When it says
Thread.sleep(17)

you are assuming you render takes 0ms - which is most probably not correct.

The sleep should be 17 - time to render. So you should tke note on how long it takes to render stuff and reduce that from the sleep time afterwards.

Offline lcass
« Reply #5 - Posted 2015-03-28 23:37:01 »

That just makes it more complicated.
You already have your delta calculated , just check to see if the delta >= 60/1000000000 , this is if you are using nano_time(); this will render at 60 - 58 FPS just modify the value at will.
Offline basil_

« JGO Bitwise Duke »


Medals: 418
Exp: 13 years



« Reply #6 - Posted 2015-03-29 00:47:03 »

a good and solid timer would be nice to have .. but it's not gonna happen Sad ..

using
Thread.sleep()
is ok, using
Thread.yield()
is not that good .. but helps with "jerky" loops.

whatever you do .. try using
Thread.speep(1)
and then try to find a threshold when to use
Thread.yield()
to skip the last tiny bit of time to keep the loop stable.

afaik that's the only way to get it to do someting useful.
Offline jonjava
« Reply #7 - Posted 2015-03-29 10:29:45 »

You can take a look at what LWJGL uses: https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/opengl/Sync.java

Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1314 views)
2018-06-10 19:43:48

EgonOlsen (1179 views)
2018-06-10 19:43:44

EgonOlsen (916 views)
2018-06-10 19:43:20

DesertCoockie (1317 views)
2018-05-13 18:23:11

nelsongames (1106 views)
2018-04-24 18:15:36

nelsongames (1338 views)
2018-04-24 18:14:32

ivj94 (2080 views)
2018-03-24 14:47:39

ivj94 (1231 views)
2018-03-24 14:46:31

ivj94 (2230 views)
2018-03-24 14:43:53

Solater (798 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!