![]() |
|
VB.NET - Simple Collision Detection - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework) +--- Thread: VB.NET - Simple Collision Detection (/Thread-VB-NET-Simple-Collision-Detection) Pages:
1
2
|
VB.NET - Simple Collision Detection - #Swag_mybb_import6403 - 04-22-2012 If your new to game making and want to check for a collision I've made a small sub that will be perfect for you: Code: '::::::::::::::::::::Made by kaed @ swag.li & hackcommunity.com::::::::::::::::::::
'If your new to making games and want to check for a collision this is what you need.
'Example of use: simple_collision_detection(PictureBox1, PictureBox2)
Private Sub simple_collision_detection(ByVal control1 As Control, ByVal control2 As Control)
If control1.Left < control2.Left And control2.Left < control1.Right And control1.Top < control2.Top And control2.Top < control1.Bottom Or _
control1.Left < control2.Left And control2.Left < control1.Right And control2.Bottom < control1.Bottom And control1.Top < control2.Bottom Or _
control1.Left < control2.Right And control2.Right < control1.Right And control1.Top < control2.Top And control2.Top < control1.Bottom Or _
control1.Left < control2.Right And control2.Right < control1.Right And control2.Bottom < control1.Bottom And control1.Top < control2.Bottom Or _
control1.Left < control2.Left And control2.Left < control1.Right And control2.Top < control1.Top And control1.Bottom < control2.Bottom Or _
control2.Right < control1.Right And control1.Left < control2.Right And control2.Top < control1.Top And control1.Bottom < control2.Bottom Or _
control1.Top < control2.Top And control2.Top < control1.Bottom And control2.Left < control1.Left And control1.Right < control2.Right Or _
control2.Bottom < control1.Bottom And control1.Top < control2.Bottom And control2.Left < control1.Left And control1.Right < control2.Right Then
'code goes here!
MessageBox.Show("hit!")
End If
End SubTo check constantly for a collision I suggest you use this on a Timer_tick event. The syntax is: Code: simple_collision_detection(control1, control2)Here's a demonstration video of it: Share any opinions or questions you have please! RE: VB.NET - Simple Collision Detection - ArkPhaze - 04-22-2012 That whole thing could be simplified like so: Code: Dim r1 As New Rectangle(PictureBox1.Location, PictureBox1.Size)
Dim r2 As New Rectangle(PictureBox2.Location, PictureBox2.Size)
MsgBox(If(r1.IntersectsWith(r2), "Collision", "No Collision"))Or as a function: Code: Private Shared Function CollisionDetected(Control1 As Control, Control2 As Control) As Boolean
Dim r1 As New Rectangle(Control1.Location, Control1.Size)
Dim r2 As New Rectangle(Control2.Location, Control2.Size)
Return If(r1.IntersectsWith(r2), True, False)
End FunctionAnd it's usage as an example: Code: If CollisionDetected(PictureBox1, PictureBox2) Then MsgBox("Hit!")
RE: VB.NET - Simple Collision Detection - #Swag_mybb_import6403 - 04-22-2012 or you could do that. Would this same method still work if I made a circular control and used circles instead? RE: VB.NET - Simple Collision Detection - ArkPhaze - 04-22-2012 (04-22-2012, 01:03 PM)#swag Wrote: or you could do that. Would this same method still work if I made a circular control and used circles instead? It still has rectangular boundaries regardless of what control you make for the most part. You try to fit the rectangle as close as possible to fit the control so that it's not as obvious though. There are ways to make irregular shaped controls, but then you just define a polygon for that shape instead of a rectangular to check for intersections. RE: VB.NET - Simple Collision Detection - #Swag_mybb_import6403 - 04-23-2012 (04-22-2012, 08:15 PM)PhazeShift Wrote:(04-22-2012, 01:03 PM)#swag Wrote: or you could do that. Would this same method still work if I made a circular control and used circles instead? I know you can make irregular shaped controls as I've done that before, I just wasn't sure if you could use .intersectswith for other shapes besides rectangles. Thanks again. RE: VB.NET - Simple Collision Detection - invisal - 08-12-2013 Quote:I know you can make irregular shaped controls as I've done that before, I just wasn't sure No, you cannot use .IntersectsWith with other shape because it only take other Rectangle as an argument. For further reference check the following link: http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.intersectswith.aspx RE: VB.NET - Simple Collision Detection - invisal - 08-12-2013 Quote:I know you can make irregular shaped controls as I've done that before, I just wasn't sure No, you cannot use .IntersectsWith with other shape because it only take other Rectangle as an argument. For further reference check the following link: http://msdn.microsoft.com/en-us/library/system.drawing.rectangle.intersectswith.aspx RE: VB.NET - Simple Collision Detection - invisal - 08-12-2013 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 RE: VB.NET - Simple Collision Detection - invisal - 08-12-2013 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 RE: VB.NET - Simple Collision Detection - ArkPhaze - 08-12-2013 (08-12-2013, 06:03 AM)invisal Wrote:Quote:if I made a circular control and used circles instead? Not too bad, but why did you go with a Class instead of a Structure? |