
Game Physics Cookbook
By :

Unlike a Raycast, when we perform a linetest we only care about a Boolean result. To check if a Line and Sphere are intersecting, we need to find the closest point to the center of the Sphere on the Line. If the distance between the closest point and the center of the Sphere is less than the radius of the Sphere, the shapes intersect:
We are going to implement a function to check if a Line and a Sphere intersect. This function will return a Boolean result. We will avoid the square root involved in finding the distance between the two points by checking the squared distance.
Follow these steps to implement line testing against a sphere:
Declare the Linetest
function in Geometry3D.h
:
bool Linetest(const Sphere& sphere, const Line& line);
Implement the Linetest
function in Geometry3D.h
:
bool Linetest(const Sphere& sphere, const Line& line) { Point closest = ClosestPoint(line, sphere.position); float distSq = MagnitudeSq(sphere.position...