hey if ya have 2 movin objects, and you want something to happen when they colide,how do you detect they have colided. get me? (i'm fairly new to vb)

reboot

01-30-2002, 11:59 AM

well... I searched this forum for "collision detection" and got All These (http://www.visualbasicforum.com/search.php?s=&action=showresults&searchid=5204&sortby=lastpost&sortorder=descending) hits.

Searching is cool..... :)

Teric

01-30-2002, 02:02 PM

On a basic level, collision detection works in this manner:

You have two objects, let's call them ObjA and ObjB.

Each object has an X and Y coordinate (and a Z coordinate as well, if you're working in 3D).

Each object also has a height and width (and depth in 3D).

In 2D terms, you can use the X, Y, height and width to detect if a collision occurs between ObjA and ObjB. There are several ways of doing this.

1. If your X and Y coordinates indicate the upper left corner of your object, then the dimensions of your object can be determined by a rectangle. This rectangle can be shown by drawing horizontal lines at Y and at Y + Height, and by drawing vertical lines at X and at X + Width. This rectange can be determined for both objects (you don't have to draw it, just know where the edges are). In order to detect a collision, you check to see if the corners or edges of both objects have overlapped. You can do this using the following:

'First Check to see if there is an 'X Collision' (You can draw a
'vertical line down through both objects)
If (ObjA.X >= ObjB.X and ObjA.X <= (ObjB.X + ObjB.Width)) Or _
((ObjA.X + ObjA.Width) >= ObjB.X and (ObjA.X + ObjA.Width) <= (ObjB.X + ObjB.Width)) Then
'We know that the objects are on the same vertical line, now
'check Y Collision
If (ObjA.Y >= ObjB.Y and ObjA.Y <= (ObjB.Y + ObjB.Height)) Or _
((ObjA.Y + ObjA.Height) >= ObjB.Y and (ObjA.Y + ObjB.Height) <= (ObjB.Y + ObjB.Height)) Then
'Collision!
'Do your collision code here.
End If
End If

2. If your X and Y Designate the center of an object and your objects are more or less rectangular, you can do something similar to #1, but you must compute the coordinates of your left, top, right, and bottom edges by using something like this:

Once you've done that, you can use those variables in the code shown in #1 above.

3. If your X and Y designate the center of an object and your objects are roughly circular, you can use some math to figure out if there is a collision. Remember from math class that the radius of a circle is the distance from its center to its edge. So, if anything comes closer than the radius distance, you know it's a collision.

To determine a collision between two circular objects, you need to calculate the distance between the two center points, then see if that distance is less than or equal to the sum of the two objects' radii (plural of radius). In this manner:

'Now compute the distance between the center points of the two
'objects. You can do this using the Pythagorean theorem (remember
'this one from math class?) a^2 + b^2 = c^2, or
'c = Square Root (a^2 + b^2)
Distance = Sqr((ObjA.X - ObjB.X)^2 + (ObjA.Y - ObjB.Y)^2)

'Now check to see if the distance between the two centers is
'less than or equal to the sum of the two radii.
If Distance <= (ObjARadius + ObjBRadius) Then
'Collision!
'Do your collision code here
End If

Whew... sorry, long note. But I wanted to share my views on collision detection. There are probably more ways to determine collisions out there, but these are the ones that I know.

ChiefRedBull

01-31-2002, 03:45 AM

Nicely said Teric. :) I didn't think of the circular methods....

Just another quick note, you can use the InterSectRect to speed up rectangular detection.

wild wolf

01-31-2002, 03:58 AM

well said teric,
click here (http://www.visualbasicforum.com/showthread.php?s=&threadid=12160&highlight=speed) for more help

wow this is strange, i posted a reply here and now i cant see it :eek:

Iceplug

01-31-2002, 08:05 AM

I made a boolean function to check if two objects touch. Unfortunately, I don't have it now.:(