Public Group

# Collision detection in VB.Net

This topic is 5026 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
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009769
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!