Derive ray-plane and ray-sphere intersection formulas from scratch with interactive visualizations, step-by-step math, and practical tips for handling edge cases like parallel rays and negative t values.
What Is a Ray?
The way we describe a ray in real life is that it starts from a point and extends infinitely in a given direction.
We can represent this definition mathematically. The ray starts at a point, which we call , and extends in a specified direction . Since we cannot represent infinity in a computer, we introduce a parameter , which denotes the length of the ray.
Ray-Plane Intersection
A plane is a good starting point to discuss intersections, and a common question is: how can we determine when a ray intersects with a plane?
The idea is that if a ray intersects with a plane, the intersection point must lie on the plane.
We know that we can represent this intersection point using the definition of a ray:
We also know that if a point lies on a plane, the dot product of that point and the plane’s normal is zero:
Therefore, we can plug our equation for into the dot product equation:
The question of whether a ray intersects with a plane then becomes a matter of solving for the unknown variable . If the equation can be solved, then we can say that an intersection occurs.
But, hold on—how can we calculate the normal of the plane?
We can assume that the plane is fixed at the origin and faces upward. When a transformation is needed, we can use a matrix to transform the plane. For now, the normal of the plane can be taken as .
Now, let’s proceed to solve the equation using the quadratic formula.
Solving for — Step by Step
We start with the condition for a ray intersecting a plane:
Now we expand the dot product into two terms:
Then we apply the associativity of scalar multiplication to take out of the dot product:
Next, we subtract from both sides to isolate the term with :
Finally, we divide both sides by to solve for :
This gives the scalar , which tells us how far along the ray direction the intersection with the plane occurs.
Edge Cases: Negative and Parallel Rays
There are two invalid cases for this function:
If is a negative value, it means the plane is behind our ray. Imagine we are the ray facing in the direction of , in this case, a negative indicates that the plane lies behind us, which makes no sense in the context of an intersection.
The other invalid case occurs when the denominator is zero. This implies that the plane is parallel to the ray’s direction, and we need to handle this situation to avoid division by zero errors, which could cause compilation issues.
To sum it up: we consider valid if and only if it falls within the range .
Ray-Sphere Intersection
We all know that if a point lies on a sphere, then the square of the distance from the point to the center of the sphere must equal the square of the radius . That is:
This expression is exactly the square of the Euclidean distance between point and the center. Using vector notation, we can write this more compactly as:
where is the center of the sphere. Taking the square root of both sides (and assuming the distance is non-negative), we get:
Now, for simplicity, we assume that the center of the sphere is located at the origin, i.e., . In this case, the expression simplifies to:
By definition, the length of a vector is:
Therefore, the equation becomes:
To make our lives easier, let’s square both sides and get rid of that annoying square root:
There you go. You can play the same trick again by replacing with the ray definition , and we get:
You probably already know that dot products are distributive:
This is actually a perfect square formula:
The only thing that might feel a bit fast here is that I just pulled outside — but it’s fine because it’s a scalar, as we did before.
We already know everything about the ray — that is, as the ray origin and as the ray direction — and we also know the radius of the sphere.
So, this becomes a quadratic equation in terms of the unknown .
Maybe it’s easier to recognize it in this order:
Let’s solve it using the quadratic formula:
So here:
- is
- is
- is
I don’t really want to expand that equation, to be honest.
But if you’re interested, here it is:
Hopefully you’re not scared — I really wanted to use color coding but KaTeX doesn’t support it :(
Anyway, here it is.
Two Solutions, One Intersection — Why We Use min()
First things first: we do need to evaluate this equation every frame if we solve it analytically.
So, the first check here is whether the whole thing inside the square root is not less than zero, because we’re not dealing with complex numbers here.
Then, yeah — we actually get two solutions.
Just imagine: the first point where the ray hits the sphere is the entry point, and there should be another one where it exits.
So, we only care about the closest one. That’s why you just use the min() function when you implement it.
That’s all for today — hope you enjoyed it and found it useful.
See you!