Rectangular Collision Detection

Started by
4 comments, last by MJP 15 years, 10 months ago
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.

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]
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.

It's a typo. You should be compared the two X values.
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!

Yeah it's a typo, sorry about that.

This topic is closed to new replies.

Advertisement