Skip to main content
gamedev.net
🔒 Locked

Rectangle / Rectangle collision

Started by BloodLust666 2 replies 2.5k views
Original Post
BloodLust666
BloodLust666
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)
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Bob Janova
Bob Janova
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;
BloodLust666
BloodLust666
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]
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Zakwayda
Zakwayda
Quote:
Original post by EvilKnuckles666
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
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.