How do you derive a ray-versus-sphere intersection test, and what does the discriminant of the resulting quadratic tell you?
answer
- substitute the ray into the sphere
- quadratic in t
- sign of the discriminant
- smallest non-negative root
basics
~20 sSubstitute the ray o + t*d into |x - c|^2 = r^2 to get a quadratic in t. A negative discriminant is a miss, zero a tangent graze, positive two crossings; the hit is the smallest non-negative root.
solid answer
~50 sWith the ray `o + t*d` (unit `d`) and a sphere of centre `c` and radius `r`, let `m = o - c`. Substituting gives `t^2 + 2*b*t + k = 0` with `b = dot(m, d)` and `k = dot(m, m) - r^2`, so `t = -b ± sqrt(b^2 - k)`. The discriminant `b^2 - k` equals `r^2` minus the squared distance from the centre to the ray's line: negative is a miss, zero a graze, positive two crossings. I early-out when `k > 0` and `b > 0`, because the origin is outside and pointing away. Otherwise the hit is `-b - sqrt(disc)`; if that is negative the origin is inside the sphere and `-b + sqrt(disc)` is where the ray leaves it. For a non-unit direction the `t^2` coefficient `dot(d, d)` must stay in the formula.
code
pseudocode · 13 linesfunction raySphere(o, d, c, r): // d has unit length
m = o - c
b = dot(m, d)
k = dot(m, m) - r*r
if k > 0 and b > 0:
return MISS // outside and pointing away
disc = b*b - k
if disc < 0:
return MISS // line passes the sphere by
t = -b - sqrt(disc) // entry point
if t < 0:
t = -b + sqrt(disc) // origin inside: exit point
return HIT(t)go deeper
Recall the setup: substitute the ray into the sphere equation, get a quadratic in t, and read hit or miss from the sign of the discriminant.
Derive the half-b form on a whiteboard, explain the outside-and-away early-out, and pick the correct root when the origin is inside the sphere or the sphere is behind it.
Explain the discriminant's geometric meaning, when the square root can be skipped, and how precision degrades for a small, distant sphere so the origin should be shifted closer.
Treat ray-sphere as the cheap gate in front of exact mesh tests, and weigh how tight bounding spheres must be against the false positives that push work onto triangle tests.
## Setting up the problem A **ray** is a starting point `o` and a direction `d`, describing the points `o + t*d` for `t >= 0`. A **sphere** with centre `c` and radius `r` is every point `x` with `dot(x - c, x - c) = r^2`. Bounding spheres are a cheap first test for a hitscan shot: a sphere around a character's head, say, can reject most shots before any triangle of the mesh is touched. The intersection is the set of `t` values for which the ray point also satisfies the sphere equation. ## Deriving the quadratic Let `m = o - c`, the vector from the sphere's centre to the ray's origin. Then: 1. Substitute: `dot(m + t*d, m + t*d) = r^2`. 2. Expand: `dot(d, d)*t^2 + 2*dot(m, d)*t + dot(m, m) - r^2 = 0`. 3. If `d` is unit length, `dot(d, d) = 1`. Name `b = dot(m, d)` and `k = dot(m, m) - r^2`, giving `t^2 + 2*b*t + k = 0`. 4. The quadratic formula with the factor of two cancelled gives `t = -b ± sqrt(b^2 - k)`. For a direction that is not unit length, keep `a = dot(d, d)` and use `t = (-b ± sqrt(b^2 - a*k)) / a`. Forgetting `a` is a common slip in this derivation. ## Reading the discriminant The **discriminant** is `disc = b^2 - k`. It has a direct geometric meaning: `-b` is the parameter of the ray's closest approach to the centre, the squared distance from the centre to the ray's line is `dot(m, m) - b^2`, and so `disc = r^2 - (squared distance from the centre to the line)`. | Discriminant | Geometry | Roots | |---|---|---| | `disc < 0` | The line passes farther than `r` from the centre | None: a miss | | `disc = 0` | The line just touches the sphere | One double root: a tangent graze | | `disc > 0` | The line passes through the sphere | Two roots, `sqrt(disc)` either side of `-b` | The discriminant describes the infinite **line**, not the ray. A positive discriminant does not mean the shot hits, because both crossings may lie behind the shooter. ## Picking the right root With `t0 = -b - sqrt(disc)` and `t1 = -b + sqrt(disc)`, so `t0 <= t1`: - Both roots positive: the origin is outside and the sphere is ahead; the hit is `t0`, the entry point. - `t0` negative, `t1` positive: the origin is inside the sphere (`k < 0`); the ray leaves through `t1`. Whether that counts as a hit at `t = 0` or at the exit depends on the query. - Both roots negative: the sphere is entirely behind the origin; a miss. - For a weapon with a maximum range, also reject a hit beyond that range. ## Implementation notes - **Early-out:** when `k > 0` (origin outside) and `b > 0` (direction pointing away from the centre), both roots are negative, so return a miss before the square root. - **Yes-or-no tests need no square root:** after that early-out, `disc >= 0` alone decides a hit, because an outside origin pointing toward the centre puts `t0` ahead of it and an inside origin always hits. - **Precompute `r^2`** per sphere rather than squaring in the loop. - **Precision:** when the sphere is tiny and far away, `b^2` and `k` are large and nearly equal, so their difference loses digits. Shifting the origin closer to the sphere before solving is the usual remedy; the floating-point reasons belong with numerical robustness. ## A worked example A ray from `o = (0, 0, 0)` along `d = (0, 0, 1)` toward a sphere at `c = (0, 1, 10)` with `r = 2`: `m = (0, -1, -10)`, `b = -10`, `k = 101 - 4 = 97`, `disc = 100 - 97 = 3`. The roots are `10 ± sqrt(3)`, so the shot enters at about `t = 8.27` and leaves at about `t = 11.73`. The centre's distance from the line is 1, and `r^2 - 1 = 3` matches the discriminant.
- Why is b^2 - k the same as checking whether the centre lies within r of the ray's line?With a unit direction, `-b` is the parameter of closest approach, and the squared distance from the centre to the line is `dot(m, m) - b^2`. Substituting `k = dot(m, m) - r^2` gives `b^2 - k = r^2 - (squared distance)`, so the discriminant is non-negative exactly when that distance is at most `r`.
- How do you limit the test to a weapon's maximum range?Compute the entry root as usual and accept it only if `0 <= t <= range` (with a unit direction, `t` is distance). A hit beyond the range is a miss. If the origin is inside the sphere, decide whether the query reports `t = 0` or the exit root, and check that against the range too.
- When can you skip the square root entirely?When the caller only needs to know whether the ray hits, not where. After rejecting an outside origin pointing away (`k > 0` and `b > 0`), a non-negative discriminant guarantees a hit at a non-negative `t`, so the test is just a few dot products and comparisons.
- A positive discriminant always means the sphere is hit in front of the shooter.
- The smaller root is always the hit distance, even when the ray starts inside the sphere.
- The formula t = -b ± sqrt(b^2 - k) works unchanged for a non-unit direction.
- A ray that starts inside a sphere cannot intersect it.
- You must take a square root to know whether the ray hits at all.