1
\$\begingroup\$

I want to work on a model of some kind of simple shape shooter game I would develop for either the desktop or the Android, using the libGDX library. There's something I want to incorporate into the game, and that is a countdown timer with the smallest unit in centiseconds.

The trouble I'm having is which resource to use to incorporate a fully-accurate game timer where it is in sync with real time, meaning that every second on the game timer, when the game is running at full speed, is equal to one second in real time. (I should understand what is a high-resolution timer)

Java's Swing Timer class won't do, and so are the two methods from the static System class. And I'm not very confident about the timer classes I've looked up online, unless I accidentally ran into one of them and never realized it's completely accurate.

I have practiced implementing my own countdown timer except that it ain't synchronized because I'm doing an update for every 10 milliseconds. I asked a similar question on StackOverflow, but I am getting a little too obsessed in trying to find a perfectly good timer.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Throw your notion of synchronization with "real time" out the window. From a software perspective, the "real time clock" (better known as wall clock) is an awful awful thing. It is constantly drifting and many systems are configured to periodically adjust it by synchronizing with a time server. If you happen to compare two time values queried from the wall clock before and after this happens your game may think that time ran in reverse. System.nanoTime (...) offers no guarantee of monotonic behavior either, but it at least eliminates periodic ntp synchronization as one of the sources. \$\endgroup\$ Jan 16, 2014 at 0:50

1 Answer 1

1
\$\begingroup\$

System.nanoTime()

It's a high-resolution timer in nanoseconds. This is as good as it gets in Java. But since it is nanosecond precision, it should be more than enough for your needs.

Regarding your concern on sync with real time: Most games don't care about the real world time but are only interested in elapsed time intervals. However, one second delta time measured with System.nanoTime() is obviously equal to one second in real world.

\$\endgroup\$
5
  • \$\begingroup\$ Can you please explain to me the delta time? By the way, isn't there any third-party libraries that have an actual game countdown timer I can implement myself and result in a diminishing rate equal to the real world? Because I don't really believe in System.nanoTime() from what other people have said... :( \$\endgroup\$ Jan 15, 2014 at 23:10
  • \$\begingroup\$ Delta time is the difference in elapsed time. In your example (to achieve centiseconds) you could call System.nanoTime() in a loop and accumulate time differences until you get a centisecond and then update your game logic. But since you need only centiseconds precision, System.currentTimeMillis() should also be enough. What are your concerns regarding these methods? \$\endgroup\$
    – loodakrawa
    Jan 15, 2014 at 23:21
  • \$\begingroup\$ In an action method, like actionPerformed for a timer, it doesn't help keeping the elapsed time displayed on a JLabel consistent with how long it takes for one centisecond to elapse in both realtime. It may do the same with a drawn AWT string. Unless you are considering that I do not use an object that throws an action event, and instead use a thread. \$\endgroup\$ Jan 15, 2014 at 23:23
  • \$\begingroup\$ I really don't know what are you trying to achieve since you brought Swing into the story. I recommend you read about game loops here: gafferongames.com/game-physics/fix-your-timestep \$\endgroup\$
    – loodakrawa
    Jan 15, 2014 at 23:30
  • \$\begingroup\$ I read part of it and decided to implement the best algorithm with a few changes, and have now got a working timer. I think this is the best I got. Thanks! \$\endgroup\$ Jan 16, 2014 at 3:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .