GodotThings icon indicating copy to clipboard operation
GodotThings copied to clipboard

When player is in a float Vector2 position, they're much blurrier than the environment

Open estherflails opened this issue 10 months ago • 2 comments

Thank you so much for creating the Smooth Pixel Filter, it's been super useful! :) I'd like to ask for help with an issue I'm having. I'm working on a top-down game. I had to turn off "Snap 2D transforms to pixel" in the project settings, and this made it so whenever the player is not in an integer Vector2 position, their sprite would get very blurry compared to the environment. I tried to fix this by rounding their position when they stop moving, and despite this not seeming to actually alter their position it seemed to work. Except, I noticed the blurriness come back when the player collides with the edge of a capsule CollisionShape2D, and of course they're still a bit blurry when moving but that's usually less noticable. Does anyone know a solution for this? :/

Image

estherflails avatar Feb 15 '25 22:02 estherflails

I don't use Godot anymore and don't know too much about its 2D rendering, so take this with a grain of salt.

It looks like the player sprite is "correctly filtered" in the screenshot, its posotion seems to be ~half a pixel off on both axes. So this is about the worst case you can get (the pixel grid of the monitor and the sprite are fully out of phase). It's not as bad on high scaling factors but at the 3x scale it is noticeable.

I don't know enough about your setup (camera movement etc.) to say for sure, but in some way I don't think there is a complete solution to this problem. If you want smooth rendering without aliasing, filtering is the way to go but it can cause issues like you've described. You can snap sprites and the camera to the pixel grid, but this effectively "disables" filtering as if you'd use nearest-neighbor sampling and you're back to possibly jittery motion.

I think your approach to snap the player position when still is an interesting approach. No idea how collision affect your problem but my recommendation would be snapping the player's visual sprite only and keeping the logical sprite and collision shape untouched. Otherwise this could also introduce gameplay glitches.

Some things to keep in mind here:

  • You don't need to snap the sprites to pixel units of the game world. You can get finer precision if you snap them to the rendered pixels, though the math is slightly more involved.
  • Depending on how Godot anchors sprites, it's possible that rounding is half a pixel off with sprites that have an even/odd size.
  • You also need to take the camera position into consideration if it moves during gameplay.
  • Snapping can be done smoothly over time if it's to jarring (haven't fully thought this one through).

Hope that helps.

CptPotato avatar Feb 16 '25 10:02 CptPotato

Thanks for the response! :) I tried a variety of things to the point I ended up manually calculating the difference between the float position and what it would take to round it up to integer, and setting the player sprite global_position to be that. It does seem to work, as far as setting the position goes, but for some reason the blurriness still happens. So I guess it doesn't work after all? I'm at a loss to be honest. It prints an integer Vector2 and I know it actually affects the sprite position because I checked with bigger numbers, but stuff like this still appears... Starting to feel like my Godot is a little broken 😅

Image

In my project settings the window width override setting doubles the viewport size and the camera zoom is 1.5 on top of that. I also tried setting the viewport height to the window height I want with no override settings and just zooming in more. On float numbers like 2.5 it might even be better because everything is the same amount of blurry, but on whole numbers it's the same story. 😅 The sprite is 48x48 pixels and isn't half a pixel off in the editor thankfully.

I was planning to allow the player to zoom in and out like in Stardew Valley, but it's not looking good 😶 The blur isn't as bad as in the original image I posted, so I guess maybe I'll just have to live with it. But any other tips would still be appreciated!

Could you maybe tell me a bit more about how I should go about snapping to rendered pixels? I haven't been able to find anything about it.

estherflails avatar Feb 17 '25 20:02 estherflails