Given a sphere at location vector spherePosition
with a radius of sphereRadius
and a unit vector rayVector
, (yes, I detest short variable names
,) how do we test to see if the ray intersects
with our sphere
? This is an important test for projectile
calculations, ray tracing
, particle motion
, and many other computer applications
. And, to be honest, this is about as easy as intersection tests
First, we need the scalar sphereDistanceFromOrigin, which can be calculated as the magnitude of the vector spherePosition.
- If we can travel from the origin in direction rayVector a distance of sphereDistanceFromOrigin, this gives us a new vector rayTravelledVector. This vector is the closest that our ray will ever get to spherePosition.
- We subtract rayTravelledVector from spherePosition and get the vector distanceFromSphere.
- If the magnitude of distanceFromSphere is less than sphereRadius, this ray will intersect the sphere.