Collision detection & response
Axially aligned bounding box (AABB)
Closest Point in an AABB
This is done by “pushing” into along each axis in turn. Notice that if the point is already inside the box, this code returns the original point.
if (x < minX) {
x = minX;
} else if (x > maxX) {
x = maxX;
}
if (y < minY) {
y = minY;
} else if (y > maxY) {
y = maxY;
}
if (z < minZ) {
z = minZ;
} else if (z > maxZ) {
z = maxZ;
}
Intersection of a Sphere and AABB
... we first find the point on the box that is closest to the center of the sphere by using the... Closest Point in an AABB... We compute the distance from this point to the center of the sphere and compare this distance with the radius. (Actually, in practice we compare the distance squared against the radius squared to avoid the square root in the distance computation.) If the distance is smaller than the radius, then the sphere intersects the AABB.