Linetest Sphere

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:

Linetest Sphere

Getting ready

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.

How to do it…

Follow these steps to implement ...

Get Game Physics Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.