Ray/disc intersections
A ray/disc intersection is just the combination of two other tests. We check for an intersection between the ray and the infinite plane that contains the disc; if such an intersection exists, then we check whether the hit position lies on the disc, using a simple distance check.
I've already written about ray/plane intersection tests, so this article will build on those. I've also provided code at the bottom as a public domain work.
The distance check
Let's review our ray/plane check:
- Do
- origin of the disc
- Dn
- disc surface normal
- Ro
- the ray's origin
- Rd
- the ray's direction
- Hp
- the position at which the ray hits the infinite plane containing our disc
- Hd
- the hit distance: the distance from the ray origin to the hit position
We'll supplement that with a distance check:
- Dr
- radius of the disc
On its own, this is actually a check to see if the hit position lies inside of a sphere centered on
Sadly, there's no way to avoid having to do the math for the hit position. Ah, well. We can at least save a square root operation (good for computers) if we compute the check like this, remembering that the dot product of a vector with itself is the vector's length squared:
Free code!
The following C++ code is licensed under CC0 (full legal text / summary), and so is effectively a public domain work. You can incorporate it into your programs without the need for attribution, payment, or similar. (That said, linking back here would be polite!)
This code makes use of the GLM library for vector math. It also depends on my ray_plane_intersection
function.