Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1402 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Member




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


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

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
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #1 - Posted 2004-01-13 08: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
« League of Dukes »

JGO Kernel


Medals: 54
Projects: 20


Mentally unstable, best avoided.


« Reply #2 - Posted 2004-01-13 08: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..

pseudo:

1  
2  
3  
4  
5  
6  
7  
8  
9  
    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

Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline crystalsquid

Junior Member




... Boing ...


« Reply #3 - Posted 2004-01-13 08: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:
1  
2  
3  
4  
5  
6  
    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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
  // 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-14 00: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!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline NexusOne

Junior Member




Java games rock!


« Reply #5 - Posted 2004-01-14 07: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-15 00:48:46 »

Quote
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!

http://wiki.java.net/bin/view/Games/JeffFAQ
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (134 views)
2013-05-17 21:29:12

alaslipknot (143 views)
2013-05-16 21:24:48

gouessej (172 views)
2013-05-16 00:53:38

gouessej (167 views)
2013-05-16 00:17:58

theagentd (176 views)
2013-05-15 15:01:13

theagentd (161 views)
2013-05-15 15:00:54

StreetDoggy (204 views)
2013-05-14 15:56:26

kutucuk (228 views)
2013-05-12 17:10:36

kutucuk (228 views)
2013-05-12 15:36:09

UnluckyDevil (231 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.115 seconds with 20 queries.