Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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 1783 times)
0 Members and 1 Guest are viewing this topic.
Offline bauerr

Junior Member




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


« Posted 2004-01-13 07: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 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 Kernel


Medals: 159
Projects: 23
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..

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!
Legends of Yore - The Casual Retro Roguelike
Offline crystalsquid

Junior Member




... 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:
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-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!

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

Junior Member




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 »

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.

 

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

The first screenshot will be displayed as a thumbnail.

TehJavaDev (13 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (31 views)
2014-08-16 06:45:27

Tekkerue (30 views)
2014-08-16 06:22:17

Tekkerue (19 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!