public static CollisionState PerPixel(ISprite sprite1, ISprite sprite2, Rectangle rect, GraphicsDeviceManager graphics) { // sets the coordinates relative to (0,0) being the top left corner of this. Texture2D projTexture = sprite2.GetTexture(); Texture2D hitBoxTexture = sprite1.GetHitbox(); Texture2D vulnerableTexture = sprite1.GetVulnerable(); Color[] hitBoxPixels; Color[] vulnerablePixels; Color[] projectilePixels; CollisionState results = CollisionState.None; Rectangle objRect = rect; Rectangle projRect = new Rectangle(0, 0, rect.Width, rect.Height); //initial tests to see if the box is even applicable to the object texure being checked if (rect.X + rect.Width <= 0 || rect.Y + rect.Height <= 0) { return(CollisionState.None); } if (rect.X >= sprite1.GetSpriteWidth() || rect.Y >= sprite1.GetSpriteHeight()) { return(CollisionState.None); } if (rect.X < 0) { objRect.X = 0; objRect.Width += rect.X; projRect.X -= rect.X; projRect.Width += rect.X; } if (rect.Y < 0) { objRect.Height += rect.Y; objRect.Y = 0; projRect.Y -= rect.Y; projRect.Height = objRect.Height; } for (int i = 0; i <= objRect.Width; i++) { if (objRect.X + i == sprite1.GetSpriteWidth()) { objRect.Width = i; projRect.Width = objRect.Width; break; } } for (int i = 0; i <= objRect.Height; i++) { if (objRect.Y + i == sprite1.GetSpriteHeight()) { objRect.Height = i; projRect.Height = objRect.Height; break; } } if (objRect.Width == 0 || objRect.Height == 0) { return(0); } hitBoxPixels = new Color[objRect.Width * objRect.Height]; vulnerablePixels = new Color[objRect.Width * objRect.Height]; projectilePixels = new Color[objRect.Width * objRect.Height]; if (sprite2.GetMirrorX()) { projTexture = sprite2.MirrorTexture(sprite2, graphics, sprite2.GetTexture()); } if (sprite1.GetMirrorX()) { hitBoxTexture = sprite1.MirrorTexture(sprite1, graphics, sprite1.GetHitbox()); vulnerableTexture = sprite1.MirrorTexture(sprite1, graphics, sprite1.GetVulnerable()); } projTexture.GetData <Color>( 0, projRect, projectilePixels, 0, objRect.Width * objRect.Height ); hitBoxTexture.GetData <Color>( 0, objRect, hitBoxPixels, 0, objRect.Width * objRect.Height ); vulnerableTexture.GetData <Color>( 0, objRect, vulnerablePixels, 0, objRect.Width * objRect.Height ); for (int y = 0; y < objRect.Height; y++) { for (int x = 0; x < objRect.Width; x++) { Color colorA = hitBoxPixels[y * objRect.Width + x]; Color colorB = projectilePixels[y * objRect.Width + x]; Color colorC = vulnerablePixels[y * objRect.Width + x]; if (colorA.A != 0 && colorB.A != 0) { if (results == CollisionState.None) { results = CollisionState.Standard; } } if (colorC.A != 0 && colorB.A != 0) { results = CollisionState.Hurtbox; break; } } } return(results); }