Day 5, Assume nothing
Today I Learned that I shouldn’t have assumed the sphere would ever need to change from being a unit sphere, so intersecting gets even simpler:
\[\|\dot{O} + t\overrightarrow{D}\|^2 - 1 = 0\]def intersect(ray)
l = ray.origin - Point::ZERO
d = ray.direction
a = d.dot(d)
b = 2 * d.dot(l)
c = l.dot(l) - 1
intersections = quadratic(a, b, c).map { |t| Intersection.new(t, self) }
Intersections.new(*intersections)
end
You might wonder why subtract the zero point from the ray origin. \(x - 0 = x\), right? Wrong! \(\dot{p_1} - \dot{p_0} = \overrightarrow{v_1}\), subtracting points yields a vector, due to that neat trick I mentioned on the 0:th day, and \(v_1 \cdot v_2\) is only valid for vectors, not points.
See, a point has w = 1, and a vector has w = 0
\[\begin{bmatrix} x1 \\ y1 \\ z1 \\ 1 \end{bmatrix} - \begin{bmatrix} x0 \\ y0 \\ z0 \\ 1 \end{bmatrix} = \begin{bmatrix} x1 \\ y1 \\ z1 \\ 0 \end{bmatrix}\]Like magicth.
Next related
Day 6, Shadows, Light and Magic
More than just hit detection, we’ve moved into the realm of reflection, lighting and shading. This is mainly about projecting vectors on other vectors, using dot products \(\overrightarrow{v1} \cdot \overrightarrow{v2}\) to measure the length of the projection. For that we need to find normal vectors of surfaces, and a light source.
Previous related
Day 4, Intersections
More math heavy stuff, calculating the positions along a ray, where it intersects a sphere. Turns out this is as simple as solving a quadratic equation, with some special coefficients.
Day 3, Casting rays into spheres
Finally some practical applications of the math heavy weekend, casting abstract rays at abstract spheres. Well, on a sliding scale from linear algebra to physical representation, it’s still a step in the direction.
Day 2, Matrices, matrices, matrices. Oh, and more matrices
Today was spent on a heavy chunk of linear algebra, matrices. This is the foundation for everything else, so time well spent reimplementing what’s already in the stdlib, just to refresh my math skills. If there are still days left of Devember when I’m finished, I’ll come back and replace all this with the stdlib implementation instead, hoping I’m somewhat close to the same API.