Time for action – supporting collision detection

  1. Add the properties and methods needed to support collision detection to the Sprite class:
    public Rectangle BoundingBoxRect
    {
        get 
        {
        return new Rectangle(
            (int)location.X + BoundingXPadding,
            (int)location.Y + BoundingYPadding,
            frameWidth - (BoundingXPadding * 2),
            frameHeight - (BoundingYPadding * 2));
        }
    }
    
    public bool IsBoxColliding(Rectangle OtherBox)
    {
        return BoundingBoxRect.Intersects(OtherBox);
    }
    
    public bool IsCircleColliding(Vector2 otherCenter, float otherRadius)
    {
        if (Vector2.Distance(Center, otherCenter) <
            (CollisionRadius + otherRadius))
            return true;
        else
            return false;
    }

What just happened?

The BoundingBoxRect property provides a Rectangle object equivalent to the location and size of the sprite ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.