Java game runs with different delays ( sleep ) on WinXP compared to Linux Ubuntu

Hello!

I have a game on a build, my game is action with smoke and fire -> http://temp4321.dy.fi/dist4/appletloader.php

Why my game runs slow on WinXP and OK with Ubuntu 9.04.
I have no knowledge about timers or any delays. i have added the following code to help understand my problem.


class coordinateThread extends Thread
    {
        public void run()
        {
            while ( true )
            {
                long endtime = System.nanoTime()+2000000;

                while ( endtime > System.nanoTime () )
                    try
                    {
                        Thread.sleep( 0, 200000 );
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }

This code is my only extra Thread on my game, it is build to make game coordinate changes smoother.
As this is only my second Java game, and first with LWJGL, i need this help.

With Ubuntu my game runs ok, but with my WINXP my game runs real slow even as the screen update is smooth ??
So, With XP my game runs far too slow, why is this i have no experience on timers or any delays ??

How to fix this problem, what is the best answer, is my approach to Thread.sleep() totally wrong ??

//----

Thanks,

I didnt even know that Thread.sleep() could ahve to numbers passed on to it…

what does it do having 2 numbers?

Warning - while you were typing a new reply has been posted. You may wish to review your post. 

;D

hi,
sleeping, waiting, yealding is platform dependent - precision may vary, and yes windows has lower timer precision than linux.

The easiest way to render with constant framerate is to enable vsync and render fast as possible (without sleep). The gpu driver will do the rest for you and limit the framerate to the screen refresh rate (e.g 60hz is very common for LCD screens).

gl.glEnableSwapInterval(1);

Note: user can overwrite this setting in the driver configuration

In LWJGL, it’s Display.setVSyncEnabled(true) to enable vsync.

Also, consider using the Display.sync() method to time the main loop. Or at least use Sys.getTime() and Sys.getTimerResolution() rather than System.nanoTime().

Cas :slight_smile:

Thread.sleep( 0, 200000 );

Hm. Well… look at the Sun implementation…

ms += (ns < 500000) ? 0 : 1;

So you’re basically calling Thread.sleep(0);

Hello!

I think i get it working with my Athlon on WinXP + Ubuntu9.04
it works now a real better, i removed the nanotime(), and installed import org.lwjgl.util.timer;
With LWJGL timer, my thread runs a lot better, i keep this timer for until i found even better.


//------------------------------------------------------------------------------
    class coordinateThread extends Thread
    {
        public void run()
        {
            while ( true )
            {
                timer.tick();
                float endtime = (float)(timer.getTime()+0.002f);

                while ( endtime > timer.getTime() )
                try
                    {                        
                        Thread.sleep( 0, 99999 );
                        timer.tick();
                    } catch (InterruptedException ex)
                    {
                        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                    }

//----

Thanks,

Hmm!

Should i change my sleep to Thread.sleep(1); ??
what is the best way to sleep a while on Java Thread ??


[quote]Also, consider using the Display.sync() method to time the main loop.
[/quote]
I have Display.sync (40); on my code.


Should i go counting timer ticks and multiply my sin + cos with timer values, to get real clock based moving.
I just dont really like, when game goes like a atomclock, smooth it have to be, still, atomclock is a bit too much.

( edit. )
I started to count my sin + cos for plane moves with LWJGL.TIMER, now my game is very very smooth.
Demo game here -> http://temp4321.dy.fi/dist4/appletloader.php

//----

Thanks,

I’m using thread.yield and then not processing anything that is 0ms. Only downside is that unless you’re using calculus for physics you’re going to be having slightly different accelerations(running really fast V. really slow) if you’re just using delta-time.


The fun thing with using thread.sleep on windows or pretty much anywhere is that even telling the thread to sleep for 30mins could instead get it to sleep for 0 seconds. While I don’t ever see this happening the documentation is almost worded to say “don’t be surprised or blame us if it does”.