0

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.

Skami
  • 1,506
  • 1
  • 18
  • 29

1 Answers1

0

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.

DanMoran
  • 11
  • 1
  • I was reading from the wrong part of the spritesheet but i fixed it, now that im playing though i get a crash from this line of code: player.texture.GetData(0, src, personTextureData,0, 87*100); saying that An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.Xna.Framework.Graphics.dll Additional information: The rectangle is too large or too small for this resource. – Pontus Franzén Apr 23 '12 at 12:54
  • I fixed that error too, i was using frameSize * currentFrame and checked so it wasnt bigger than the texture.Width, the problem was that if frameSize * currentFrame was 500 and the picture was 512 in width in would try to read from 500 and then add the 100 in width so it got too 600 wich is more. – Pontus Franzén Apr 23 '12 at 13:33