Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
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  
  Timing problems  (Read 780 times)
0 Members and 1 Guest are viewing this topic.
Offline Juriy

Junior Member





« Posted 2006-01-30 11:14:37 »

I'm new to java game development, and i've started from development of a simple tile-based game. Anyway... I've got a huge problem with timing, since System.currentTimeMills do not work as it supposed to. I've read the cokeandcode tutorials, but I don't want to use a JNI solution in my game.

I've read that "one way to deal with the inconsistency of timing on Windows is to average the change in time between frames". And I've done a separate timing class:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
public class LoopTimer {
   
   private long[] loopStatistics;
   private int loopsCollected = 0;
   
   private boolean inited = false;
   private long loopStart = 0;
   
   public LoopTimer(int numLoopsForStatistics){
      loopStatistics = new long[numLoopsForStatistics];
   }
   
   public LoopTimer(){
      this(5);
   }
   
   public long getLoopTime(){
     
      long loopTime = System.currentTimeMillis() - loopStart;
      if (loopTime == 0){ // Sometimes after the loop the time is still zero!!!
        return getAverage();
      }
         
      loopStart = System.currentTimeMillis();

      if (!inited){
         inited = true;
         return 1;
      }
     
      if (loopsCollected == 0){
         for (int i = 0; i < loopStatistics.length; i++)
            loopStatistics[i] = loopTime;
         loopsCollected++;
         return loopTime;
      }
     
      loopStatistics[loopsCollected - 1] = loopTime;
      loopsCollected++;
      if (loopsCollected >= loopStatistics.length)
         loopsCollected = 1;
     
      return getAverage();
     
   }
   
   private long getAverage(){
      long sum = 0;
      for (int i = 0; i < loopStatistics.length; i++)
         sum += loopStatistics[i];
     
      long avg = (long)sum/loopStatistics.length;
      if (avg <= 0)
         return 1;
      return avg;
   }
}


Then I've used it like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
      while(doGameLoop){
         currentFrames++;
         // Update FPS
        if (System.currentTimeMillis() - lastTime > 1000){
            totalFrames = currentFrames;
            currentFrames = 0;
            lastTime = System.currentTimeMillis();
         }

         g = (Graphics2D)strategy.getDrawGraphics();
         g.setColor(new Color(25, 25, 25));
         g.fillRect(0, 0, 600, 600);
         
         loopTime = loopTimer.getLoopTime();
         System.out.println(loopTime);
         gameWorld.update(loopTime);
         gameWorld.draw(g);
         
         g.setColor(new Color(230, 230, 230));
         g.drawString(String.valueOf(totalFrames) + " fps", 10, 10);
         
         g.dispose();
         strategy.show();
         
         try {
            Thread.sleep(5);
         } catch (InterruptedException e) {}


And when I've tried to run all this together i see that the mooving speed of the objects is not constant at all! Sometimes my starships start move faster (for about a second or so). Maybe there is another way to manage this probem?

http://voituk.kiev.ua - java tutorials, tips and tricks (Russian)
Offline tusaki

Junior Member


Medals: 1


In a mad world only the mad are sane.


« Reply #1 - Posted 2006-01-30 11:48:33 »

Yeah, there are two things you can do. You can, if you wish to use java 1.5, take a look at System.nanoTime().

The other thing is that for my own programs I've never bothered to create a seperate timing class.

Something like this always worked for me: (Assuming you have some kind of position and direction vector, where the direction-vector is the distance the object should cover in one second)
1  
2  
3  
4  
5  
6  
7  
8  
9  
    lastTime = currentTime;
    currentTime = System.currentTimeMilis();
    long deltaTime = currentTime - lastTime;
       
    float scaledX = (directionVector.x / 1000) * deltaTime;
    float scaledY = (directionVector.y / 1000) * deltaTime;

    position.x = position.x + scaledX;
    position.y = position.y + scaledY
Offline Jeff

JGO Coder




Got any cats?


« Reply #2 - Posted 2006-02-04 05:59:43 »

Note that your first statement is technically incorrect.

System.current Time Milis works *exactly* as designed and documented.

Resolution is not documented nor promised.  As others say, see the nanoTimer

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.

radar3301 (9 views)
2014-09-21 23:33:17

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (46 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24: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!