Time for action – supporting collision detection
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 accounting for the padding values around the edges. There is already a BoundingBox
class as part of the XNA Framework, and while C# will not have a problem...