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

## 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 on other sites
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 on other sites
Quote:
 Original post by MJPIf 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 on other sites
It's a typo. You should be compared the two X values.

##### Share on other sites
Quote:
 Original post by ToohrVykIt'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 on other sites
Yeah it's a typo, sorry about that.

1. 1
Rutin
65
2. 2
3. 3
4. 4
5. 5

• 17
• 10
• 29
• 20
• 9
• ### Forum Statistics

• Total Topics
633415
• Total Posts
3011768
• ### Who's Online (See full list)

There are no registered users currently online

×