Hello Guest

Author Topic: Help me understand why the pixels in my sprites are "shifting"?  (Read 7905 times)

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
This is probably a real noob question, but humor as we all have to learn sometime!

I have a sprite collection setup like so


Each sprite frame in the sprite sheet is 26x24, thought the sprite themselves are only 18x24.

However, I'm noticing when running the game, that certain pixels in the sprite seem to be shifting, or getting lost, and sections changing shape. Take a look at this example (taken from the game window in Unity), specifically the eyes


This is what it should look like (taken from my image editor)


I'm using a tk2dCamera with a native resolution of 1920x1080 with a wildcard override of pixel perfect, center.

I don't know if this is something specific to 2D Toolkit, maybe how it scales the sprites or something? Perhaps I have my sprites setup wrong? I really don't know why this is happening, so any insight would be helpful. Thanks!
« Last Edit: April 07, 2014, 12:02:25 am by Neeko »

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #1 on: April 07, 2014, 01:02:54 am »
I must of missed this post when I was first searching, but this sounds related to my issue as well.

http://2dtoolkit.com/forum/index.php?topic=1033.0

I think having my pixels per meter so high (I'm not even sure where 100 came from originally), I'm unable to set them to pixel perfect, instead the scale is set to 1, 1, 1, which may be scaling the sprite down.

Looks like I need to mess with my sprites size, scale and determine the native resolution I want to target, though there doesn't seem to be a hard rule on what's best.
« Last Edit: April 07, 2014, 01:07:03 am by Neeko »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #2 on: April 07, 2014, 06:11:48 pm »
100 is fine, and it should be pixel perfect when displayed with the camera set to 100 pixels per meter.. You should make sure the sprite position is always pixel perfect, and your sprite is snapped to a pixel position (i.e. the position is always a multiple of 0.01 if using 100 pixels per meter).

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #3 on: April 07, 2014, 08:43:35 pm »
You should make sure the sprite position is always pixel perfect, and your sprite is snapped to a pixel position (i.e. the position is always a multiple of 0.01 if using 100 pixels per meter).

So do you literally mean I should be modifying my gameobject's position like so, after movement translation?

Code: [Select]
// Psuedocode
gameObject.transform.position *= 0.01f

I fear this will have undesirable effects, specifically with collision. Or am I misunderstanding you?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #4 on: April 08, 2014, 05:47:57 am »
If you're using physics then yes you will have issues - the way to get pixel perfect results like that is to keep the sprite image separate, and mirror the position, snapping to the closest pixel. Most games ignore this though - it depends on how far you want to go with this.

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #5 on: April 08, 2014, 02:19:20 pm »
Okay thanks. I think I'm nitpicking at this point and the effect is so small and would be barely noticeable (if at all) during actual gameplay.

fsadeq

  • 2D Toolkit
  • Sr. Member
  • *
  • Posts: 353
    • View Profile
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #6 on: April 09, 2014, 03:06:46 pm »
FYI Niko, I'm in a similar situation. I just make sure the camera is properly snapped - couldn't care less about the objects themselves. Looks fine.

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #7 on: April 10, 2014, 01:58:34 pm »
Would you mind sharing how you snap the camera?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #8 on: April 11, 2014, 02:10:10 pm »
If your camera is 100 pixels per meter, you want to make sure the camera position is a multiple of 0.01. Basically do something like this

position.x = Mathf.Round(position.x * pixelsPerMeter) / pixelsPerMeter;
and likewise for y and z.

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Re: Help me understand why the pixels in my sprites are "shifting"?
« Reply #9 on: April 15, 2014, 12:58:18 pm »
Ah okay, I wasn't dividing. That certainly snaps the camera into place, but I was still seeing the same behavior with my original sprite. However, I've actually completely replaced the sprite in the original post with a different design, and I haven't noticed any pixel distortion with the new sprites.

Thanks again!