Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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  
  Applet, Mouse IO, and Performance  (Read 939 times)
0 Members and 1 Guest are viewing this topic.
Offline gholker

Junior Newbie





« Posted 2007-05-08 16:03:26 »

Hi, thanks for reading,

I am doing some basic 'research' and i've written an Applet that animates a ball and moves a paddle using MouseMotionListener. The Applet implements Runnable, i got this idea from a tutorial somewhere, i say this because i'm not entirely sure how that's working. I know it fixed the problem i had where [i guess] the Applet Thread hogged the CPU and the IO was never processed.

My problem is that there are blips in performance. I can see the blips visually as well in the output on the console. I say output the currentTime - endOfLastIterationOfLoop, that's probably confusing so let me give it to you in pseudo code:

time1 = System.currentTimeMillis ();  // for first iteration.
while (true) {
     
        ball.animate ();

        
       time2 = System.currentTimeMillis();
         System.out.println (time2-time1);
         time1 = System.currentTimeMillis();
           
        repaint();
}

so the output is something like
...
...
...
0
0
0
0
0
0
15
0
...
...
...

I tried using Thread.sleep(), inserting a 10ms delay, which results in:

16
0
31
16
15
16

Mostly it's 16s and 15s but that occasional 31 bothers me. I suppose this is Garbage Collection?
Actually those zeros bother me too. Is this a matter of System.out.println (timeDiff) rounding the long value to zero? Or is this value actually zero? If the Thread is supposed to sleep why doesn't it? I understand it goes long because the timer on XP is ~15ms. And the 31ms delay seems to be ~double the 15 or 16.

I suppose i should do a test myself, but is this behaviour similar in an Application / WebStart ?
Would something like http://javolution.org/help?

And what about how this problem scales? If/When i have a large number of objects to animate and more IO to accept and more game logic to compute, what happens then? To me it seems it would get worse.

EDIT: thanks again!
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2007-05-08 16:35:29 »

Looks like an issue with the accuracy of currentTimeMillis(), which unfortunately isn't 1 millisecond. You might want to try use GAGE timer or some other alternative to check.

Kev

Offline gholker

Junior Newbie





« Reply #2 - Posted 2007-05-08 20:46:24 »

am i asking for too much accuracy or is it reasonable to expect a higher frame rate?
Do the frameworks / engines for java games all fix this problem?
Thanks for pointing me to GAGE Timer, but ideally i'd like to have a pure Java solution, if possible.
I notice Jake2 (the Quake2 port in java) says it's pure Java how could they manage to pull it off?
Is there a way of checking User IO on your own terms instead of AWT based? (Although Kill Game Programming in Java recommends AWT - as far as i've read)
If i'm asking riduculous questions just let me know... or just don't respond i guess...

actually i'm wondering if Jake2 takes the hit of a 15ms timer. It has a max 66 FPS, which is good, but those little blips in performance are annoying, so could that be fixed with better garbage collection handling? I guess i may try that route though my programd doesn't seem to be doing enough to require serious GC.
I'll read more KGPiJ

EDIT: am i taking the wrong angle on this? i suppose using native binaries becomes necessary for OGL so maybe i should just learn native binary linking and get on with it?
          I noticed something in a Java3D tutorial or something or other that mentioned a 'wakeuponawtevent' method.. thought that might help..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #3 - Posted 2007-05-11 13:29:20 »

Quote
It has a max 66 FPS, which is good, but those little blips in performance are annoying, so could that be fixed with better garbage collection handling?

Without knowing what's causing those performance blips, it's impossible to tell if GC is to blame or not. Most likely however, GC isn't the cause unless you're doing huge numbers of allocations each frame but I guess for just an animated ball and paddle that's not the case.
Try the -verbose:gc switch

I'm thinking it's probably caused by coarse timing. You could try to measure timing across multiple frames (10 frames for example) instead of each frame and mediate the results.

I'm not sure how Jake2 is doing it's timing, but I think it's open source, right?

Another thing you might want to try is to use the (mediated) timing results to cap the frame rate to 60fps or something (using Thread.sleep()), it might lead to more consistent frame rates.

In any case, profile to be sure what's going on!

Offline cylab

JGO Ninja


Medals: 38



« Reply #4 - Posted 2007-05-11 14:35:21 »

am i asking for too much accuracy or is it reasonable to expect a higher frame rate?
Do the frameworks / engines for java games all fix this problem?

I think you misunderstood the accuracy statement. It did not neccessarily reference to your performance problems, but to the fact, that you measurement is flawed, since the System.currentTimeMillies() just don't return milliseconds, but "jumps" around in chunks of ~15+ milliseconds depending on your system.

To measure times, the easiest would be to make your game require a JDK > 1.5 and use System.nanoTime()

Regarding your performance problem, it is hard to tell without seeing your actual rendering code. There are a lot of possibilities to cause this "blips":

- You are using the inaccurate timer to calculate you ball-movement

  conclusion:  you don't have a peformance problem at all, just your math is influenced by the
  currentTimeMillies shortcumming

  solution: use System.nanoTime() or meassure the time over a larger duration (e.g. 10frames)
  and divide the result as needed

- You are updating your coordinates only in the mouse listener callback

  conclusion: the mouse listener is called an an irregular basis, so you migt get to many
  or to few events for a smooth animation

  solution: try to smooth the mouse-movement by calculating averages for mouse movement
  direction and speed based on the mouse listener callbacks and animate you movements in a
  constant render loop based on this values

- You are repainting the panel inside the mouse listener callbacks

  conclusion:  depending on your rendering speed, this might block enqueue mouse events,
  so you get jumpy time frames between the mouse listener calls.

  solution: do the rendering in a dedicated rendering loop by painting on a BufferedImage
  and just blit the finished "scene" to the panel in an SwingUtilities.invokeLater() call. The last step
  could possibly be circumvented by (in/re)-validate/repaint your panel, but I am not so familiar with
  realtime animations in Swing/AWT.

Maybe there are other possible problems, too


Mathias - I Know What [you] Did Last Summer!
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.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!