Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Timing problems  (Read 481 times)
0 Members and 1 Guest are viewing this topic.
Offline Juriy

Jr. Member
**

Posts: 77



« on: 2006-01-30 06: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

Full Member
**

Posts: 112
Medals: 1


In a mad world only the mad are sane.


« Reply #1 on: 2006-01-30 06: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 Kernel
*****

Posts: 3535


Got any cats?


« Reply #2 on: 2006-02-04 00: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]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.115 seconds with 20 queries.