Making a game run the same speed on different computers

View: New views
5 Messages — Rating Filter:   Alert me  

Making a game run the same speed on different computers

by java3d-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi

I've made a few simple web applet games in Java(some 2D, some 3D using Java3D) and I would like to make them run at the same speed(a fixed frame rate?) on different computers(i.e. with varying hardware performance). I was just wondering: is there a built-in way to do that in Java?
[Message sent by forum member 'jon987' (jon987)]

http://forums.java.net/jive/thread.jspa?messageID=279115

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Making a game run the same speed on different computers

by java3d-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You can set max FPS on Java3D by setting  minimun frame cycle time, like below example  :

[code]
// my  maximun desired  FPS
float targetFPS = 30.0f;
// calculate  time
long cicleTime = (long)(1000.0f / targetFPS) ;
//apply it
canvas3D.getView().setMinimumFrameCycleTime(cicleTime);
[/code]



On Java2D this is more complex. You must calculate your sleep time in a way to complement it to fit in your frame cycle .

frameCycleTime = timeToRender + sleeptime;

Take care that WinXP has latency ~15ms, so don't trust blindly on System.currentTimeMilllis() and Thread.sleep(long) on values lower than 30.

Some projects change sleeptime time each 5 or 10 frames.
[Message sent by forum member 'zesharp' (zesharp)]

http://forums.java.net/jive/thread.jspa?messageID=279130

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Making a game run the same speed on different computers

by java3d-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

you need to create a Thread from which you call your update function. This threads need to be setup with a WakeupCondition trigger = new WakeupOnElapsedTime(XX), where XX is the time in milisec. However, this still does not guarantee that it always looks the same speed, as creating a frame is still depending on the PC performance. And if a computer is to slow to be within the given XX time, it will be slower.
[Message sent by forum member 'oliverotto' (oliverotto)]

http://forums.java.net/jive/thread.jspa?messageID=279132

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Making a game run the same speed on different computers

by java3d-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You should decouple the logic from the rendering frame rate. Use System.nanoTime() to measure the duration of the last frame. Use that to update the logic the next frame. You can cap the frame rate by looping with a Thread.yield(). Or just render flat out with no breaks. In Java3D you would use A WakeupOnElapsedFrames(0) to render flat out.
[Message sent by forum member 'lamer77' (lamer77)]

http://forums.java.net/jive/thread.jspa?messageID=279149

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Making a game run the same speed on different computers

by java3d-interest :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

OK thanks guys - I've managed to implement a method, based on your advice, that works :)
[Message sent by forum member 'jon987' (jon987)]

http://forums.java.net/jive/thread.jspa?messageID=296451

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...

LightInTheBox - Buy quality products at wholesale price!