RE: VB.NET - Simple Collision Detection 08-12-2013, 06:03 AM
#9
Quote:if I made a circular control and used circles instead?
I have made a simple custom shape class which can check the intersection
between Rectangle and Circle. I will take a small set of code from my code
where it detects rectangle and circle collision
Code:
Public Function IntersectWith(ByVal circle As VisalCircle) As Boolean
Dim dist As New Point(Math.Abs(circle.Mid.X - Me.Mid.X), _
Math.Abs(circle.Mid.Y - Me.Mid.Y))
'' If circle and rectangle are too far away from each other
'' which it is impossible for them to collide.
If dist.X > Me.HalfLength.Width + circle.Radius OrElse _
dist.Y > Me.HalfLength.Height + circle.Radius Then
Return False
End If
'' The circle mid point is inside the rectangle.
If dist.X < Me.HalfLength.Width OrElse _
dist.Y < Me.HalfLength.Height Then
Return True
End If
'' If all the above fail, it means that the circle is
'' near the corner of the rectangle.
Return (dist.X - Me.HalfLength.Width) ^ 2 + _
(dist.Y - Me.HalfLength.Height) ^ 2 _
<= circle.Radius ^ 2
End FunctionThe following is the full source code with interactive interface. You can drag
the circle and box around with your mouse. When they collide, all the border
will turn red.
http://pastebin.com/RdseyG80



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)