HaxeFlixel / flixel

Free, cross-platform 2D game engine powered by Haxe and OpenFL
https://haxeflixel.com/
MIT License
1.92k stars 427 forks source link

FlxSprite twitching when a pixel-perfect camera follows a FlxSprite #3195

Closed ChillyCider closed 1 week ago

ChillyCider commented 1 week ago

Code snippet reproducing the issue:

A full demonstration is viewable here (and its source code).


Observed behavior: When the player walks around, their avatar sometimes twitches 1 pixel to the left of the Camera's center.

Expected behavior: The camera should follow the player sprite without deviation.

The demo contains a workaround via the "Floored Cam" button, which switches to a slightly modified camera. I'm not confident in the mathematical correctness of this solution, so I would appreciate other eyes on the code. My camera changes are in FlooredCam.hx.

If the solution looks good, I'd be happy to submit a PR for FlxCamera itself.

ChillyCider commented 1 week ago

I did some more testing in a different project, and unfortunately I still see some extremely rare twitching on the player sprite. So it isn't foolproof, probably not mathematically correct.

I'm in a game jam, but I'll keep you posted if I find another workaround.

ChillyCider commented 1 week ago

Setting the camera's targetOffset to (0.5, 0.5) fixed what I was seeing. As far as I can tell, this is not an engine issue, it's just an issue with having a pixelPerfectRender camera's target be on a pixel boundary of the sprite.