Rendered Image "Chugging" While Moving

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:


		@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,

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

What do you mean by leaking time?

I will try this as soon as Eclipse finishes installing a plugin…

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.

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.

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


		@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.

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.

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?

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.

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.

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.

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

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.

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