# Visual Basic > Visual Basic 6 and Earlier >  [RESOLVED] [VB6]  Simple rectangle collision detection?

## OrangeBlaze

Hello all!  :wave: 

I was looking for a simple collision detection for VB6, and I found a couple good ones, but I found them too complicated for my needs, and most of the code didn't make sense (I am still a bit new) I only need to detect if two rectangles are touching/overlapping. 

Thanks for the help!  :big yellow:

----------


## Elroy

IMHO, that's a nice project for a "still a bit new" person.

Why don't you show us what you've got, and please select your code (when posting), and then click the # button (on the forum's toolbar).

----------


## Joe Caverly

I did a search of the VBForums, using the words;



```
detect if two rectangles are touching overlapping
```

and it returned many results.

One result was Overlapping Calculation in VB.NET, which can be converted to VB6 (math is math).

Many other results were returned for the query.

Joe

----------


## OptionBase1

Edit:  Something like this should work:



```
If ((Rect1.Left > Rect2.Left And Rect1.Left < (Rect2.Left + Rect2.Width)) Or _
    (Rect1.Left < Rect2.Left And (Rect1.Left + Rect1.Width) > Rect2.Left)) And _
   ((Rect1.Top > Rect2.Top And Rect1.Top < (Rect2.Top + Rect2.Height)) Or _
    (Rect1.Top < Rect2.Top And (Rect1.Top + Rect1.Height) > Rect2.Top)) Then
    
    MsgBox "They are overlapping"
Else
    MsgBox "They are not overlapping"
End If
```

----------


## ColinE66

You've got the PtInRect (i.e. Point In Rectangle) API that you could use. Just need to test each corner (i.e. Point) of rectangle A to see if any exist wthin rectangle B.

----------


## Eduardo-

IntersectRect API.




> Return value:
> 
> If the rectangles intersect, the return value is nonzero.
> 
> If the rectangles do not intersect, the return value is zero.


Other rectangle functions: https://learn.microsoft.com/en-us/wi...ngle-functions

----------


## OrangeBlaze

Thanks all for your help on this problem, I was originally going to use code similar to the answer OptionBase provided, but I think I will use the PtInRect API for this case.

Happy Holidays!

----------


## Schmidt

> Thanks all for your help on this problem, I was originally going to use code similar to the answer OptionBase provided, but I think I will use the PtInRect API for this case.


Better use IntersectRect - because it covers "all universal cases".

With PtInRect you cannot be sure, even when you test "all 8 corner-points" instead of just 4 -
just imagine an intersection of two rectangular-stripes which look like below:
*+*
...where none of the two "stripes" has any corner-point in the other rectangular-stripe.

Olaf

----------


## OrangeBlaze

Thanks for pointing that out, I didn't think of it!

----------


## baka

I use this:



```
If Not (x1 > p.x2 Or y1 > p.y2 Or x2 < p.x1 Or y2 < p.y1) Then
```

I found it much easier to use and faster I think instead of an API that require 3 RECT if its not needed.
so
x1,x2,y1,y2 is for one rect
and
p.x1,p.x2,p.y1,p.y2 is the other

and yes, it works for 2 shapes in a "+" position as well.

----------


## reexre

I use this


```
Public Function RectOverlap(aX1#, aY1#, aX2#, aY2#, bX1#, bY1#, bX2#, bY2#) As Boolean
    If aX1 > bX2 Then Exit Function
    If aY1 > bY2 Then Exit Function
    If bX1 > aX2 Then Exit Function
    If bY1 > aY2 Then Exit Function
    RectOverlap = True
End Function
```

Where X1,Y1 ( left,top ) are the upper left coordinates and X2,Y2 ( left+width , top+height ) are the lower right coordinates.

same approach as baka but written differently. (maybe faster, as much as it matters, because there may be fewer comparisons)

----------


## ColinE66

> Better use IntersectRect - because it covers "all universal cases".
> 
> With PtInRect you cannot be sure, even when you test "all 8 corner-points" instead of just 4 -
> just imagine an intersection of two rectangular-stripes which look like below:
> *+*
> ...where none of the two "stripes" has any corner-point in the other rectangular-stripe.
> 
> Olaf


Ooh, yeah. Guess I didn't think that through :/

Never knew about the IntersectRect API call - somehow passed me by...

----------

