# Rect to Rect Collision Side Detection ?

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

## Recommended Posts

Hi guys!

i have coded a quick Rect to Rect Collision detection
that detects any size cubes for collision. Works fine.

I am looking for a way to actually determine sides of Recrs
that have collided.

Please if anyone has time and idea, please post it here.
Thanks for your time.

here is the code:

bool rectCollision(Rect &REC1, Rect &REC2){         // Check if Rect Y is on Y of the other Rect     if (REC1.CenterVertex.y - (REC1.h/2) <= REC2.CenterVertex.y + (REC2.h/2) &&         REC1.CenterVertex.y + (REC1.h/2) >= REC2.CenterVertex.y - (REC2.h/2))     {         // Check if Rect X is on X of the other Rect         if (REC1.CenterVertex.x + (REC1.w/2) >= REC2.CenterVertex.x - (REC2.w/2) &&             REC1.CenterVertex.x - (REC1.w/2) <= REC2.CenterVertex.x + (REC2.w/2)){                          // Collision has occured             // Determine which side has collided ??!???             // Return True (ther is  a collision, screen blinks to indicate that)             return true;         }                                                                                                   }      return false;}

##### Share on other sites
A few common solutions:

1. Measure the overlap in each of the four directions (+/- x and y), and consider the side where the overlap is least to be the side where the collision occurred.

2. Determine the side based on the direction of the velocity of the moving object (if only one of the objects is moving).

3. Determine the side based on the relative positions of the objects prior to the intersection.

4. Use a swept test and determine the time of intersection (and where the intersection took place) exactly.

• 10
• 17
• 9
• 14
• 41