# Rectangle / Rectangle collision

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

## Recommended Posts

I know how to figure out whether one rectangle is colliding with another, but how do I figure out with edge it hit with having to REcheck the collision again and using the vectors of motion? is that possible? here's my detection: // COLLISION! // Note: The position is from the center point, which is why i divide the dimension by 2 if ((CurObj.X + (CurObjDim.X / 2.0f) < CollideObj.X - (CurObjDim.X / 2.0f) || CurObj.Y + (CurObjDim.Y / 2.0f) < CollideObj.Y - (CurObjDim.Y / 2.0f) || CurObj.X - (CurObjDim.X / 2.0f) > CollideObj.X + (CurObjDim.X / 2.0f) || CurObj.Y - (CurObjDim.Y / 2.0f) > CollideObj.Y + (CurObjDim.Y / 2.0f)) == false)

##### Share on other sites
See all those things with || between them? Those are the different ways the collision can occur. Try returning a value that indicates which way the collision happened, maybe with 0 for no collision.

if(CurObj.X + (CurObjDim.X / 2.0f) < CollideObj.X - (CurObjDim.X / 2.0f)) return 1;else if .... return 2;...else return 0;

##### Share on other sites
actually, shouldn't i check for two of those to happen, because a rectangle won't intersect another rectangle linearly, at least two sides of the rectangle will intersect with another one.

I can figure out which two intersected, but I need to find which one to more the first rectangle back to.

ex:

the two different instances come out to the same collision, but the motion vector is different, how do i use that to find out which side it hit and then figure out where to put the rectangle back so it look like it stuck to the edge and at the point it hit?

edit: i probably should have made it more clear that the left example the first box hits the left side of the second box; and the second example the first box hits the bottom of the second box

[Edited by - EvilKnuckles666 on July 28, 2006 5:53:47 PM]

##### Share on other sites
Quote:
 Original post by EvilKnuckles666actually, shouldn't i check for two of those to happen, because a rectangle won't intersect another rectangle linearly, at least two sides of the rectangle will intersect with another one.I can figure out which two intersected, but I need to find which one to more the first rectangle back to.ex:the two different instances come out to the same collision, but the motion vector is different, how do i use that to find out which side it hit and then figure out where to put the rectangle back so it look like it stuck to the edge and at the point it hit?edit: i probably should have made it more clear that the left example the first box hits the left side of the second box; and the second example the first box hits the bottom of the second box
To distinguish between these two cases you'll need to perform a swept rather than static intersection test. There's a good explanation of this test, with code, for axis-aligned boxes in a gamasutra article (by Gomez, I believe) which you can find in the gdnet articles section.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
13
5. 5

• 9
• 14
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632601
• Total Posts
3007359

×