Ray Intersections

Chris Tralie

Ray Intersect Plane

\[ (\vec{p} - \vec{a}) \cdot \vec{n} = 0 \]

Plug in p0 + tv as p in the plane equation

\[ (\vec{p_0} + t\vec{v} - \vec{a}) \cdot \vec{n} = 0 \]

Rearrange and put all of the constants together

\[ ( (\vec{p_0} - \vec{a}) + t\vec{v} ) \cdot \vec{n} = 0 \]

Distribute the dot product and solve for t

\[ t (\vec{v} \cdot \vec{n}) = - (\vec{p_0} - \vec{a}) \cdot \vec{n} \]

Distribute the negative through on the right side and divide by v . n to get the final equation

\[ t = \frac{ (\vec{a} - \vec{p_0}) \cdot \vec{n} } {\vec{v} \cdot \vec{n}} \]


Ray Intersect Triangle


Ray Intersect Sphere

Recall that the parametric form for a ray is

\[ \vec{p(t)} = \vec{p_0} + t \vec{v} \]

The larger the t, the further we move along the ray, as depicted in the picture below:

We also need an implicit equation for the a sphere centered at vec3 c with a radius of float r. This is shown in the picture below:

By definition, every point vec3 p on such a sphere is a distance r from the center c. This can be expressed as the equation:

\[ | \vec{p} - \vec{c} | = r \]

Or in other words, the length of the vector p-c is r. This equation turns out to a bit annoying because it has a square root. If we recall that the dot product of a vector with itself is its squared length, then we can rewrite the above equation as

\[ (\vec{p} - \vec{c}) \cdot (\vec{p} - \vec{c}) = r^2 \]

Now we want to find a point on the sphere that agrees with the ray. So let's plug in the parametric equation for the ray into vec3 p

\[ (\vec{p_0} + t\vec{v} - \vec{c}) \cdot (\vec{p_0} + t\vec{v} - \vec{c}) = r^2 \]

Let's re-arrange the terms so that the constant vectors v and p0 are together:

\[ ( (\vec{p_0} - \vec{c}) + t\vec{v} ) \cdot ( (\vec{p_0} - \vec{c}) + t\vec{v} ) = r^2 \]

If we follow the distributive rule for the dot product (that is, a . (b+c) = a.b + a.c), then we can FOIL the above to get

\[ (\vec{p_0} - \vec{c}) \cdot (\vec{p_0} - \vec{c}) + 2 t \vec{v} \cdot (\vec{p_0} - \vec{c}) + t^2 \vec{v} \cdot \vec{v} = r^2 \]

This is actually just a quadratic equation in t with lots of fancy constant coefficients! To see this, we'll rearrange this a bit:

\[ \left( \vec{v} \cdot \vec{v} \right) t^2 + \left( 2 \vec{v} \cdot (\vec{p_0} - \vec{c}) \right) t + \left( (\vec{p_0} - \vec{c}) \cdot (\vec{p_0} - \vec{c}) - r^2 \right) = 0 \]

Or, in other words, the canonical form for a quadratic equation (!)

\[ at^2 + bt + c\]

where we have

a

\[ \vec{v} \cdot \vec{v} \]

b

\[ 2 \vec{v} \cdot (\vec{p_0} - \vec{c}) \]

c

\[ (\vec{p_0} - \vec{c}) \cdot (\vec{p_0} - \vec{c}) - r^2 \]

We can solve this with the quadratic formula:

\[ t = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]

I'll refer to the smaller of the two t's (where we subtract the square root) as tmin and the larger of the two as tmax. But wait, why are there two solutions? This is because the sphere is curved in a way where a ray can puncture into it at tmin, travel on the inside, and then exit out the other side at tmax, as shown below:

That said, we definitely have a few special cases to consider, as we discussed in the class:

\[ b^2 - 4ac < 0 \]

Or, in other words, there are no real solutions. In this case, the ray completely misses the sphere

\[ b^2 - 4ac = 0 \]

In this case, there is a "double root" (since plus or minus 0 is the same), which translates geometrically into the ray just grazing the sphere

\[ t_{\text{min}}, t_{\text{max}} < 0\]

In this case, there are two real solutions, but they're on the wrong side of the ray, so the ray is pointing away from the sphere

\[ t_{\text{min}} < 0, t_{\text{max}} \geq 0\]

In this case, the ray is on the inside of the sphere, so only one of the solutions (tmax) is actually in front of the ray