MattM
MattM

Reputation: 1289

How to load sprite sheets that clamp to the sprite, excluding the background?

I'm having a lot of trouble googling for this because I don't really know the terminology for it. This HAS to be a common problem though. I want to iterate through a sprite sheet, but I want it to IGNORE the transparent background around the sprite and not include that as part of what's displayed.

For example, if I have a few frames, I want to iterate through each frame and load that image. Then, in that frame, I want to narrow it down by drawing a rectangle around the image itself, not the extra "background". This would probably be accomplished by finding the corners of the sprite which are non-transparent pixels (Not sure how this part works).

Does this make sense? Again, not sure exactly which words to use here...let me know if this is unclear.

The goal here is loading sprites that are exactly square with other frames, so they won't wobble or bounce unintentionally.

Thanks much!!

Upvotes: 1

Views: 502

Answers (1)

Mike B
Mike B

Reputation: 2672

I am working on my first game as well and I had a similar problem with the transparent areas around my sprites, in this case for collisions.

What I did was set it up so that each sprite has a position, a height, a width and Padding for X and Y.

Vector2 position = new Vector2(100,100);
int frameHeight = 48;
int frameWidth = 48;
int paddingX = 4;
int paddingY = 3;

With that info you can get what you need, for instance for the rectangle that represents the bounding box around the sprite I can use:

boundingRectangle = new Rectangle(
  (int)position.X + paddingX, 
  (int)position.Y + paddingY, 
  frameWidth - (paddingX * 2), 
  frameHeight - (paddingY * 2));

I read this in XNA 4.0 Game Development by Example by Kurt Jaegers (Which has been a ton of help for me)

Upvotes: 1

Related Questions