Rectangular Collision Detection
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.
If you're doing this with axis-aligned rectangles (rectangles that are always flat to the ground), it's pretty easy.
[Edited by - MJP on June 26, 2008 1:04:08 PM]
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]
Quote:Original post by MJPHey. 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.
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;}
Cheers.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement