Raycast Sphere
Given a ray with origin o, indirection d and a sphere with origin c and radius r; we want to check if the ray ever intersects the sphere:
If the ray intersects the sphere, this intersection will happen at some distance along the ray. Within the context of ray casting, we often assume it takes one second to travel one unit along the ray. Because of this, distance and time are often used interchangeably.
Because of this ambiguity with the vocabulary, many resources might say that the ray intersects the sphere at some time, t. If the ray does not intersect the sphere, t is undefined.
Getting ready
We are going to implement a function to check if a ray and a sphere intersect. This function will return t, the time along the ray at which the intersection takes place. If there is no intersection, we will set t to be a negative number.
How to do it…
Follow these steps to implement raycasting against a sphere:
Declare the
Raycast
function inGeometry3D.h
:float Raycast(const Sphere& sphere...