Slick Jittery Movement

Posted this on Slick Forum too but its just so mind boggling


public final class LolGame extends BasicGame { 
     float xPos = 100.0f; 
     float yPos = 100.0f; 
     float xChange = 4.0f; 
     float yChange = 5.0f; 
     
     public LolGame() { 
          super("Jitter in da house"); 
     } 
     
    @Override 
     public void init(GameContainer gc) throws SlickException { 
          gc.setForceExit(true); 
         gc.setShowFPS(true); 
         gc.setClearEachFrame(true); 
         gc.setTargetFrameRate(60); 
         gc.setVSync(true); 
     } 
     
     public static void main(String[] args) throws SlickException { 
         AppGameContainer app = new AppGameContainer( new LolGame() ); 
         app.setDisplayMode(800, 600, false); 
         app.start(); 
         System.exit(0); 
     } 

     @Override 
     public void update(GameContainer gc, int delta) throws SlickException { } 
     
     @Override 
     public void render(GameContainer gc, Graphics g) throws SlickException { 
        g.setColor(Color.white); 
        g.fillOval(xPos, yPos, 20, 20); 
       xPos+=xChange; 
        yPos+=yChange; 
        if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange; 
        if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange; 
    } 
 }

this will jitter… why ?

its probably because you’re updating your logic in the render method and not controlling it via the delta value, not doing this will make movement dependant on the fps thus jittery as the fps moves up or down.

What you should try is remove the following from the render method

xPos+=xChange;
yPos+=yChange;
if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange; 

and put the following in the update method

xPos += xChange * delta;
yPos += yChange * delta;

if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange; 

You’ll probably also need to lower the xCharge and yCharge values to slow down the speed to what you want as its now multiplied by delta.

ok now I know this is how you’re supposed to do it, but how could you ever predict what delta would be

when lowering changeX I would have to know

Also, I just tried it
using * delta
and using setsmoothupdate(true) and * delta

both times I print out the delta each frame to see it.

without smoothupdate they range from 15 to sometimes 35
with it, it seems stead at 16

and here is the kicker: it doesn’t matter, both stil jitter


float xChange = 0.4f;
float yChange = 0.5f;
...
gc.setSmoothDeltas(true); // or not, doesn't matter actually
...
public void update(GameContainer gc, int delta) throws SlickException {
    	xPos+=xChange * delta;
    	yPos+=yChange * delta;
    	if (xPos <= 0 || xPos >= gc.getWidth())  xChange = -xChange;
    	if (yPos <= 0 || yPos >= gc.getHeight()) yChange = -yChange;
    }

still jitters D=

And whats strange is that, i wanted to make a small new game, and got this behavior. However my main game doesn’t have that problem.
My main game also only uses the render and not the update, because I knew of the delta, but I ported my whole game from Java2D, and obviously changing all the values in the game to fit delta would be outrageous.
And my game works fine. Even if I copy this ball bouncing code into my game, it doesn’t jitter.
I think its because there is so much going on and in this empty example not, so that fps changes are greater here.

You don’t, the whole point is that its suppose to be variable so that it adjusts your logic to move at a constant pace.

try without the lines gc.setTargetFrameRate(60); and gc.setVSync(true); to see if you can get rid of the jitter, should help narrow down where the issue is.

You can also try setting gc.setMaximumLogicUpdateInterval(50) or even 30 to avoid any big jumps, so that the delta doesn’t go too big thus avoid jumping.

Sounds like you were doing it wrong with Java2D too, being frame dependant on one system might work fine, but results would probably be different on another computer.

Seems to have no jitter now, with 4000+ frames
but a soon as I enable vsync or sync to framerate, it jitters again

using gc.setMaximumLogicUpdateInterval(30); its better but still jitters here and there

I mean this is as basic as it gets, it should be straightforward to get it right

You’re right, you cannot know the delta beforehand. But you probably know how fast you want your objects to move, and from there you can say something like: “I know how far I want my ball to move in 1 second”. Lets say you want to move 5 pixels in 1 second (1000 ms). You take 5/1000, and then you know your multiplicator. In this instance with 5 pixels pr second, it would be x += 0,005 * delta

meaning that if I have value of change now, I do this value / 16.666 * delta so get the result I want
1000ms / 60 fps = 16.666
and it does work, obviously
still some jitter… but like I thought the busier a game gets, the more this problem vanishes.

I never thought in movement per time, but per frame, and since I force 60 fps, it seems fixed.
god I hate working with different hardware / PCs sometimes, consoles don’t have this problem D=