Jump to content
  • Advertisement
Sign in to follow this  
BloodLust666

Rectangle / Rectangle collision

This topic is 4526 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

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


Link to post
Share on other sites
Advertisement
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 this post


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


Link to post
Share on other sites
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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!