Rendering Loop Stuttering On Random App Instances

I’m using a fixed timestep in my rendering loop to interpolate the render positions and maintain a steady framerate of 60fps and update interval of 30tps.

I have a test animation that moves a single image across the screen in a linear path, checking for a minimum timestep on each update.

The odd thing is, the animation is smooth on a majority of the times I launch the program, but sometimes it begins to stutter (i.e. the image “shakes” while moving or jumps ahead/behind in its position a little bit).

What I don’t understand, is that the frame-rate monitor has the frame rate and update rate staying steady even while the image is stuttering during the animation.

What could be causing this? And why is it so inconsistent between application instances?

If you need any code snippets, let me know.

Thanks in advance for your input!

You can’t have a “fixed variable” timestep. It’s one or the other. :point:

Of course we need code snippets. We can’t read your mind. :persecutioncomplex:

How much does it jump/stutter? Could be a rounding error (or lack thereof) when drawing or faulty interpolation.

That or it could just be Swing, if you’re using that.

My mistake. That was a stupid miswording. It’s a fixed timestep (I’ll fix that in the question).

There’s a lot of different code snippets, so I figured we should start with speculation and theory and then I can get you any specific sections of code you need (i.e. rendering logic, timestep loop, etc.)

I’m using Math.round when applying the interpolation to rendering positions. That should work fine right?

I am using Swing. The goal of the engine was to use pure Java2D. Surely it can work…

Could be Swing, the code, system clock or a number of things.

A notorious cause can be the inaccurate windows clock which you can force to use a more accurate one by making an infinite daemon loop. The “windows daemon fix” which some call it.

Game loops have been discussed extensively and the slight stutter you mention isn’t unheard of:

Game loops:

Stutter discussion:

I am actually already using that infinite daemon timer fix. It does seem to work quite nicely for the Windows timer.

After switching the buffer and display BufferedImages to native image type (Graphics.createNativeImage I think is the method), and adding the -Dsun.java2d.opengl=true VM argument, the stuttering seems to be mostly gone on Windows. It does still occur on Linux, however. That actually surprises me quite a bit… I would’ve expected Linux to perform better.

That may have something to do with my Graphics driver on Linux. Not sure. Are there any known issues with Java2D on Linux?

Unfortunately Java2D just can’t be relied upon. Your issues sound like they may be unique to your system or drivers, but that’s just the problem – you’re always going to have machines that have issues. This is complete guesswork, but in my experience around 10% of people have problems of one kind or anything playing games I have made in Java2D. It’s also worth mentioning that Applets are for the most part broken now because of all the security scares (Apple no longer has Java enabled without fiddling, for example), so there is little advantage to using Java2D. You might as well touch the hardware if you have a downloaded app.

I was able to bring the stuttering down to a reasonable minimum on Linux, and nearly eliminate it on Windows.

I find that playing with various things such as BufferStrategy buffer count, render ops, and max-updates in the fixed timestep can make a difference depending on the platform. That seems reasonable. It isn’t uncommon that you have to adjust video settings in a game to get optimal results.

Setting the JVM flag -Dsun.java2d.opengl=true REALLY helps. I highly recommend that to anyone having slow rendering/stuttering problems in Java2D.