Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Rendered Image "Chugging" While Moving  (Read 2144 times)
0 Members and 1 Guest are viewing this topic.
Offline ghostsoldier23

Junior Member


Medals: 1



« Posted 2012-11-25 18:08:42 »

The Java2D engine I'm building uses a render loop that iterates over a Set of Renderable objects.  It calls render(RenderControl) on each Renderable object.  It's then up to each object to call render(int,int,int,int,int[]) so that the image data is added into the main BufferedImage's raster.

In a test object that just renders an image on screen and increments the position based on how much time has passed (to animate it), I have this code:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      @Override
      public void render(RenderControl rc) {

         if (System.currentTimeMillis() - last >= 20) {
            x+=4;
            y+=4;
            last = System.currentTimeMillis();
         }
         
         rc.render(x, y, img.getWidth(), img.getHeight(), data);
      }


Where img is a BufferedImage representing the image loaded previously using ImageIO.read.

When this runs, however, the moving image kind of chugs in it's movement.  It freezes or relapses in its path around every second, even though the frame rate is consistently 55-60fps (capped using Thread.sleep).

Is there something wrong with the way I'm doing this?  Should the position changing code be executed in a separate thread?

Let me know if you need any more information.  Many thanks in advance!

Let me know if you need any more information,

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-11-25 18:11:29 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
      @Override
      public void render(RenderControl rc) {

         if (System.currentTimeMillis() - last >= 20) {
            x+=4;
            y+=4;
-            last = System.currentTimeMillis();
+            last += 20; // not leaking time anymore
        }
         
         rc.render(x, y, img.getWidth(), img.getHeight(), data);
      }


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline deepthought
« Reply #2 - Posted 2012-11-25 18:24:52 »

I've had a similar problem. could you please explain how he was leaking time?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #3 - Posted 2012-11-25 18:26:36 »

What do you mean by leaking time?

I will try this as soon as Eclipse finishes installing a plugin....
Offline ctomni231

JGO Wizard


Medals: 99
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #4 - Posted 2012-11-25 18:33:53 »

I'll take a shot at answering this.

Calling System.currentTimeMillis() every time you want to want to update the last function causes leaks for two reasons.

1) When you call it in the "if" clause, it'll get a time let's say (10 millisecs). Then, when you call it again at "last" it'll then read (13 millisecs). You have just lost a few milliseconds of time because of the small difference of the 2 calls.

2) System.currentTimeMillis() is not an 100% reliable call and suffers from OS slowdowns. If you call the function and your program stutters at the sleep() function, the time will be way off.

By using "last -= 20", it makes sure that even if the time stutters, you will not be losing any frames of time in the game. It also guarantees that there is only one time call per update cycle, and that prevents time leaks entirely. 

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2012-11-25 18:34:08 »

What if the amount of time between now and last is something like 65ms. You'd lose 45ms in your update logic, causing a slowdown in the animation you're rendering.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #6 - Posted 2012-11-25 18:42:25 »

I added the last+=20 and the stuttering still occurs in the animation.  Granted it looks somewhat different than before, but nonetheless still occurs.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #7 - Posted 2012-11-25 18:58:22 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
      @Override
      public void render(RenderControl rc) {
         
         final int INTERVAL = 20;

         long mark, diff;
         if ((diff = (mark=System.currentTimeMillis()) - last) >= INTERVAL) {
            x+=5;
            y+=5;
            last = mark;
            last -= (diff - INTERVAL);
         }
         
         rc.render(x, y, img.getWidth(), img.getHeight(), data);
      }


This seems to work a little better but there's still some stuttering.
Offline Regenuluz
« Reply #8 - Posted 2012-11-26 13:11:41 »

Well, you shouldn't update position in the render method, that belongs in some update() method that handles logic. Render should, well, only handle rendering.
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #9 - Posted 2012-12-10 03:05:23 »

I am now using a fixed timestep render/update method modeled after the example in this thread: http://www.java-gaming.org/index.php?topic=24220.0.

The image is still chugging (when there are multiple images) even though the FPS and TPS are staying at a constant 60 and 30 (respectively).

How can this be possible?  If everything is occurring on a fixed timestep, and the rate isn't changing, why should there be chugging?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Best Username Ever

Junior Member





« Reply #10 - Posted 2012-12-10 03:17:55 »

Can you define "chugging"?
Offline ghostsoldier23

Junior Member


Medals: 1



« Reply #11 - Posted 2012-12-10 03:19:27 »

Can you define "chugging"?

Right sorry.  Image stuttering... so when the image is moving, it occasionally shakes or moves back on it's position.  Basically a lack of position update constancy.
Offline Best Username Ever

Junior Member





« Reply #12 - Posted 2012-12-10 04:22:07 »

If it's moving backwards or out of place, that's a serious bug. If it's stutters or doesn't move at a constant rate it's because you're not interpolating between updates.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #13 - Posted 2012-12-10 04:54:38 »

I would simply blame Java2D. It's actually normal for it to stutter. Even on my powerful computer a simple square stutters every so often while it moves across the screen.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 798
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #14 - Posted 2012-12-10 05:01:29 »

Like BUE said, it makes no sense to blame Java2D if the image moves backwards.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #15 - Posted 2012-12-10 05:07:03 »

Jumping and moving backwards ... you don't have multiple threads updating positions, do you?  Might help to pastebin a complete example that reproduces the problem.
Offline StumpyStrust
« Reply #16 - Posted 2012-12-10 07:29:51 »

How slow are you trying to move? If it is anything smaller then 1, then you will get stuttering.

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.

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

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

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

Tekkerue (39 views)
2014-09-09 02:24:56

mitcheeb (58 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (35 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19

mitcheeb (40 views)
2014-09-04 23:08:59
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!