Intersection between AABB and a capsule (swept sphere)

Viewed 370

I have an axis aligned bounding box in R3 space defined by minimum vector A and maximum vector B, and a capsule defined by a segment with end points a and b, and radius r. I would like to check if the two shapes intersect.

I know that the two shapes do in fact intersect if the capsule's defining segment intersects the AABB. However how do I handle the remaining case where the segment does not intersect the AABB, but the capsule still does.

2 Answers

Maybe you can try to thicken the cube by the radius of the capsule (basically, the box becomes a box with vertices replaced by spheres and you can think of the thickened box as spanned by these spheres) and replace the capsule by the line segment between the two centers of the sphere. The line segment intersects the thickened box exactly when the capsule intersects the original box.

If you only need to check whether the shapes intersected, and not where they intersected, you can reduce capsule-AABB intersection testing into capsule-capsule intersection testing.

  • find closest point of both start and end point of capsule on AABB (effectively, clamp them to the AABB's bounds)
  • if the two clamped points are equal, test capsule-point intersection with that point
    • same as capsule-sphere intersection with a sphere of radius 0
  • if the two clamped points are different, test capsule-line segment intersection
    • same as capsule-capsule intersection, with the capsule formed by the line segment having a radius of 0

I've only tested this in 2D, but I think it should work in 3D too.

Related