Sphere-to-plane
To check if a sphere intersects anything you follow a simple formula. Find the closest point to the sphere on the shape and use this point to find the distance between the sphere and the shape. Compare the resulting distance to the radius of the sphere. If the distance is less than the radius, there is a collision. Checking if a sphere and plane intersect follows this same formula:
Getting ready
We are going to implement a function to test if a sphere and a plane are intersecting. We will also use a #define
macro to implement convenience functions to see if a plane intersects a sphere. This macro just switches the function name and arguments around.
How to do it…
Follow the given steps to implement a sphere to plane intersection test:
Declare
SpherePlane
inGeometry3D.h
:bool SpherePlane(const Sphere& sphere, const Plane& plane);
Declare the
PlaneSphere
macro inGeometry3D.h
:#define PlaneSphere(plane, sphere) \ SpherePlane(sphere, plane)
Implement
SpherePlane
inGeometry3D...