Jump to content
• Advertisement

# Sphere contained by AABB

This topic is 4787 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

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 this post

##### Share on other sites
Advertisement
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 this post

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

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5
• Advertisement

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013450
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!