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; }
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 with us declaring a parameter with the name of an existing class, we are appending Rect
to the end of the name here simply to avoid confusion.
When checking for a bounding box collision, the IsBoxColliding()
method accepts another bounding box and returns true if the two rectangles overlap at any point. The Rectangle class' Intersects()
method contains the logic to check for this overlap.
When performing bounding circle collisions, the IsCircleColliding()
method accepts a Vector2
representing the center of the object the sprite will be compared against, and the other object's radius. If the distance between the two centers is less than the sum of the radii of both objects, then the two circles overlap.
18.191.5.239