Sign in to follow this  

Collision detection in VB.Net

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 If
End 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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this