• Advertisement
Sign in to follow this  

AABB inside Sphere vs. just intersecting

This topic is 2386 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'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?

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;
}


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
Advertisement
Sign in to follow this  

  • Advertisement