Public Group

# Collision detection in VB.Net

This topic is 4930 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Im making a space invaders type game in vb.net 2003. Ive got the aliens moving, i can move the ship and fire the lasers. All i need is help with collision detection between the lasers fired by the player and the alien ship.

##### Share on other sites
There are many ways of testing whether objects have collided or will collide with each other. One easy way is to think of the objects as rectangles with a top and left edge coordinate and a bottom and right edge coordinate. Let's call the objects (lasers in your case) A and B. Then test if A's right edge is to the right of B's left edge and A's left edge is to the left of B's right edge. If this condition is true then test if A's bottom edge is below B's top edge and A's top edge is above B's bottom edge. If this condition is true as well it means rectangle A and B are intersecting somewhere.

Here's a function for it:

Public Function SimpleCollision(ALeft As Integer, ATop As Integer, ARight As Integer, ABottom As Integer, _BLeft As Integer, BTop As Integer, BRight As Integer, BBottom As Integer) As Boolean    If ARight > BLeft And ALeft < BRight Then        If ABottom > BTop And ATop < BBottom Then            SimpleCollision = True        End If    End IfEnd Function\Jimmy H

edit: maybe I should also mention I assumed the coordinate system has the y-axis pointed downwards so the Top-variables have lower y-values than the bottom ones.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002054
×