Introduction#
Where did the player click? Did the bullet hit? Can the light reach this point? What does the camera see through this pixel? All of these are the same question: does this ray hit that shape, and where? This post puts the previous posts in the series to work to answer it:
- What a ray is, written with vectors
- How to test a ray against a circle (or a sphere) using a dot product and Pythagoras
- A tiny ray tracer: a picture made by firing one ray per pixel
What Is a Ray?#
A ray is a half-line: it starts at a point, the origin, and goes on forever in one direction. With vectors, every point on it can be written as:
where t is how far along the ray you are (with a unit-length direction, t is the actual distance). t = 0 is the origin, t = 2 is two units along, and negative t would be behind the start, which doesn’t count. Asking “where does the ray hit the shape?” means finding the smallest positive t where point(t) is on the shape.
Hitting a Circle#
Here’s a neat way to test a ray against a circle with centre C and radius r, using only things from earlier posts. Drag the ray’s origin, its direction and the circle.
Testing a ray against a circle
The method:
- How far along the ray is the centre? Take the vector from the origin to the centre and dot it with the (unit) direction. That’s the projection from the dot product post. Call it along.
- How close does the ray come to the centre? The point along units down the ray is the closest point to the centre (purple). Measure the distance from it to the centre (vector length). If it’s more than the radius, the ray misses.
- Where does it cross the edge? If it’s closer than the radius, the closest point, the centre and the hit point make a right-angled triangle. Pythagoras gives the distance from the closest point to the edge: √(r² − distance²). The ray enters at along minus that, and leaves at along plus it.
The same steps work for a sphere in 3D without changing a thing: the vectors just have three numbers instead of two. Spheres are the classic first shape in every ray tracer for exactly this reason.
Other shapes have their own tests. A ray against a flat plane, like a floor, is a single division, and a ray against a triangle is a plane test followed by the “which side of each edge” test from the cross product post.
A Tiny Ray Tracer#
Now for the fun part. Put a camera somewhere, and fire one ray for each pixel of a (very small, one-row) picture, fanned out across its field of view. For each ray, find the nearest ball it hits and colour that pixel with the ball’s colour. Shade it darker where the ray strikes the surface at a glancing angle, using the dot product of the surface normal and the ray, just like Basic Lighting. The strip at the bottom is the result.
A one-row ray tracer: one ray per pixel
Drag a ball in front of another and it hides it in the picture, because each pixel takes the nearest hit. That’s a ray tracer’s version of the depth buffer. Raise the number of rays and the picture sharpens: more pixels, more rays.
This is the opposite of how the rest of this site draws things. Rasterisation starts from each triangle and asks “which pixels do you cover?”; ray tracing starts from each pixel and asks “what do you see?”. Ray tracing makes reflections and shadows natural (fire another ray towards the light: if it hits something first, the point is in shadow), but it’s far more work, which is why games long relied on rasterisation and now use ray tracing hardware for selected effects.
Testing every ray against every triangle in a scene would be hopelessly slow: millions of rays times millions of triangles. Real ray tracers first group triangles into a tree of nested boxes, called a bounding volume hierarchy (BVH). A ray first tests the big boxes and only looks inside the ones it actually passes through, so it usually only has to test a handful of triangles. Modern graphics cards have dedicated ray tracing units that walk these trees and perform ray-box and ray-triangle tests in hardware, which is what made real-time ray traced reflections and shadows practical in games.
Where You’ll Use This#
- Mouse picking: turn the clicked pixel into a ray from the camera (Cameras) and find what it hits.
- Games: bullets, line-of-sight checks, and keeping characters standing on the ground are all ray casts.
- Shadows and reflections in ray tracing; the shadow map is rasterisation’s answer to the same “can the light see it?” question.
- Ray marching, which steps along a ray using a distance function to find shapes made of pure maths (Signed Distance Fields).
Summary#
- A ray is origin + t × direction, for t ≥ 0.
- Ray hits circle or sphere: project the centre onto the ray (dot product), measure how close the ray comes, then use Pythagoras to find where it crosses the edge.
- The nearest positive t is the first hit.
- Ray tracing fires a ray per pixel and keeps the nearest hit: the reverse of rasterisation.
That’s the end of Maths for Graphics, for now! These six ideas (vectors, dot and cross products, sine and cosine, lerp and rays) underpin almost everything else on the site.
Further Reading#
- Peter Shirley and others, Ray Tracing in One Weekend: a famous free book that builds a real ray tracer step by step, starting with rays and spheres.
- Scratchapixel, A Minimal Ray-Tracer: ray-sphere intersection explained in detail.
- Jacco Bikker, How to build a BVH: how real ray tracers find hits quickly.