Sphere-to-sphere
To check if two spheres overlap, we check if the distance between them is less than the sum of their radii. We can avoid an expensive square root operation by checking the square distance between the spheres against the squared sum of their radii:
Getting ready
Checking if two 3D spheres intersect is very similar to checking if two 2D circles intersect. We are going to implement a new function to check if two spheres intersect. This is the simplest 3D intersection function we are going to write.
How to do it…
Follow the given steps to implement sphere-to-sphere intersection testing:
Declare the
SphereSphere
function inGeometry3D.h
:bool SphereSphere(const Sphere& s1, const Sphere& s2);
Implement the
SphereSphere
function inGeometry3D.cpp
:bool SphereSphere(const Sphere& s1, const Sphere& s2) {
First find the sum of the radius of the two spheres:
float radiiSum = s1.radius + s2.radius;
Next find the squared distance between the two spheres:
float sqDistance =...