Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (580)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
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  (Read 2008 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Devvie

Java!!!!!!!!!!! !!!

« Posted 2004-01-13 07:35:59 »


I have the following problem: I wrote a little Applet-based game.
All object movings are computed by a special class running in its
own thread. This thread is decelerated with Thread.sleep().

The problem is: On my (old) computer the game is doing satisfactorily.
But on faster computers the game is too fast if it is started with the
settings from my computer.

Is there a possibility how the decelaration can be choosen depending on
the computers speed?

Thank you in advance.

Offline Herkules

Senior Devvie

Friendly fire isn't friendly!

« Reply #1 - Posted 2004-01-13 07:43:06 »

Just scale your motion offsets per update with System.currentTimeMillis() as a first guess.

HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline kevglass

« JGO Spiffy Duke »

Medals: 263
Projects: 25
Exp: 18 years

Coder, Trainee Pixel Artist, Game Reviewer

« Reply #2 - Posted 2004-01-13 07:44:41 »

Rather than detecting how fast the machine is, it might be easier to record how long the last "loop" took and subtract that away from a constant loop time..


    while (gameRunning) {
        start = System.currentTimeMillis();
        // do loop work here

        end = System.currentTimeMillis();


You should not however that sleep() and currentTimeMillis() doesn't have a very good resolution on Windows based machines and hence can cause you some problems. You might want to consider check out GAGE Timer at:


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

Junior Devvie

... Boing ...

« Reply #3 - Posted 2004-01-13 07:56:50 »

You need to do two things:
Firstly, regulate the sleep time by however long the CPU has taken for its processing. So for a really slow CPU, the sleep will be '0'. For a fast machine, it could be up to 40ish. You would also see this as the applet using less CPU time - 90%+ on a slow machine, and 20%+ on a fast machine.

e.g:  To stick to 20 fps:
    lastTime = currentTime;
    currentTime = System.currentTimeMillis();
    long pause = 50 - (currentTime - lastTime);
    if(pause < 0) pause = 0;
    currentTime = System.currentTimeMillis();

Secondly, you have to perform the logic updates when neccesary, i.e. when enough time has passed:

  // Define this somewhere else...
  static long elapsedTime;


   elapsedTime += currentTime - lastTime;
  while(elapsedTime > 50)
       // Do 50ms worth of updating
       elapsedTime -= 50;

These examples are for 20 frames per second update (20 fps = 50ms per frame, hence the '50's in the examples). Due to Windows 98 (& Windows Millenium) timers only having 50ms resolution, this is the best you can do on these systems. Windows 2000, XP, Macs or Linux have better timer resolutions so you can aim for higher framerates. Just adjust the ms times: for 30fps, use '33', for 50fps use '20', etc.

Hope this helps,

- Dom
Offline Jeff

JGO Coder

Got any cats?

« Reply #4 - Posted 2004-01-13 23:22:20 »

But isn't it nice that we're now hearing the complaint "Java is too FAST" ? ?  Grin

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Offline NexusOne

Junior Devvie

Java games rock!

« Reply #5 - Posted 2004-01-14 06:29:32 »

haha JAVA is "too fast" now? that's news to me.. and news to my 3-fps game as well.
Offline Jeff

JGO Coder

Got any cats?

« Reply #6 - Posted 2004-01-14 23:48:46 »

haha JAVA is "too fast" now? that's news to me.. and news to my 3-fps game as well.

There is no hammer so fine one cannot hit their thumb with it.

If you're really getting 3fps I'd suggest you start looking for sore appendages.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

Waterwolf (20 views)
2015-05-20 15:01:45

chrislo27 (25 views)
2015-05-20 03:42:21

BurntPizza (59 views)
2015-05-10 15:53:18

FrozenShade (45 views)
2015-05-07 09:11:21

TheLopais (208 views)
2015-05-06 13:36:48

TheLopais (192 views)
2015-05-06 13:35:14

TheLopais (198 views)
2015-05-06 13:33:39

TheLopais (214 views)
2015-05-06 13:32:48

TheLopais (214 views)
2015-05-06 13:31:28

ClaasJG (236 views)
2015-04-30 20:33:25
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 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‑
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!