Jump to content
  • Advertisement
Sign in to follow this  
Mybowlcut

Rectangular Collision Detection

This topic is 3799 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 this is really simple but I can't figure it out. I know how to test whether a rectangle is inside a rectangle and hence whether it is outside... but I am unsure of how to deal with overlapping (collisions). Can someone please explain this/provide pseudo-code? Cheers.

Share this post


Link to post
Share on other sites
Advertisement
If you're doing this with axis-aligned rectangles (rectangles that are always flat to the ground), it's pretty easy.


bool CollisionTest(Rectangle rect1, Rectangle rect2)
{
if (rect1.maxX < rect2.minX)
return false;
if (rect1.minX > rect2.maxX)
return false;
if (rect1.maxY < rect2.maxY)
return false;
if (rect1.minY > rect2.minY)
return false;

return true;
}


[Edited by - MJP on June 26, 2008 1:04:08 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by MJP
If you're doing this with axis-aligned rectangles (rectangles that are always flat to the ground), it's pretty easy.


bool CollisionTest(Rectangle rect1, Rectangle rect2)
{
if (rect1.maxX < rect2.minX)
return false;
if (rect1.minY > rect2.maxX)
return false;
if (rect1.maxY < rect2.maxY)
return false;
if (rect1.minY > rect2.minY)
return false;

return true;
}
Hey. Thanks for the fast reply. What is the second if statement actually testing? I don't get why you'd compare an x and y value in the same if statement.

Cheers.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
It's a typo. You should be compared the two X values.
Ahhh. I thought that might have been the case but was unsure since I'm really bad at this stuff haha.

Cheers!

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!