0
votes

I'm using a spritesheet animation in XNA and I want to use pixel perfect collision. My problem is that the pixel perfect collisions checks if the current pixel on the spritesheet is transparent instead of checking against the part of the spritesheet i'm using. This is my code:

blockTextureData = new Color[shot.texture.Width * shot.texture.Height];
shot.texture.GetData(blockTextureData);

personTextureData = new Color[player2.texture.Width * player2.texture.Height];

player2.texture.GetData(personTextureData);

if (IntersectPixels(player2.CollissionBox, personTextureData, shot.CollissionBox, blockTextureData))

I'd like to know how to choose a specific part of the spritesheet and check collision against it. I used this MSDN guide.

EDIT: I managed to figure it out, i used this instead

src = new Rectangle(frame, 0, 87, 100);

player.texture.GetData<Color>(0, src, personTextureData,0, 87*100);

and frame is were you start it, e.g 0 for the first and 87 for the second since i use a spritesheet with 2 pictures wich is 2*87 in width and 100 in height.

Edit2: The problem now is that once you hit the pixelperfect will stop working, so it only works for 1 hit.

1

1 Answers

0
votes

If you could elaborate on what you mean when you say it only works once that would help.

I had to tweak that same IntersectsPixels function quite a bit to work with my project. One thing I found helpful was using a very slow animation of a square (say 100x100) filled with pure red, then completely transparent in it's only other frame.

It helped a lot with debugging issues related to pixel comparisons on collision and animated sprites.

See if you can provide more information on exactly what isn't working and how it's breaking down. Best of luck solving your issues.