Sign in to follow this  

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

This topic is 3866 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 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 this post


Link to post
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 this post


Link to post
Share on other sites

This topic is 3866 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this