Hello Guest

Author Topic: Dimensions of a tk2dsprite  (Read 6309 times)

fluffipup

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 1
    • View Profile
Dimensions of a tk2dsprite
« on: August 17, 2013, 07:04:28 pm »
Hello

I am having some trouble with the correct methods to set the width/height of a sprite. The issue boils down to the fact the "scale" of the sprite is not related to the pixel dimensions (where 1 meter = 1 pixel).

Im not sure if this means my tk2dcamera is setup incorrectly or not?

But Im getting the bounds of an object and trying to resize a sprite to the bounds. So I am setting the "scale" of the tk2dsprite but the problem stems from the fact the sprite width and height is "420x100" and yet setting the sprite scale to 1:1 means the sprite is 320x320.

So, the two dont translate well. But I assumed pixel perfect (pixels-per-meter = 1) would ensure the sprite itself would be 420x100 in scale...? Or am I wrong..

Thanks
« Last Edit: August 17, 2013, 07:07:15 pm by fluffipup »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Dimensions of a tk2dsprite
« Reply #1 on: August 17, 2013, 08:04:36 pm »
The scale isn't related to dimensions at all. Its meant to be 1, 1 at unless you want it to be bigger or smaller than its native size. If you create your camera at 1 pixel per meter, you'd probably want to set up your sprite collections at the same size. If your sprites and camera are set up with the same settings, you'll find that the scale will be 1,1,1 for it to be pixel perfect.

A scale of 320x320 on it suggests your sprite collection is set up with different parameters, likely 640 height and 1 ortho size. Are you using one of the demo sprites?

If you want the sprite to fit a particular "size" at 1 pixel per meter regardless of configuration, you can use something similar to this:

Vector2 yourSizeInPixels;
var nativeSize = sprite.CurrentSprite.GetUntrimmedBounds().size;
sprite.scale = new Vector2( yourSizeInPixels.x / nativeSize.x, yourSizeInPixels.y / nativeSize.y, 1);

This will get you the scale you need to apply to resize it to fit the required bounds.