Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 speed keeps changing  (Read 2634 times)
0 Members and 1 Guest are viewing this topic.
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Posted 2011-12-04 06:40:45 »

Hi, I have a game loop like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
while(true)
        {
            start = System.nanoTime();

            draw();
            if(inGame)update();
            while(System.nanoTime() - start <  31250000)
            {
            Thread.yield();
            }


Its a very simple loop for a lil application. However, when running it in both the IDE and as an executable jar, in runs at different speeds. Sometimes it runs normally, but often it'll run slower than before. The speed dosnt change mid way - it's either slow from start to end, or normal from start to end. Its a very simple program so it isnt just my CPU not being able to handle it. Is this a common problem, a fault with my loop or just my PC?
Offline Mads

JGO Ninja


Medals: 26
Projects: 3
Exp: 6 years


One for all!


« Reply #1 - Posted 2011-12-04 06:58:57 »

Try using Thread.sleep, instead.

Offline theagentd
« Reply #2 - Posted 2011-12-04 08:37:18 »

Could also be a timing accuracy problem. Windows has a very inaccurate global timer, but a more accurate timer will be used when certain functions are called. Other running programs can have this better timer activated, which would affect your program. However, the main problem is that your program suffers so much from the bad timer accuracy, but on to that later.

To force the program to constantly run the high accuracy timer, create a thread and have it sleep forever in a loop at the start of your program.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
    public static void startSleepFixThread(){
        new Thread("Sleep fix thread") {
           {
                setDaemon(true);
           }
            public void run() {
                try {
                    while(true){
                        Thread.sleep(Long.MAX_VALUE);
                    }
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }.start();
    }


Concerning the other problem (your program), it can be solved very easily. The problem is your start = System.nanoTime() line. Change your loop to this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
        long start = System.nanoTime();
        while(true){
            draw();
            if(inGame)update();

            long end = System.nanoTime();
            if(end - start <  31250000){
                try{
                    Thread.sleep((31250000 + start-end) / 1000000); //Sleep time in milliseconds
               catch(InterruptedException ex){
                    ex.printStackTrace();
                }

            start += 31250000; //No error build-up!
       }


And finally, why are you aiming for 32 FPS? Isn't 60 or 50 more common and better looking?

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

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #3 - Posted 2011-12-04 13:02:50 »

The thread hack on Windows is only valid when using the system thread scheduler AFAIK - the nanotime timer is a different one. Thread.yield() I think is largely unaffected by the hack too - it fixes Thread.sleep().

Cas Smiley

Offline theagentd
« Reply #4 - Posted 2011-12-04 13:31:48 »

The thread hack on Windows is only valid when using the system thread scheduler AFAIK - the nanotime timer is a different one. Thread.yield() I think is largely unaffected by the hack too - it fixes Thread.sleep().

Cas Smiley
Hmm... Then why does he get so weird performance? Let's see what OP says when he gets back...

Myomyomyo.
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #5 - Posted 2011-12-04 18:00:33 »

Hey guys thanks for the replies

I tried the loop you proposed, not sure what to say...

I ran in 15 times. 3 of which it ran at the normal 'intended' speed, 11 times it ran at the slower speed, again the same as seen before with the last loop. 1 time it ran at an insanely fast speed, everything was whizzing across the screen, havnt seen that before ^^. How the hell can the same code run at such varying speeds?

I'm starting to wonder if it's something I've done wrong with my setup. If I change the time for it to wait, to say, 10000 or even 0, the majority of times I run it it STILL goes at this slow speed, only occasionally going at the new speed.

Quote
class main extends JFrame implements  KeyListener
Quote
public main()
    {
        super("Name");
        this.setSize(1130,700);
        this.setResizable(false);
        this.setIgnoreRepaint(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        this.addKeyListener(this);
        createBufferStrategy(2);
        bs = getBufferStrategy();

There's nothing stupid here is there?

No reason in particular for the 32fps. Its got little that moves and is slow moving at that. Is just a demo for some concept.
Offline ra4king

JGO Kernel


Medals: 336
Projects: 2
Exp: 5 years


I'm the King!


« Reply #6 - Posted 2011-12-04 18:13:16 »

Here's my pretty gameloop: http://www.java-gaming.org/topics/solving-stuttering-with-fixed-timesteps-once-and-for-all/24975/msg/213649/view.html#msg213649

Offline counterp

Senior Member


Medals: 11



« Reply #7 - Posted 2011-12-04 21:30:09 »

And what exactly goes on in the draw and update methods?
Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #8 - Posted 2011-12-04 23:28:03 »

The draw method:

Graphics2D g2 = (Graphics2D)bs.getDrawGraphics();
Then I use this to draw the GUI, and draw all the NPC sprites. its pretty much
for(int i = 0; i < number; i++)
npc.draw(g2);

basically.

The update method is just the same, but the update makes a decision for the npc (just random number chooses action) then sets its velocity, or if a move is chosen it just moves the NPC around the screen.

It's only simple logic going on in the update, I can't think of anything that could do this. Nothing special at all.
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #9 - Posted 2011-12-04 23:36:10 »

It's worth noting that the timer used by System.nanoTime(), on certain motherboards, CPUs and OS combinations, can randomly change speed depending on CPU power settings. That's why we don't use it ourselves and rely on the slightly lower-resolution timer from LWJGL.

Cas Smiley

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

Senior Member


Medals: 7
Exp: 1 year



« Reply #10 - Posted 2011-12-05 02:07:41 »

I changed my game loop to

while(true)
{

draw
update
}

No timers at all, and it still happens (hahaha). Sometimes insanely fast, sometimes slow. It turns out it is my draw method causing it, so sorry for wasting peoples time. Ive no idea what in there is doing it though, about to find out I hope.
Offline delt0r

JGO Knight


Medals: 26
Exp: 18 years


Computers can do that?


« Reply #11 - Posted 2011-12-05 11:09:02 »

Quote
t's worth noting that the timer used by System.nanoTime(), on certain motherboards, CPUs and OS combinations, can randomly change speed depending on CPU power settings. That's why we don't use it ourselves and rely on the slightly lower-resolution timer from LWJGL.
Interesting. That indicates that some implementations uses the clock cycle counter (x86 still have em?). May need to stop with the habit of using nanoTime().

What we all have to struggle with is that modern computers are not real time systems. But are/should be fast enough to fake it.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #12 - Posted 2011-12-05 11:55:22 »

I read some article a few years ago by a professional game developer (ho ho Wink) about how it's basically all a bit screwy, and you need to use 3 different clocks (the CPU clock, multimedia clock, and the system time) and use them to agree on actual time elapsed since last time using various heuristics and dead reckoning. It was all a bit hairy. We've found that the multimedia clock on Windows is pretty much 100% accurate to 1ms, and the system time on Linux and MacOS is 100% accurate to 1ms as well. The nanotimer has varying degrees of strangeness.

Cas Smiley

Offline theagentd
« Reply #13 - Posted 2011-12-05 15:47:00 »

I changed my game loop to

while(true)
{

draw
update
}

No timers at all, and it still happens (hahaha). Sometimes insanely fast, sometimes slow. It turns out it is my draw method causing it, so sorry for wasting peoples time. Ive no idea what in there is doing it though, about to find out I hope.
Maybe you're getting OpenGL acceleration randomly? I had so many problems with it that I'm just sticking to pure OpenGL.

Myomyomyo.
Online Roquen
« Reply #14 - Posted 2011-12-05 15:51:40 »

I haven't paid attention recently, but yeah, alot of speedstep machines (what, 10 yrs ago) didn't account for changing of CPU speed for power/heat consumption and the counter reflected 'actual' CPU clockticks.  Couple that with multicore machines where each physical CPU has it's own counter = fun!
Offline sproingie

JGO Kernel


Medals: 201



« Reply #15 - Posted 2011-12-05 19:21:19 »

On some architectures and OS's, Java's scheduler will actually ignore an excessive number of Thread.yield calls called within a short time of each other.  Looking at it a little deeper, it looks to just be Solaris that's special-cased that way in the jvm code, but the OS itself may also have different ideas as to how to treat threads causing scheduler churn.  Yield is really just a suggestion -- if you really want a sleep, use sleep.


Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #16 - Posted 2011-12-08 18:37:12 »

theagentd I think you must be right, because it was very rare, but perfect when whatever it was occured. The regular slowness was being caused by a scaling operation on an image every draw call

 Roll Eyes
Offline theagentd
« Reply #17 - Posted 2011-12-08 19:30:53 »

The OpenGL acceleration is so unreliable that it's better to explicitly disable it and focus on making your game work with without it.

Myomyomyo.
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.

pw (24 views)
2014-07-24 01:59:36

Riven (24 views)
2014-07-23 21:16:32

Riven (18 views)
2014-07-23 21:07:15

Riven (21 views)
2014-07-23 20:56:16

ctomni231 (50 views)
2014-07-18 06:55:21

Zero Volt (45 views)
2014-07-17 23:47:54

danieldean (36 views)
2014-07-17 23:41:23

MustardPeter (39 views)
2014-07-16 23:30:00

Cero (54 views)
2014-07-16 00:42:17

Riven (55 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!