Anda di halaman 1dari 4

Vb6 Tutorial:

Rectangular Collision Detection, by Timothy Morris.

This tutorial will teach you how to use collision detection in Visual Basic 6.
To find out if two rectangles have collided, first we need to check 4 things:

1. If the right side of rectangle 1 is greater than the left side of rectangle 2
2. If the left side of rectangle 1 is less than the right side of rectangle 2
3. If the bottom side of rectangle 1 is greater than the top side of rectangle 2
4. If the top side of rectangle 1 is less than the bottom side of rectangle 2

In this example I will use two shapes as my rectangles. To find the right side of a rectangle, we need
to add the width of the rectangle to the left value of the rectangle. For example:

Right = Shape1.Left + Shape1.Width

We also use this method to find the bottom of the rectangle:

Bottom = Shape1.Top + Shape1.Height

Now we need to do the math. First, find if the right of shape 1 is greater than the left side of shape 2:

If (Shape1.Left + Shape1.Width) > Shape2.Left...

Now, if the left of shape 1 is less than the right of shape 2:

If Shape1.Left < (Shape2.Left + Shape2.Width)...

Now, use the same method for top and bottom sides:

If (Shape1.Top + Shape1.Height) > Shape2.Top...


If Shape1.Top < (Shape2.Top + Shape2.Height)...

Put all this together and we get:

If (Shape1.Left + Shape1.Width) > Shape2.Left AND Shape1.Left < (Shape2.Left + Shape2.Width) AND
(Shape1.Top + Shape1.Height) > Shape2.Top AND Shape1.Top < (Shape2.Top + Shape2.Height)
then...

COLLISION!
Another way to do this is to call the API IntersectRect. We do this by first creating a type called RECT.
This is done by starting your code with:

Option Explicit

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

This creates the type called RECT. RECT has properties Left, Right, Top and Bottom. You can create a
new RECT by typing:

Dim <name> as RECT

For example:

Dim rect1 as RECT

To change the properties of rect1, for example, the top property, we type:

rect1.top = 5

We then add below:

Private Declare Function IntersectRect Lib "user32" Alias "IntersectRect" _


(lpDestRect As RECT, lpSrc1Rect As RECT, lpSrc2Rect As RECT) As Long

This declares the Function IntersectRect so we can use it later. Before we call the function
IntersectRect, we first need to create a variable of variable type Boolean. A Boolean can be used
ONLY to store the value TRUE of FALSE. To do this, type:

Dim as Boolean

Then, we type:

Collision = IntersectRect (<temprectangle>, <rectangle1>, <rectangle2>)


IntersectRect works by taking the properties of <rectangle1> and <rectangle2> and comparing
them to find the intersection. It does all the math for you. It then returns the intersection as a
RECT into <temprectangle>. It also returns if there was an intersection as a TRUE or FALSE value
into RectCollision.

An example is:

Dim Rect1 as RECT

Rect1.left = 5
Rect1.right = 10
Rect1.top = 5
Rect1.bottom = 10

Dim Rect2 as RECT

Rect2.left = 7
Rect2.right = 12
Rect2.top = 7
Rect2.bottom = 12

Dim Collision as Boolean


Dim Rect3 as RECT

Collision = IntersectRect (Rect3, Rect1, Rect2)

Intersect will work out the intersection between Rect1 and Rect2 and returns it in Rect3. Rect3 will
become the rectangle where it was intersected. So Rect3 will have:

Left – 7
Top – 7
Right – 10
Bottom – 10

Also, Collision will become TRUE because there was an intersection.

To sum it all up, I will show you how to use this to find an intersection between two shapes:

Dim Rect1 as RECT


Dim Rect2 as RECT
Dim Rect3 as RECT
‘Also Note that you can write this as Dim Rect1, Rect2, Rect3 as RECT

Rect1.Left = Shape1.Left
Rect1.Top = Shape1.Top
Rect1.Right = Shape1.Left + Shape1.Width
Rect1.Bottom = Shape1.Top + Shape1.Height
Rect2.Left = Shape2.Left
Rect2.Top = Shape2.Top
Rect2.Right = Shape2.Left + Shape2.Width
Rect2.Bottom = Shape2.Top + Shape2.Height

Dim Collision as Boolean


Collision = IntersectRect(Rect3, Rect1, Rect2)

If there is an intersection, Collision will be TRUE, else it will be FALSE. In this case, because there
was an intersection, Collision will be TRUE.

Both theses ways can be used to find Rectangle Collision Detection. Use whichever you find
easiest.

Anda mungkin juga menyukai