Skip to main content
GameDev.net gamedev.net
🔒 Locked

AABB Collision Method?

Started by Mindowindow Jun 22, 2007 at 7:39 PM 1 replies 1k views
Original Post
Mindowindow
Mindowindow
I came up with a method for testing AABB collisions, I don't know if its any good.
bool AABox::intersect(const AABox& b) const
{
	return ((int(max.x - b.min.x) ^ int(min.x - b.max.x)) <= 0 && 
		(int(max.y - b.min.y) ^ int(min.y - b.max.y)) <= 0 &&
		(int(max.z - b.min.z) ^ int(min.z - b.max.z)) <= 0);
}
It appears to work for every type of intersection I've tested, including when neither min or max are inside the other or when both are. Do you think this method might be faster than the simple testing if mins are greater than maxes? It avoids branching at the cost of some subtraction, but I just want to know if its a valid method in respect to that one?
If it isn't clear in the code, this method essentially tests to see if subtracting the other min from the max is the same sign as the other max from the min.
zedz
zedz
been a while since ive looked at generated code, but i think that still has branching ie
if ( X ) return
Jasper Bekkers
Jasper Bekkers
Quote:
Original post by Gondolin
It avoids branching at the cost of some subtraction, but I just want to know if its a valid method in respect to that one?

Don't forget the cost of casting to an integer (if they weren't already), if you're using IEEE floats you might get away with comparing the integer representation of the floating point number. The only thing useful to say is benchmark. And see if the intersection test is really the bottleneck, and if it is there might be better solutions to minimise the number of tests.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.