Knowing that the ship is "on the upper side" of the strip is not enough to land: we need to know how high it is and above which point. Knowing that two trajectories pass each other without touching is not enough to avoid an accident: we need to know how far apart they pass. This lesson answers those questions with a single idea repeated in different settings: the distance from a point to an object is measured along the perpendicular path, and the foot of that perpendicular is the closest point, the projection. With the same tool—removing or flipping the component of a vector along a normal—we will also obtain the bounce of a ray off a surface and the sliding of an object along it, and the matrices that carry out those operations.
The code assumes that the functions from the previous lessons of the module (norm, distance, normalize, dot_product, cross_product, line, plane_from_points, plane_from_point_normal) and, from Module 2, affine_matrix and transform_point are already defined. Remember that a plane is a pair (normal, d) with a unit normal.
Signed distance from a point to a plane
Take a plane with unit normal \(\hat{\mathbf{n}}\) that passes through \(\mathbf{p}_0\), and any point \(\mathbf{p}\). The displacement \(\mathbf{p} - \mathbf{p}_0\) has one part parallel to the plane and another along the normal. Only the second moves the point away from the plane, and its size is the scalar projection from "Dot Product and Cross Product":
\[ \text{dist}(\mathbf{p}) = \hat{\mathbf{n}} \cdot (\mathbf{p} - \mathbf{p}_0) = \hat{\mathbf{n}} \cdot \mathbf{p} + d \]
It is exactly the expression whose sign we were using in side_of_plane. With the normalized equation, evaluating the plane at a point gives its signed distance: the absolute value is the distance, and the sign, the side. With an unnormalized equation we have to divide:
\[ \text{dist}(\mathbf{p}) = \frac{A x + B y + C z + D}{\sqrt{A^2 + B^2 + C^2}} \]
Example. Height of the ship \((8, 3, 6)\) above the strip \(x + 2y + 2z - 17 = 0\):
\[ \text{dist} = \frac{8 + 2 \cdot 3 + 2 \cdot 6 - 17}{\sqrt{1 + 4 + 4}} = \frac{9}{3} = 3 \]
The ship is 3 m above the strip, measured perpendicular to it. For the base of the lighthouse we get \(-17/3 \approx -5.67\): 5.67 m away, on the other side. This also clarifies the meaning of \(d\) in the normalized equation: it is the signed distance from the origin to the plane.
Keeping the sign is useful: a signed distance that goes from positive to negative between two frames indicates that the object has crossed the plane.
Projection and reflection of a point with respect to a plane
The point of the plane closest to \(\mathbf{p}\) is its orthogonal projection: we reach it by going back, along the normal, exactly the distance that separates us from the plane.
\[ \mathbf{p}_{\text{proj}} = \mathbf{p} - \text{dist}(\mathbf{p}),\hat{\mathbf{n}} \]
If instead of stopping at the plane we travel twice as far, we arrive at the symmetric point, the reflection of \(\mathbf{p}\) (its image in a mirror placed in the plane):
\[ \mathbf{p}_{\text{refl}} = \mathbf{p} - 2,\text{dist}(\mathbf{p}),\hat{\mathbf{n}} \]
Example. For the ship, with \(\text{dist} = 3\) and \(\hat{\mathbf{n}} = \left(\tfrac{1}{3}, \tfrac{2}{3}, \tfrac{2}{3}\right)\): the vector \(3,\hat{\mathbf{n}}\) is \((1, 2, 2)\).
- Projection: \((8, 3, 6) - (1, 2, 2) = (7, 1, 4)\). Check: \(7 + 2 + 8 = 17\), it is in the plane. It is the point of the strip located "directly below" the ship, where its shadow would be drawn with a light perpendicular to the strip.
- Reflection: \((8, 3, 6) - (2, 4, 4) = (6, -1, 2)\). Its signed distance is \((6 - 2 + 4 - 17)/3 = -3\): the same, on the other side.
import numpy as np
def point_plane_distance(plane, p):
"""Signed distance: positive on the normal's side."""
n, d = plane
return dot_product(n, p) + d
def project_onto_plane(plane, p):
n, d = plane
return np.asarray(p, dtype=float) - point_plane_distance(plane, p) * n
def reflect_point(plane, p):
n, d = plane
return np.asarray(p, dtype=float) - 2.0 * point_plane_distance(plane, p) * n
ship = np.array([8.0, 3.0, 6.0])
strip = plane_from_points([13.0, 0.0, 2.0], [9.0, 4.0, 0.0], [9.0, 0.0, 4.0])
print(round(point_plane_distance(strip, ship), 6), round(point_plane_distance(strip, [0.0, 0.0, 0.0]), 4))
print(np.round(project_onto_plane(strip, ship), 6), np.round(reflect_point(strip, ship), 6))Closest point on a line
Take the line \(\mathbf{p}_0 + t,\mathbf{v}\) and a point \(\mathbf{q}\) off it. The point of the line closest to \(\mathbf{q}\) is the one at which the segment joining them is perpendicular to the line. And finding it is, once again, a projection: we project the displacement \(\mathbf{q} - \mathbf{p}_0\) onto the direction \(\mathbf{v}\).
\[ t = \frac{(\mathbf{q} - \mathbf{p}_0) \cdot \mathbf{v}}{\mathbf{v} \cdot \mathbf{v}} \qquad\qquad \mathbf{c} = \mathbf{p}_0 + t,\mathbf{v} \qquad\qquad \text{dist} = \lVert \mathbf{q} - \mathbf{c} \rVert \]
The parameter \(t\) says where, along the line, the foot of the perpendicular is; the distance is the norm of the perpendicular part of the parallel/perpendicular decomposition.
Example. How close will the ship, with trajectory \((8, 3, 6) + t,(2, 0, -1)\), pass to beacon Alpha \((12, 7, 4)\)?
- \(\mathbf{q} - \mathbf{p}_0 = (4, 4, -2)\).
- \(t = \dfrac{4 \cdot 2 + 0 + (-2)(-1)}{4 + 0 + 1} = \dfrac{10}{5} = 2\).
- Closest point: \((8, 3, 6) + 2,(2, 0, -1) = (12, 3, 4)\).
- Distance: \(\lVert (12, 7, 4) - (12, 3, 4) \rVert = \lVert (0, 4, 0) \rVert = 4\).
In 2 s the ship will pass 4 m from the beacon, directly below it. Since \(\mathbf{v}\) is a velocity here, \(t\) is the instant of closest approach.
If only the distance matters, there is an alternative formula with the cross product. The parallelogram formed by \(\mathbf{q} - \mathbf{p}_0\) and \(\mathbf{v}\) has base \(\lVert \mathbf{v} \rVert\) and, as its height, the distance we are after, so:
\[ \text{dist} = \frac{\lVert (\mathbf{q} - \mathbf{p}_0) \times \mathbf{v} \rVert}{\lVert \mathbf{v} \rVert} \]
In the example: \((4, 4, -2) \times (2, 0, -1) = (-4, 0, -8)\), of norm \(\sqrt{80}\), and \(\sqrt{80}/\sqrt{5} = 4\).
Rays and segments: clamping the parameter
On a ray (\(t \ge 0\)) or a segment (\(0 \le t \le 1\)), the foot of the perpendicular may fall outside the valid stretch. In that case, the closest point is the nearer endpoint. The solution is as simple as it sounds: compute \(t\) as for the line and clamp it to the allowed range.
| Object | Parameter | Clamping |
|---|---|---|
| Line | Any | None |
| Ray | \(t \ge 0\) | \(t = \max(t, 0)\) |
| Segment \(A + t,(B - A)\) | \(0 \le t \le 1\) | \(t = \min(\max(t, 0), 1)\) |
Example 1: the tower. The lighthouse tower is the segment from \(A = (0, 0, 0)\) to \(B = (0, 12, 0)\). For the ship: \(t = \dfrac{(8, 3, 6) \cdot (0, 12, 0)}{144} = \dfrac{36}{144} = 0.25\), within the range. Closest point: \((0, 3, 0)\); distance: \(\lVert (8, 0, 6) \rVert = 10\). It is the cylindrical radius from "Coordinates and Planes".
Example 2: above the lamp. For a probe located at \((3, 16, 0)\): \(t = 192/144 \approx 1.33\), out of range. It is clamped to \(t = 1\): the closest point is the lamp \((0, 12, 0)\), and the distance, \(\lVert (3, 4, 0) \rVert = 5\). Without clamping we would have obtained 3, the distance to the imaginary extension of the tower.
Example 3: a ray. For the base of the lighthouse and the ship's future trajectory: \(t = \dfrac{(-8, -3, -6) \cdot (2, 0, -1)}{5} = -2\). Negative: the closest point of the complete line, \((4, 3, 8)\), has been left behind. On the ray, the closest point is the origin (\(t = 0\)): the ship will never again be as close to the lighthouse as it is now, \(\sqrt{109} \approx 10.44\) m away.
def closest_point_on_line(r, q):
"""Returns (t, point) for the point of the line closest to q."""
origin, direction = r
t = dot_product(np.asarray(q, dtype=float) - origin, direction) / dot_product(direction, direction)
return t, origin + t * direction
def point_line_distance(r, q):
t, closest = closest_point_on_line(r, q)
return distance(q, closest)
def closest_point_on_segment(a, b, q):
a = np.asarray(a, dtype=float)
ab = np.asarray(b, dtype=float) - a
t = dot_product(np.asarray(q, dtype=float) - a, ab) / dot_product(ab, ab)
t = min(max(t, 0.0), 1.0)
return a + t * ab
trajectory = line(ship, [2.0, 0.0, -1.0])
beacon_alpha = np.array([12.0, 7.0, 4.0])
lighthouse, lamp = np.array([0.0, 0.0, 0.0]), np.array([0.0, 12.0, 0.0])
print(closest_point_on_line(trajectory, beacon_alpha), point_line_distance(trajectory, beacon_alpha))
print(closest_point_on_segment(lighthouse, lamp, ship), closest_point_on_segment(lighthouse, lamp, [3.0, 16.0, 0.0]))
t, closest = closest_point_on_line(trajectory, lighthouse)
print(t, closest, round(distance(lighthouse, closest), 4))The distance from a point to a segment is the basis of collisions with "capsules" (a segment with thickness), a volume widely used for characters and elongated ships.
Distance between two lines
Skew lines
Two skew lines, \(\mathbf{p}_1 + t,\mathbf{u}\) and \(\mathbf{p}_2 + s,\mathbf{v}\), have a single common perpendicular: a segment that joins them and is perpendicular to both. Its length is the distance between them. There are two ways of computing it.
The distance only. The direction of the common perpendicular is \(\mathbf{u} \times \mathbf{v}\). The distance is the projection, onto that direction, of any vector joining the two lines, for example \(\mathbf{w} = \mathbf{p}_2 - \mathbf{p}_1\):
\[ \text{dist} = \frac{\lvert \mathbf{w} \cdot (\mathbf{u} \times \mathbf{v}) \rvert}{\lVert \mathbf{u} \times \mathbf{v} \rVert} \]
The numerator is the scalar triple product: if it is 0, the lines are coplanar and intersect, as we saw in the previous lesson.
The closest points as well. We look for \(t\) and \(s\) such that the segment joining the two points is perpendicular to \(\mathbf{u}\) and to \(\mathbf{v}\). With \(\mathbf{w}_0 = \mathbf{p}_1 - \mathbf{p}_2\), the two conditions give a 2×2 system:
\[ \begin{cases} (\mathbf{u} \cdot \mathbf{u}),t - (\mathbf{u} \cdot \mathbf{v}),s = -,\mathbf{u} \cdot \mathbf{w}_0 \ (\mathbf{u} \cdot \mathbf{v}),t - (\mathbf{v} \cdot \mathbf{v}),s = -,\mathbf{v} \cdot \mathbf{w}_0 \end{cases} \]
Example. The ship's trajectory, \((8, 3, 6) + t,(2, 0, -1)\), and the freighter's, \((6, 3, -2) + s,(2, 2, 1)\), which we classified as skew in the previous lesson.
- \(\mathbf{u} \times \mathbf{v} = (0 \cdot 1 - (-1) \cdot 2,\ (-1) \cdot 2 - 2 \cdot 1,\ 2 \cdot 2 - 0 \cdot 2) = (2, -4, 4)\), of norm 6.
- \(\mathbf{w} = (-2, 0, -8)\); \(\mathbf{w} \cdot (2, -4, 4) = -4 + 0 - 32 = -36\). Distance: \(36/6 = 6\).
- Points: \(\mathbf{u} \cdot \mathbf{u} = 5\), \(\mathbf{u} \cdot \mathbf{v} = 3\), \(\mathbf{v} \cdot \mathbf{v} = 9\), \(\mathbf{w}_0 = (2, 0, 8)\), \(\mathbf{u} \cdot \mathbf{w}_0 = -4\), \(\mathbf{v} \cdot \mathbf{w}_0 = 12\). The system \(5t - 3s = 4\), \(3t - 9s = -12\) has the solution \(t = 2\), \(s = 2\).
- Point on the ship's line: \((12, 3, 4)\). Point on the freighter's line: \((10, 7, 0)\). Their difference, \((2, -4, 4)\), has length 6 and is parallel to \(\mathbf{u} \times \mathbf{v}\), as it should be.
The two routes pass 6 m from each other. Careful: that is the distance between the routes, not necessarily between the vehicles, which would only be 6 m apart if they passed through those points at the same time.
Parallel lines
If \(\mathbf{u} \times \mathbf{v} = \mathbf{0}\), the formulas above divide by zero (the determinant of the system is \(\lVert \mathbf{u} \times \mathbf{v} \rVert^2\)). But the case is easier: the distance between two parallel lines is the distance from any point of one to the other, a point-line problem. The escort drone \((10, 3, 6)\) flies parallel to the ship: the separation between their routes is \(\sqrt{0.8} \approx 0.894\) m, even though the vehicles are 2 m apart.
def closest_points_between_lines(r1, r2, tolerance=1e-9):
"""Points of r1 and of r2 that are closest to each other."""
p1, u = r1
p2, v = r2
w0 = p1 - p2
a, b, c = dot_product(u, u), dot_product(u, v), dot_product(v, v)
d, e = dot_product(u, w0), dot_product(v, w0)
determinant = a * c - b * b
if determinant < tolerance: # parallel: t is fixed at 0
t, s = 0.0, e / c
else:
t = (b * e - c * d) / determinant
s = (a * e - b * d) / determinant
return p1 + t * u, p2 + s * v
def distance_between_lines(r1, r2):
q1, q2 = closest_points_between_lines(r1, r2)
return distance(q1, q2)
freighter_route = line([6.0, 3.0, -2.0], [2.0, 2.0, 1.0])
print(closest_points_between_lines(trajectory, freighter_route), distance_between_lines(trajectory, freighter_route))
print(round(distance_between_lines(trajectory, line([10.0, 3.0, 6.0], [2.0, 0.0, -1.0])), 4))For segments, \(t\) and \(s\) are clamped to \([0, 1]\) with a little more care (when one is clamped, the other has to be recomputed); the idea is the same.
Distance between parallel planes
Two parallel planes, written with the same unit normal, differ only in \(d\). Since \(d\) is the signed distance from the origin to each plane, the separation between them is:
\[ \text{dist} = \lvert d_1 - d_2 \rvert \]
The plane parallel to the strip that passes through the ship is \(\hat{\mathbf{n}} \cdot \mathbf{p} - \tfrac{26}{3} = 0\) (because \(\hat{\mathbf{n}} \cdot (8, 3, 6) = \tfrac{26}{3}\)). The distance to the strip is \(\left\lvert -\tfrac{26}{3} + \tfrac{17}{3} \right\rvert = 3\), as we already knew. If the normals are opposite, the sign of one of the equations has to be flipped first. And if the planes are not parallel, they intersect and the distance is 0.
Reflecting and sliding a vector
So far we have projected and reflected points. The same operations applied to directions (velocities, rays of light) are the ones that bring a scene to life. Given a surface with unit normal \(\hat{\mathbf{n}}\), any vector \(\mathbf{d}\) breaks down into its normal part, \((\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}}\), and its tangent part, the rest.
Sliding means removing the normal part and keeping the tangent one:
\[ \mathbf{d}_{\text{slide}} = \mathbf{d} - (\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}} \]
It is what a character does when walking into a wall: the part of its velocity that pushes it toward the wall is canceled and the rest moves it along the wall.
Reflecting means flipping the normal part and keeping the tangent one:
\[ \mathbf{r} = \mathbf{d} - 2,(\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}} \]
It is the law of the bounce: the outgoing angle equals the incoming one. It is used by a bouncing ball, by a projectile that ricochets and, as we will see in Module 5, by the light that produces the highlights on a polished surface. Reflection preserves the norm; sliding, in general, reduces it.
Example 1: the beam bouncing. The lighthouse beam reaches the strip with direction \(\mathbf{d} = \left(\tfrac{1}{9}, -\tfrac{8}{9}, \tfrac{4}{9}\right)\). \(\mathbf{d} \cdot \hat{\mathbf{n}} = \dfrac{1 - 16 + 8}{27} = -\dfrac{7}{27}\) (negative: the beam travels toward the surface).
\[ \mathbf{r} = \frac{(1, -8, 4)}{9} + \frac{14}{27} \cdot \frac{(1, 2, 2)}{3} = \frac{(9, -72, 36) + (14, 28, 28)}{81} = \frac{(23, -44, 64)}{81} \approx (0.284, -0.543, 0.790) \]
Checks: \(23^2 + 44^2 + 64^2 = 6561 = 81^2\), it is still a unit vector; and \(\mathbf{r} \cdot \hat{\mathbf{n}} = +\tfrac{7}{27}\), the normal component has changed sign.
Example 2: sliding. The ship touches the strip with velocity \((1, -1, -1)\). \(\mathbf{v} \cdot \hat{\mathbf{n}} = \tfrac{1 - 2 - 2}{3} = -1\). Sliding velocity: \((1, -1, -1) + \left(\tfrac{1}{3}, \tfrac{2}{3}, \tfrac{2}{3}\right) = \left(\tfrac{4}{3}, -\tfrac{1}{3}, -\tfrac{1}{3}\right)\). Its speed drops from \(\sqrt{3} \approx 1.73\) to \(\sqrt{2} \approx 1.41\) m/s: the difference is what the landing gear absorbs.
def reflect(d, n):
"""Reflects the direction d off a surface with unit normal n."""
d = np.asarray(d, dtype=float)
n = np.asarray(n, dtype=float)
return d - 2.0 * dot_product(d, n) * n
def slide(v, n):
"""Removes from v the component along the unit normal n."""
v = np.asarray(v, dtype=float)
n = np.asarray(n, dtype=float)
return v - dot_product(v, n) * n
n_strip = strip[0]
bounce = reflect(np.array([1.0, -8.0, 4.0]) / 9.0, n_strip)
print(np.round(bounce * 81.0, 6), round(norm(bounce), 6))
print(np.round(slide([1.0, -1.0, -1.0], n_strip), 4))Projection and reflection matrices
Sliding and reflecting are linear transformations of the vector \(\mathbf{d}\), so—as we established in Module 2—they have a matrix. The key is to write \((\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}}\) as \(\hat{\mathbf{n}},(\hat{\mathbf{n}}^T \mathbf{d}) = (\hat{\mathbf{n}}\hat{\mathbf{n}}^T),\mathbf{d}\), where \(\hat{\mathbf{n}}\hat{\mathbf{n}}^T\) is a 3×3 matrix (column times row), the outer product:
\[ P = I - \hat{\mathbf{n}}\hat{\mathbf{n}}^T \quad\text{(projection onto the plane)} \qquad\qquad R = I - 2,\hat{\mathbf{n}}\hat{\mathbf{n}}^T \quad\text{(reflection)} \]
For the strip, with \(\hat{\mathbf{n}}\hat{\mathbf{n}}^T = \dfrac{1}{9}\begin{pmatrix} 1 & 2 & 2 \ 2 & 4 & 4 \ 2 & 4 & 4 \end{pmatrix}\):
\[ P = \frac{1}{9}\begin{pmatrix} 8 & -2 & -2 \ -2 & 5 & -4 \ -2 & -4 & 5 \end{pmatrix} \qquad R = \frac{1}{9}\begin{pmatrix} 7 & -4 & -4 \ -4 & 1 & -8 \ -4 & -8 & 1 \end{pmatrix} \]
These matrices generalize those of Module 2: with \(\hat{\mathbf{n}} = (0, 1, 0)\) they give \(\text{diag}(1, 0, 1)\) and \(\text{diag}(1, -1, 1)\), the projection onto the XZ plane and the reflection across it that we saw in "Transformation Matrices". Their properties sum up what we have learned:
| Projection \(P\) | Reflection \(R\) | |
|---|---|---|
| Applied twice | \(P^2 = P\) (projecting what is already projected changes nothing) | \(R^2 = I\) (two reflections give back the original) |
| Determinant | 0: it flattens space, not invertible | \(-1\): it reverses orientation |
| Orthogonal? | No | Yes: it preserves lengths and angles |
| Eigenvalues | 1, 1 (the plane) and 0 (the normal) | 1, 1 (the plane) and \(-1\) (the normal) |
These 3×3 matrices act on directions, or on points if the plane passes through the origin. For an arbitrary plane, the point formulas \(\mathbf{p} - \text{dist},\hat{\mathbf{n}}\) and \(\mathbf{p} - 2,\text{dist},\hat{\mathbf{n}}\) add a constant term, that is, a translation: \(-d,\hat{\mathbf{n}}\) for the projection and \(-2d,\hat{\mathbf{n}}\) for the reflection. The result is a 4×4 affine matrix that can be composed with all the others.
def plane_projection_matrix(n):
n = normalize(n)
return np.eye(3) - np.outer(n, n)
def reflection_matrix(n):
n = normalize(n)
return np.eye(3) - 2.0 * np.outer(n, n)
p_strip = plane_projection_matrix(n_strip)
r_strip = reflection_matrix(n_strip)
print(np.round(r_strip * 9.0, 6))
print(np.allclose(p_strip @ p_strip, p_strip), np.allclose(r_strip @ r_strip, np.eye(3)),
round(float(np.linalg.det(p_strip)), 6), round(float(np.linalg.det(r_strip)), 6))
# 4x4 affine version for the plane of the strip, which does not pass through the origin
d_strip = strip[1]
strip_mirror = affine_matrix(r_strip, -2.0 * d_strip * n_strip)
strip_shadow = affine_matrix(p_strip, -d_strip * n_strip)
print(np.round(transform_point(strip_mirror, ship), 6), np.round(transform_point(strip_shadow, ship), 6))The matrices reproduce the points \((6, -1, 2)\) and \((7, 1, 4)\) obtained at the beginning of the lesson.
Summary of formulas
With unit normals and directions (\(\hat{\mathbf{n}}\), \(\hat{\mathbf{u}}\)) unless stated otherwise:
| Problem | Formula |
|---|---|
| Signed point-plane distance | \(\hat{\mathbf{n}} \cdot \mathbf{p} + d\) |
| Projection of a point onto a plane | \(\mathbf{p} - (\hat{\mathbf{n}} \cdot \mathbf{p} + d),\hat{\mathbf{n}}\) |
| Reflection of a point in a plane | \(\mathbf{p} - 2,(\hat{\mathbf{n}} \cdot \mathbf{p} + d),\hat{\mathbf{n}}\) |
| Point of a line closest to \(\mathbf{q}\) | \(\mathbf{p}_0 + t,\mathbf{v}\), with \(t = \dfrac{(\mathbf{q} - \mathbf{p}_0) \cdot \mathbf{v}}{\mathbf{v} \cdot \mathbf{v}}\) |
| The same on a ray or segment | The same \(t\), clamped to \(t \ge 0\) or to \([0, 1]\) |
| Point-line distance | \(\dfrac{\lVert (\mathbf{q} - \mathbf{p}_0) \times \mathbf{v} \rVert}{\lVert \mathbf{v} \rVert}\) |
| Distance between skew lines | \(\dfrac{\lvert \mathbf{w} \cdot (\mathbf{u} \times \mathbf{v}) \rvert}{\lVert \mathbf{u} \times \mathbf{v} \rVert}\) |
| Distance between parallel lines | Distance from a point of one to the other |
| Distance between parallel planes | \(\lvert d_1 - d_2 \rvert\) |
| Sliding a vector | \(\mathbf{d} - (\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}}\); matrix \(I - \hat{\mathbf{n}}\hat{\mathbf{n}}^T\) |
| Reflecting a vector | \(\mathbf{d} - 2,(\mathbf{d} \cdot \hat{\mathbf{n}}),\hat{\mathbf{n}}\); matrix \(I - 2,\hat{\mathbf{n}}\hat{\mathbf{n}}^T\) |
Common Mistakes and Tips
- Using unnormalized normals. Every formula with \(\hat{\mathbf{n}}\) gives wrong results (scaled distances, reflections that change length) if the normal does not have length 1. It is the most frequent bug of this lesson.
- Throwing the sign away too soon. The signed distance contains more information than its absolute value; hold on to the sign for as long as it may be useful.
- Not clamping the parameter on rays and segments: you get the distance to an extension that does not exist.
- Applying the skew-lines formula to parallel lines. It divides by zero. Detect parallelism first.
- Confusing the distance between trajectories with the distance between objects. Lines know nothing about time.
- Reflecting with the wrong convention. In \(\mathbf{r} = \mathbf{d} - 2(\mathbf{d} \cdot \hat{\mathbf{n}})\hat{\mathbf{n}}\), \(\mathbf{d}\) is the incoming direction (it points toward the surface). Some lighting texts use the vector that points toward the light, and the formula changes sign.
- Trying to invert a projection. Its determinant is 0: the lost information (the height above the plane) cannot be recovered.
- Tip. Faced with any distance problem, ask yourself: "which vector must be perpendicular to what?" That condition, written as a dot product equal to zero, gives the equation that has to be solved.
Exercises
Exercise 1. For beacon Alpha \((12, 7, 4)\) and the strip \(x + 2y + 2z - 17 = 0\): a) compute the signed distance; b) find its projection onto the strip; c) find its symmetric point.
Exercise 2. a) Find the point of the line of the ship's trajectory, \((8, 3, 6) + t,(2, 0, -1)\), closest to the camera \((0, 5, 20)\), and the distance. b) Repeat the calculation considering only the future trajectory (ray, \(t \ge 0\)). c) Which point of the lighthouse tower (segment from \((0,0,0)\) to \((0,12,0)\)) is closest to beacon Alpha?
Exercise 3. The ship fires a measuring laser straight down, in the direction \((0, -1, 0)\), which bounces off the strip. a) Compute the reflected direction. b) The ship's current velocity is \((2, 0, -1)\); what is the result of sliding it along the strip, and why?
Solutions
Solution 1.
a) \(\text{dist} = \dfrac{12 + 14 + 8 - 17}{3} = \dfrac{17}{3} \approx 5.667\): above the strip.
b) \(\text{dist},\hat{\mathbf{n}} = \tfrac{17}{9},(1, 2, 2) \approx (1.889, 3.778, 3.778)\). Projection: \((12, 7, 4) - (1.889, 3.778, 3.778) = (10.111, 3.222, 0.222)\). Check: \(10.111 + 6.444 + 0.444 = 17\).
c) Subtracting twice as much: \((12, 7, 4) - (3.778, 7.556, 7.556) = (8.222, -0.556, -3.556)\).
Solution 2.
a) \(\mathbf{q} - \mathbf{p}_0 = (-8, 2, 14)\); \(t = \dfrac{-16 + 0 - 14}{5} = -6\). Point: \((8, 3, 6) - 6,(2, 0, -1) = (-4, 3, 12)\). Distance: \(\lVert (0, 5, 20) - (-4, 3, 12) \rVert = \lVert (4, 2, 8) \rVert = \sqrt{84} \approx 9.165\).
b) \(t = -6 < 0\): it is clamped to \(t = 0\). The closest point is the ship's current position, and the distance, \(\lVert (-8, 2, 14) \rVert = \sqrt{264} \approx 16.248\). The ship is moving away from the camera.
c) \(t = \dfrac{(12, 7, 4) \cdot (0, 12, 0)}{144} = \dfrac{84}{144} \approx 0.583\), within \([0, 1]\). Point: \((0, 7, 0)\), \(\lVert (12, 0, 4) \rVert = \sqrt{160} \approx 12.65\) m away.
Solution 3.
a) \(\mathbf{d} \cdot \hat{\mathbf{n}} = -\tfrac{2}{3}\). \(\mathbf{r} = (0, -1, 0) + \tfrac{4}{3}\left(\tfrac{1}{3}, \tfrac{2}{3}, \tfrac{2}{3}\right) = \left(\tfrac{4}{9}, -\tfrac{1}{9}, \tfrac{8}{9}\right) \approx (0.444, -0.111, 0.889)\). Its norm: \(\sqrt{16 + 1 + 64}/9 = 1\).
b) \(\mathbf{v} \cdot \hat{\mathbf{n}} = \tfrac{2 + 0 - 2}{3} = 0\): the velocity has no normal component, so sliding it leaves it untouched, \((2, 0, -1)\). We already knew that the ship flies parallel to the strip.
camera = np.array([0.0, 5.0, 20.0])
print(np.round(project_onto_plane(strip, beacon_alpha), 3), np.round(reflect_point(strip, beacon_alpha), 3))
print(closest_point_on_line(trajectory, camera), closest_point_on_segment(lighthouse, lamp, beacon_alpha))
print(np.round(reflect([0.0, -1.0, 0.0], n_strip), 3), np.round(slide([2.0, 0.0, -1.0], n_strip), 6))[10.111 3.222 0.222] [ 8.222 -0.556 -3.556] (-6.0, array([-4., 3., 12.])) [0. 7. 0.] [ 0.444 -0.111 0.889] [ 2. 0. -1.]
Conclusion
Every distance in this lesson is obtained with the same recipe: look for the perpendicular. The expression \(\hat{\mathbf{n}} \cdot \mathbf{p} + d\) of a normalized plane is the signed distance from a point to the plane, and subtracting that distance along the normal gives its projection, or its reflection if we subtract twice as much. Projecting onto a direction finds the closest point of a line, and clamping the parameter, that of a ray or a segment; the scalar triple product gives the distance between skew lines. Removing or flipping the normal component of a vector produces sliding and bouncing, two linear operations with matrices \(I - \hat{\mathbf{n}}\hat{\mathbf{n}}^T\) and \(I - 2\hat{\mathbf{n}}\hat{\mathbf{n}}^T\). So we know how to measure how far we are from touching something. The question that remains is the opposite one, and the most common in a game: once a ray has been cast—a shot, the lighthouse beam, the camera's line of sight—does it hit an object, and where? We will see that in the last lesson of the module, "Intersections: Rays, Spheres, Triangles and Boxes".
