Skip to content
Ray-Shape Intersections
step 1/5

Reading — step 1 of 5

Read

~2 min readRay Tracing

Ray-Shape Intersections

For each shape type, derive: does this ray hit, and where?

Ray-sphere:

Ray: P(t) = O + t*D Sphere: |P - C|² = r²

Substitute: |O + t*D - C|² = r² Expand to quadratic in t:

a = D · D = 1 (if D normalized)
b = 2 * D · (O - C)
c = (O - C) · (O - C) - r²
discriminant = b² - 4ac

Real solutions if discriminant ≥ 0:

python

The t parameter is the distance along the ray. We typically want closest hit > 0.

Ray-triangle (Möller-Trumbore):

python

Returns t (distance along ray) + barycentric coordinates.

Ray-plane:

  • Plane: n · P = d.
  • Substitute: n · (O + t*D) = d → t = (d - n·O) / (n·D).
  • If denominator 0: parallel.
python

Ray-AABB:

  • Slab method: 3 pairs of planes.
  • t_min, t_max per axis.
  • Intersection if max(t_mins) < min(t_maxes).

Used for BVH traversal. Fast.

Ray-mesh:

  • Test ray against every triangle.
  • Slow: O(N) per ray.
  • Fix with BVH or grid: O(log N).

For complex shapes:

  • Cubes, cylinders, cones: closed-form intersections exist.
  • General: convert to triangles + ray-triangle.
  • Implicit surfaces: ray marching (sphere tracing).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…