Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
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 too fast  (Read 595 times)
0 Members and 1 Guest are viewing this topic.
Offline Kronos

Junior Member


Medals: 1



« Posted 2013-01-22 20:27:46 »

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 Cheesy

These are the deltas at that special moment:

0.15598080623923224
1.5791460031658402

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
   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. Sad
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #1 - Posted 2013-01-22 21:11:26 »

Ah, it's from "Killer Game Programming in Java". Smiley

Maybe I have something:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      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.
Offline GabrielBailey74
« Reply #2 - Posted 2013-01-22 21:48:25 »

If you're using nanoseconds you have to append:
1  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Kronos

Junior Member


Medals: 1



« Reply #3 - Posted 2013-01-22 21:58:45 »

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 Cheesy
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 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 Cheesy
Offline GabrielBailey74
« Reply #4 - Posted 2013-01-22 22:10:41 »

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

If I had a delta of 0.2 to 1.2 in my Game I'd be in trouble lol.
1  
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.

Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #5 - Posted 2013-01-22 22:14:57 »

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.
Offline Kronos

Junior Member


Medals: 1



« Reply #6 - Posted 2013-01-25 10:24:24 »

Hello, thanks for your answers.

Is this game Loop good for 4K Contest? What would you improve or leave out? Thanks Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (20 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!