Maths · function

raySphere

The nearest positive distance along a ray to a sphere's surface, or -1.

Explained in Coordinates and units.

function raySphere(origin: ArrayLike<number>, direction: ArrayLike<number>, centre: ArrayLike<number>, radius: number): number
import { raySphere } from '@driftengine/core';

Parameters

ParameterTypeDescription
originArrayLike<number>
directionArrayLike<number>
centreArrayLike<number>
radiusnumber

In depth

An origin inside the sphere reports the far root rather than a miss, which is the same choice rayAabb makes for the same reason: a camera that has got inside something pickable is still looking at it, and reporting a miss makes the object impossible to click from close up.

direction must be unit length, which is what lets the quadratic drop its leading coefficient.