Jittery camera follow

maybe this isn't the right way to do this, but I'm trying to get the 'camera' to follow my character. while it seems to work, I am seeing it jitter a pixel or so frequently.

basic idea here is that hero is a sprite, each frame I'm telling it to move a little bit. meanwhile, I keep a cameraPos that is moving in the opposite direction, which gets passed to setDrawOffset.

hero:moveBy(moveX, moveY)
cameraPosX -= moveX;
cameraPosY -= moveY;
playdate.graphics.setDrawOffset(cameraPosX, cameraPosY)

is there a more reliable way to do this? if I switch the order of this it doesn't make any difference. i.e. if I call setDrawOffset first then move the hero.

in unity what I would do here is have the camera follow the target in a late update, so I would think moving the hero first then adjusting the offset ought to be enough to take care of things but maybe not. does moving the sprite not happening instantly? like is it on some redraw delay or something?

Just a guess: do your coordinates ever have a fractional component? You might try rounding them using math.floor() or similar before using them in any drawing functions.

TIP: If this fixes the problem (admittedly a long-shot), you can improve performance by defining local mathfloor <const> = math.floor at the top of your source file, and call that local reference rather than doing a look-up on math.floor every time.

2 Likes

thanks for the reply, I didn't come back to this immediately because I switched to working on a different prototype instead :slight_smile:

I went back and tried this out, and flooring my moveX and moveY did seem to fix up the jitter I was getting!

1 Like