Sphere-to-AABB
To check if a Sphere and an Axis Aligned Bounding Box (AABB) intercept, we must first find the closest point on the AABB to the Sphere. Once we have this point, we can figure out the distance between the Sphere and the closest point. Finally, we can compare this distance to the radius of the Sphere. If the distance between the closest point and the Sphere is less than the radius of the Sphere, the point is inside the Sphere:
Getting ready
We are going to implement a function to test if a Sphere and an AABB are intersecting. We will also use a #define
macro to implement a convenience function to see if an AABB intercepts a sphere. This macro just switches the function name and arguments.
How to do it…
Follow the given steps to implement sphere to AABB intersection testing:
Declare
SphereAABB
inGeometry3D.h
:bool SphereAABB(const Sphere& sphere, const AABB& aabb);
Declare the
AABBSphere
macro inGeometry3D.h
:#define AABBSphere(aabb, sphere) \ SphereAABB(Sphere, AABB)
Implement...