Sphere contained by AABB

This topic is 4703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

What is the best method to determine if a sphere is inside an AABB? I have to know if a sphere is inside, intersecting or outside the AABB. At the moment I’m testing the sphere against the planes of the AABB (frustum style) but that can’t be the best way to do it, right?

Share on other sites
Quote:
 At the moment I’m testing the sphere against the planes of the AABB (frustum style) but that can’t be the best way to do it, right?
Are you taking advantage of the fact that the planes are axis-aligned? That should make the resulting computations pretty trivial. In a sense, you're testing to see if a point is in an AABB 'shrunk' by the sphere radius, and a point-in-AABB test is pretty straightforward.

Share on other sites
Quote:
 Original post by CalexusWhat is the best method to determine if a sphere is inside an AABB?
A sphere S lies fully inside an AABB B if the AABB of S lies fully inside B. This, as jyk points out, you could turn into a point in AABB containment test by shrinking all sides of B by the sphere radius and testing if the sphere center lies inside the shrunken box.

To determine all three relationships (INSIDE, OUTSIDE, INTERSECTING) in a single routine, you could do this (untested code):

int ClassifySphereToAABB(Sphere s, AABB b){    int inside = 0;    float sqDist = 0.0f;    for (int i = 0; i < 3; i++) {        float v = s.c;        if (v < b.min)            sqDist += (b.min - v) * (b.min - v);        else if (v > b.max)            sqDist += (v - b.max) * (v - b.max);        else if (v >= b.min + s.r && v <= b.max - s.r)            inside++;    }    if (inside == 3) return INSIDE;    if (sqDist > s.r * s.r) return OUTSIDE;    return INTERSECTING;}

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
16
5. 5

• 9
• 33
• 13
• 13
• 10
• Forum Statistics

• Total Topics
632580
• Total Posts
3007186

×