public bool PerPixelCollision(LandEnemy eggpawn)
{
// get their sprite based on their current frame in their current animation
var sonicsCurrentSprite = GetSprite();
var eggpawnsCurrentSprite = eggpawn.GetSprite();
// their rectangle in world space whose size should be the same as their current sprite's size
var sonicsRectangle = GetRectangle();
var eggpawnsRectangle = GetRectangle();
// no intersection, return false
if (!sonicsRectangle.Intersects(eggpawnsRectangle))
{
return false;
}
// get the new rectangle that contains overlapping region of two other rectangles.
var intersectedRectangle = Rectangle.Intersect(sonicsRectangle, eggpawnsRectangle);
for(int y = intersectedRectangle.Top; y <= intersecretRectangle.Bottom; y++)
{
for(int x = intersectedRectangle.Left; x <= intersecretRectangle.Right; x++)
{
// do some math to get the local coordinates of x,y in their rectangle (the method below doesnt exist, you can create one as an extension method for the Rectangle class or put it in a static class and call it RectangleUtility)
Vector2 sonic = sonicRectangle.GetLocalPosition(x, y);
Vector2 enemy = eggpawnsRectangle.GetLocalPosition(x, y);
if (sonicsCurrentSprite.HasPixel(sonic) && eggpawnsCurrentSprite.HasPixel(enemy))
{
return true;
}
}
}
return false;
}