Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
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  
  How to handle time in a very basic game  (Read 862 times)
0 Members and 1 Guest are viewing this topic.
Offline cmorris975

Senior Newbie





« Posted 2005-10-20 16:52:18 »


Ok, I'm 6 weeks into a Java I class so my knowledge is pretty limited at this point, but as I mess around with the Java that I do know it becomes clear that I need to somehow manage the passing of time in my little java games.  In order to keep my pentium 4 computer from racing through any program I write.  Can anyone give me a clue as to how I can start to learn how to do this?  I'm at the level where I'm making a box move across a playing field, I would like to slow down the redraws.

Thanks for any help!


Chris
Offline cborders

Junior Duke





« Reply #1 - Posted 2005-10-20 18:05:15 »

You could do a Thread.sleep(n) where n is the number of milliseconds you want the thread to wait before continuing.
Offline darkprophet

Senior Duke




Go Go Gadget Arms


« Reply #2 - Posted 2005-10-20 18:19:03 »

In an ideal world, you would measure how long 1 frame is and use the time difference from each frame as a fourth dimension in your equations. However, this isn't an ideal world, however, if you dont use physics too much (i.e. alot of equations that rely on time and whatnot) then you can use the above method. Otherwise, I would go with what cborders said and limit your FPS using sleep or yield to 60 and just assume that your FPS will always be 60.

The above 60fps situation is ideal for consoles because consoles slow down if they can't achieve 60fps and render every frame, this makes sure that the game stays on track, however, PC's dont do that and your going to have to assume a minimum system requirements...

Thats my take on things, use as necessary.

DP

Friends don't let friends make MMORPGs.

Blog | Volatile-Engine
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cmorris975

Senior Newbie





« Reply #3 - Posted 2005-10-20 19:55:23 »

Thanks for the replies guys Smiley  Will mess around with thread.sleep tonight....

Offline JAW

Senior Duke


Medals: 2



« Reply #4 - Posted 2005-10-22 09:55:27 »

For very basic things I used a fixed Thread.sleep(20) or such. The processing time is so small, like up to 5 ms, so that
a fixed sleeping time does it. I am speaking of things like Pong or Breakoid and very basic graphics quality.

The second level of timing is the synchronized framerate. You decide on a time per frame, something like 50 ms (20 FPS)
or better. 33 ms for 30 FPS, 20 ms for 50 FPS, etc. Then you take the time at the beginning and end of your game loop
and calculate the time passed, then you go sleep for the remaining time.

Say your game uses 7 ms and your desired time per frame is 20, you would sleep 13 ms. One important thing is, that you at least sleep a few millis, say a minimum of 3 to 5 ms. This gives other threads and garbage collection some time to act.
This method goes down when the pc is too slow and you dont get the frame down in the desired time. The game will then slow down.

The last option is dynamic framerate. At the beginnig of each frame, you take the time. So you can measure the time that passed since the last frame. Then you do all updating based on the passed time. You need to do a sleep of 5 ms or something fixed, and everything else goes as fast or slow as it goes.

The main difference is, with a fixed framerat you can use fixed speed. 5 pixels per frame movement with 30 FPS will always be 150 pixels movement per second. When using a dynamic framerate, you can have more or less FPS and have to do dynamic movement based on the time, so that you end up with 150 pixels per second. You must calculate something like movement per ms and multiply with the ms passed since last frame.


Important: On some Systems, especially Windows, System.currentTimeMillis() is not accurate. You should use System.nanoTime() to take you timing. nanoTime gives you ms * 1.000.000 so in order to get ms times, you need to divide (time2 - time) / 1.000.000.

-JAW
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.

Longarmx (49 views)
2014-10-17 03:59:02

Norakomi (38 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (34 views)
2014-10-15 16:18:58

TehJavaDev (65 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (54 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (43 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
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

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