Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!