Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

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

This topic is 4085 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
Advertisement
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
AHhh..makes sense. I was checking if something hit something, not if it was impossible for something to happen.

Thanks

Chad

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!