# Need help with a quick collision detection algo..2-D

This topic is 4177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I don't know why this is not clicking in my head but I need help. I am working in 2-D and all I am trying to accomplish is collision detection between two rectangle objects. I am passing into a function the left, right, top, and bottom positions of each object.
bool Collision::CheckObject(int aTop, int aBottom, int aLeft, int aRight,
int bTop, int bBottom, int bLeft, int bRight){
return false;
}

I keep messing around with if statments but like I said it just not clicking for me. Regards Chad

##### Share on other sites
Go through each of object b's sides and check if that single side makes it impossible for an intersection. If the top of object b is lower than the bottom of object a, there is no way they could be intersected. Do that for all 4 sides and if none of those preclude the intersection, then there is one.

bool Collision::CheckObject(int aTop, int aBottom, int aLeft, int aRight,			int bTop, int bBottom, int bLeft, int bRight){	if (bTop > aBottom) return false; // top of b lower than bottom of a	if (bLeft > aRight) return false; // left of b to the right of a's right	if (bRight < aLeft) return false; // right of b to the left of a's left	if (bBottom < aTop) return false; // bottom of b above the top of a	return true;}

Alan

##### Share on other sites
AHhh..makes sense. I was checking if something hit something, not if it was impossible for something to happen.

Thanks

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 14
• 9
• 10
• 12
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009831
• ### Who's Online (See full list)

There are no registered users currently online

×