Sprite gets slight distortion when colliding

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Goobis

My sprite has a strange affect on it when you continue to walk into a staticbody2d with a collisionshape on it. I’m not sure what the problem is.

A screenshot or gif would help.

Dlean Jeans | 2018-03-15 12:29

Gif of the problem

Screen capture - 339bb8a4caa7f534bf71b5ca7eb5e842 - Gyazo

Goobis | 2018-03-15 16:35

This is a result of how Godot upscales sprites in 2D. Even when everything is set to pixel perfect, it still shows distortions in the image.

I consider this a small problem, but it’s something that has the be fixed by Godot. There doesn’t seem to be a solution that can be applied within the engine without getting into the source code.

I got the same problem, but i ignore it for the time being.

KND2501 | 2018-03-15 19:21

Alright, I guess i’ll ignore it for now.

Thank you for the answer!

Goobis | 2018-03-15 20:11

Did you try turning continues collision on ?

Bartosz | 2018-03-16 00:02

If there is a fix, i’d like to know, if i got one i’ll let you know as well.

KND2501 | 2018-03-16 00:02

Goobis,… what is the size and test size you are using in the project settings?

KND2501 | 2018-03-21 22:35

:bust_in_silhouette: Reply From: Bartosz

Aligning sprites to pixel grid should prevent this from happening. You need to ensure that your sprites have round size inside scene e.g 16 is ok, but 15.99 is not. Each time they move you need to snap them to pixel grid (assuming camera if used is also aligned)
Here is an example how to do that: create new node as child of sprite and add this as its script

extends Node

# sprite to snap  defaults to parent of current node
export (NodePath) var sprite_node = "../";

var _sprite;
var _original_sprite_position;

func _ready():
	_sprite = get_node(sprite_node)
	_original_sprite_position = _sprite.position

func _process(delta):
	 # reset sprite to original location
	_sprite.position = _original_sprite_position
	
	# calculate snapped global position
	var original_global_position = _sprite.get_global_position()
	var aligned_global_sprite_position = Vector2((round(original_global_position.x)),(round(original_global_position.y)))
	
	# set aligned to position as current
	_sprite.set_global_position(aligned_global_sprite_position)

Update:
After further investigation, aligning sprites is not enough to get pixel perfect graphics best result will give using method propose here with Display->Stretch → Mode set to viewport and Display->Aspect set to keep.

At least for me, everything seems to be snapping in place, the problem is pixels being stretched and compressed depending on their place in the viewport.

So i am using 384 x 216, when i scale it to 1280x720 every pixel should should become a block of 3x3 pixels. But i see pixels shifting from 3 to 4 pixels in what appears to be columns and rows.

Check my image, the platform above should be a straight line, yet parts of it are sticking out. My character is centered in the screen, no matter what direction the sprite is facing, it’s right half always shows the thicker lines.

pixelperv hosted at ImgBB — ImgBB

KND2501 | 2018-03-17 22:50

If you are willing to create and share a minimal scene to reproduce problem I’ll try find out how to fix it

Bartosz | 2018-03-17 22:59

Whats a proper way to share projects? I can Zip it up, not sure where to post or how to send.

KND2501 | 2018-03-17 23:48

If you have github account put it there otherwise dropbox, google drive, onedrive etc. will also work

Bartosz | 2018-03-18 00:01

ok. I can reproduce your problem with the main character. I will look into it properly tomorrow

Bartosz | 2018-03-18 00:49

No problem, thanx, take your time bro.

KND2501 | 2018-03-18 00:56

Check my updated answer. Not totally bulletproof but should be sufficient for your usecase.

Bartosz | 2018-03-18 20:02

Very interesting, i can work with this.

So the basic idea is to have a regular node and to apply a script that controls the viewport?

And if like you have a game with various levels/stages you could instance the node into every other scene so the game just fits and looks good all over?

Again thanx mate, this is very useful.

KND2501 | 2018-03-19 11:04