Sign in to follow this  
B_old

AABB inside Sphere vs. just intersecting

Recommended Posts

B_old    689
I've been using the following code to test for intersection between an AABB and a sphere (I don't recall where it is from). Unfortunately it doesn't tell me whether the AABB is completely inside the sphere or just intersecting. Should I add an "completely inside" test at the end, or is there a more elegant solution to do this?

[code] IntersectType Sphere::intersect(const Aabb &aabb) const
{
Vector3 min, max;
aabb.getMinMax(min, max);

float d = 0.f;

if (m_pos.x < min.x)
{
float s = m_pos.x - min.x;
d += s * s;
}
else if (m_pos.x > max.x)
{
float s = m_pos.x - max.x;
d += s * s;
}

if (m_pos.y < min.y)
{
float s = m_pos.y - min.y;
d += s * s;
}
else if (m_pos.y > max.y)
{
float s = m_pos.y - max.y;
d += s * s;
}

if (m_pos.z < min.z)
{
float s = m_pos.z - min.z;
d += s * s;
}
else if (m_pos.z > max.z)
{
float s = m_pos.z - max.z;
d += s * s;
}

if (d <= m_radius * m_radius) return intersect_in;
else return intersect_out;
}[/code]

EDIT: Apparently the above method for testing AABB-Sphere overlapping is called Arvo's method.

Share this post


Link to post
Share on other sites

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