Sign in to follow this  

Rect to Rect Collision Side Detection ?

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

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

Share this post

Link to post
Share on other sites

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