Time for action – supporting collision detection
Add the following properties and methods needed to support collision detection to the
Sprite
class:Public ReadOnly Property BoundingBoxRect As Rectangle Get return new Rectangle( CInt(_location.X) + BoundingXPadding, CInt(_location.Y) + BoundingYPadding, frameWidth - (BoundingXPadding * 2), frameHeight - (BoundingYPadding * 2)) End Get End Property Public Function IsBoxColliding(OtherBox as Rectangle) As Boolean return BoundingBoxRect.Intersects(OtherBox) End Function Public Function IsCircleColliding( otherCenter As Vector2, otherRadius As Single) As Boolean If (Vector2.Distance(Center, otherCenter) < (CollisionRadius + otherRadius)) Then return true Else return false End If End Function
What just happened?
The BoundingBoxRect
property provides a Rectangle
object equivalent to the location and size of the sprite, accounting for the padding...