Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (744)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (825)
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  
  Having trouble with delta.  (Read 2438 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK

Junior Devvie





« Posted 2012-10-06 04:45:23 »

So until recently, my getDelta() method has worked perfectly (in other projects).

This is my getDelta() method:
1  
2  
3  
4  
5  
6  
7  
public int getDelta() {
      long time = getTime();
      int delta = (int) (time - lastFrame);
      lastFrame = time;
     
      return delta;
   }

and I am calling this in a while loop that is synced to 60fps via Display.sync(60);

When I call System.out.println(delta); it does not return 16 or higher like it should.
Here is an example output.
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  
128
0
0
0
0
0
0
128
0
0
0
0
0
0
0
128
0
0
0
0
0
0
0
128
0
0
0
0
0
0
128


Has anybody experienced this and can give me a solution to get delta working properly?
Offline ReBirth
« Reply #1 - Posted 2012-10-06 04:47:02 »

At least the numbers has pattern. Where you call the println()?

Offline QuicK

Junior Devvie





« Reply #2 - Posted 2012-10-06 04:51:53 »

I am calling System.out.println(delta); right after I call getDelta();

Here is the code for my loop.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
private void startLoop() {
      while (!Display.isCloseRequested()) {
         int delta = getDelta();
         System.out.println(delta);
         renderGL();
         checkInput();
         Display.update();
         Display.sync(60);
      }
      flushSystem(false);
   }


P.S. - Thank you for the quick response Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline QuicK

Junior Devvie





« Reply #3 - Posted 2012-10-06 09:12:03 »

Maybe somebody could give me a working example and I can go from there?

This is the initialization code for my Display and some GL initialization:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private void initDisplay() throws LWJGLException {
      width = 800;
      height = 600;

      Display.setDisplayMode(new DisplayMode(width, height));
      Display.setVSyncEnabled(true);
      Display.setTitle("Game Engine: " + VERSION);
      Display.create();

      glEnable(GL_TEXTURE_2D);
      glShadeModel(GL_SMOOTH);
      glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
      glClearDepth(1.0f);
      glEnable(GL_DEPTH_TEST);
      glDepthFunc(GL_LEQUAL);

   }


Maybe I'm doing something in here that is wrong?

**On a side-note, could somebody explain what glDepthFunc(GL_LEQUAL) does?
Offline philfrei
« Reply #4 - Posted 2012-10-09 10:16:38 »

Hi -

I noticed your plaint about having other questions without many answers, and found this thread that looks like it could use some attention. (Or maybe you've already found the answer?)

Unfortunately I don't know anything about the gl code and can't help you with your side question.

About the delta question:

What is the getTime() function using as it's means of determining time? Do you know if it uses System.currentTimeMillis()? or System.nanoTime()?

The former can lead to weirdness as it's accuracy depends upon the OS, and on a lot of Microsoft Systems this can mean a 15 or 16 msec granularity. The latter is much better for accuracy, though to get an answer in the proper range, you would need 9 digits. The (int) cast shouldn't be a problem but the delta is getting close to where it go out of range. I think an int should be able to handle deltas of 2 seconds.

Curious about the 128 consistency. This occurs whether it takes 7 or 8 intervening 0's.

128 / 7 = 18.4+
128 / 8 = 16

Suspiciously close to the clock time granularity with currentTimeMillis(), though I can't quite get my head around a scenario that would cause the problem.

Hmmm....

Where is "lastframe" declared? Is it in the same class? In the same thread? Is there anything being done with it outside of this loop? Is it being altered by the Display functions?

I should learn some LWJGL.

music and music apps: http://adonax.com
Offline QuicK

Junior Devvie





« Reply #5 - Posted 2012-10-09 10:36:51 »

First of all, thank you for taking the time to look over this and try to help me Smiley

Second...
What is the getTime() function using as it's means of determining time? Do you know if it uses System.currentTimeMillis()? or System.nanoTime()?

Sorry, I forgot to include my getTime() method which is the following:

1  
2  
3  
public long getTime() { 
      return (Sys.getTime() * 1000) / Sys.getTimerResolution();
   }


I can't find whether Sys.getTime() uses System.currentTimeMillis() or System.nanoTime() or maybe it's own methods?

Where is "lastframe" declared? Is it in the same class? In the same thread? Is there anything being done with it outside of this loop? Is it being altered by the Display functions?

"lastframe" is initialized as an instance variable in the Game class (which contains my game loop and there is only one instance of this running in the program).
the only modifier I have for "lastframe" is in the Game.getDelta() class:
1  
2  
3  
4  
5  
6  
7  
public int getDelta() {
      long time = getTime();
      int delta = (int) (time - lastframe);
      lastframe = time;
     
      return delta;
   }

These are the only two spots lastframe is even used in my entire program.

Quote
I should learn some LWJGL.

I agree! It was pretty easy to learn the basics, and I've been progressing pretty steadily even in 3d rendering.
(It helps that http://www.youtube.com/user/TheCodingUniverse made a great tutorial series on it)
It's also the only java game library I've learned/used. I'm considering giving JMonkeyEngine a go as well!
Offline Varkas
« Reply #6 - Posted 2012-10-09 10:41:01 »

Maybe you should use one of the mentioned timers, currentTimeMillies() or nanoTime()
I've been using the former with some success to calculate deltas, although as others mentioned, the resolution of the timer is a bit low, and might be insufficient for constant and smooth 60 FPS.

Maybe you can try one of them and see if they work better than the timer that you are using?

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline QuicK

Junior Devvie





« Reply #7 - Posted 2012-10-09 10:45:31 »

Maybe you can try one of them and see if they work better than the timer that you are using?

I'll give it a try! I don't think the deficiencies in the resolution would cause stuttering as long as vsync is enabled...but for now I need to sleep haha. I've been up all night implementing an L-system and trying (but failing) to implement ground made from midpoint displacement :/

The only thing I don't get, is I literally copied a project that has a working delta, changed the name, ran it, and the delta was not working properly. (WTF java...)
Offline Varkas
« Reply #8 - Posted 2012-10-09 10:52:44 »

With a fresh head next day, some problems will almost solve by itself Wink

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
Offline QuicK

Junior Devvie





« Reply #9 - Posted 2012-10-09 10:59:44 »

With a fresh head next day, some problems will almost solve by itself Wink

Tongue however true that may be, this has been a problem for almost a week now...It's got to be just one little thing I'm doing in all of my programs. but I don't know what it could be, because the code seems very linear for getDelta();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #10 - Posted 2012-10-09 17:50:04 »

Thanks for the tutorial link. I plan to check it out.

I do recommend tryout out System.nanoTime() in place of System.getTime(). (And not System.currentTimeMillis().)

If you are using Eclipse, and have source code for LWJGL, you can use the F3 key to check out the source code for the function. Might be interesting to look at this, just for grins.

The fact that the number of 0's is 7 or 8 and the two numbers bound the sync rate (1000 / 60 = 16.667) seems like a big clue. This sort of bracketing happens when you count the number of times a repeating value occurs within another interval.

7 * 16.7 = 116.9
8 * 16.7 = 133.6

These two numbers bound 128. So this implies that the getTime() function may have a granularity of 128 millis, which is ridiculously high.

Is it possible getTime() is progressing in bumps of 128 msec? If so, why would this function work in one context and not another?

music and music apps: http://adonax.com
Offline QuicK

Junior Devvie





« Reply #11 - Posted 2012-10-11 03:37:28 »

Thanks to everyone who replied, I now have a working delta (I used System.nanoTime())!!

Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (150 views)
2017-09-30 02:57:34

theagentd (225 views)
2017-09-26 18:23:31

cybrmynd (303 views)
2017-08-02 12:28:51

cybrmynd (291 views)
2017-08-02 12:19:43

cybrmynd (299 views)
2017-08-02 12:18:09

Sralse (292 views)
2017-07-25 17:13:48

Archive (980 views)
2017-04-27 17:45:51

buddyBro (1105 views)
2017-04-05 03:38:00

CopyableCougar4 (1685 views)
2017-03-24 15:39:42

theagentd (1433 views)
2017-03-24 15:32:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!