Game speed

Hello,

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.

Ralf

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

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…

pseudo:


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

        end = System.currentTimeMillis();

        Thread.sleep(25-(end-start));
    }

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:

http://java.dnsalias.com

Kev

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;
    Thread.Sleep(pause);
    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

But isn’t it nice that we’re now hearing the complaint “Java is too FAST” ? ? ;D

haha JAVA is “too fast” now? that’s news to me… and news to my 3-fps game as well.

[quote]haha JAVA is “too fast” now? that’s news to me… and news to my 3-fps game as well.
[/quote]
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.