How do you represent a plane with a normal and an offset, and what does a point's signed distance to that plane tell you?
answer
- one vector and one scalar
- offset from any point on it
- dot with the normal, minus the offset
- sign picks the side
basics
~20 sA plane is a unit normal n and an offset d, holding every point x with dot(n, x) = d. The signed distance dot(n, p) - d tells which side p is on and, with a unit normal, how far away it is.
solid answer
~50 sI store a plane as a normal `n` and a scalar `d`: the plane is every `x` with `dot(n, x) = d`, and from any point `a` on it `d = dot(n, a)`. From a triangle I take `n = normalize(cross(b - a, c - a))`, so the vertex winding decides which way it faces. The signed distance `s = dot(n, p) - d` is positive on the side the normal points to, negative behind and zero on the plane; with a unit normal it is the true distance, with an unnormalised one it is scaled by `|n|`, which still gives correct side tests. On top of it I build the projection `p - s*n`, segment-crossing tests from the signs at both ends, and ray-plane intersection `t = (d - dot(n, o)) / dot(n, dir)` with a guard for a near-zero denominator.
go deeper
Recall the form dot(n, x) = d, how to get d from a point on the plane, and that the sign of dot(n, p) - d says which side p is on.
Build the plane from a triangle with a cross product, explain how winding sets the normal's direction, and derive ray-plane and segment-crossing tests from signed distance.
Show where plane conventions bite in a shipped codebase: mixed offset sign conventions, unnormalised planes feeding distance thresholds, and parallel-ray guards that need a tolerance rather than an exact zero test.
Weigh agreeing one plane convention and one winding across physics, rendering and gameplay code, since a subsystem that disagrees can silently flip front and back.
## The plane equation A **plane** in 3D is the set of points that sit at the same "height" along one direction. That direction is the plane's **normal** `n`, a vector perpendicular to the plane. The height is a single scalar, the **offset** `d`. Together they give the plane equation: `dot(n, x) = d` Every point `x` that satisfies it lies on the plane. If you know any point `a` on the plane, the offset is simply `d = dot(n, a)`. Some code writes the equation as `dot(n, x) + d = 0` instead; that only flips the sign of the stored offset, so pick one form for the whole codebase and state it. When `n` has unit length, `|d|` is the distance from the world origin to the plane. When it does not, `|d|` is that distance multiplied by `|n|`, which is why a plane is usually **normalised** by dividing both `n` and `d` by `|n|`. ## Building a plane from a triangle Game geometry usually hands you three vertices `a`, `b`, `c` rather than a normal. The recipe is: 1. Take two edges: `e1 = b - a` and `e2 = c - a`. 2. Take their cross product: `n = cross(e1, e2)`. It is perpendicular to both edges and so to the plane. 3. Normalise it, unless the triangle is degenerate (zero area gives a zero-length cross product, which cannot be normalised). 4. Set `d = dot(n, a)`. The **winding order** matters. In right-handed coordinates, `cross(b - a, c - a)` points toward a viewer who sees `a`, `b`, `c` counter-clockwise; in left-handed coordinates the same formula points toward a viewer who sees them clockwise. Swap two vertices and the normal flips, which flips the sign of every distance computed against the plane. ## Signed distance and what it tells you For a point `p`, the **signed distance** is: `s = dot(n, p) - d` - `s > 0`: `p` is on the side the normal points to, often called the front or positive half-space. - `s < 0`: `p` is behind the plane. - `s = 0`: `p` lies on the plane (in floating point, within a small tolerance). - `|s|` is the true perpendicular distance only when `n` is unit length; otherwise it is scaled by `|n|`. The sign is still correct, so a pure side test does not need a normalised plane. A useful mental picture is altitude: the plane is sea level, the normal points up, and `s` is the height above or below it. ## What you build on top of it Signed distance is the primitive almost every other plane query reuses: - **Projecting a point onto the plane:** `p - s*n` with a unit normal, the foot of the perpendicular. - **Does a segment cross the plane?** Compute `s0` and `s1` at its two endpoints. Opposite signs mean it crosses, at the parameter `t = s0 / (s0 - s1)` along the segment. - **Ray versus plane:** for a ray `o + t*dir`, solve `dot(n, o + t*dir) = d`, giving `t = (d - dot(n, o)) / dot(n, dir)`. A denominator near zero means the ray runs parallel to the plane; a negative `t` means the plane is behind the ray's origin. The sign of `dot(n, dir)` tells you whether the ray strikes the front or the back. - **Frustum and box tests:** a view frustum is six planes with normals pointing inward, and testing a bounding volume against it starts from the signed distance of the volume's centre to each plane. ## Common traps | Trap | Consequence | |---|---| | Normal never normalised | Distances are off by a factor of `|n|`; side tests still work | | Vertex winding reversed | The normal flips, so "front" and "behind" swap | | Mixing `dot(n, x) = d` and `dot(n, x) + d = 0` | The offset's sign is wrong and every distance shifts | | No guard on `dot(n, dir)` | Division by a near-zero value for rays parallel to the plane | A hitscan shot against a flat floor is the simplest case of all of this: compute `t` from the ray-plane formula, reject it if it is negative or beyond the weapon's range, and the hit point is `o + t*dir`.
- How do you intersect a ray with that plane?Substitute the ray `o + t*dir` into `dot(n, x) = d` to get `t = (d - dot(n, o)) / dot(n, dir)`. If `dot(n, dir)` is near zero the ray is parallel and either misses or lies in the plane. A negative `t` means the plane is behind the origin. The sign of `dot(n, dir)` says whether the ray approaches from the front (negative) or from behind (positive).
- How do you tell where a segment crosses the plane?Take the signed distances `s0` and `s1` of its endpoints. If they have the same sign it does not cross. If they differ, the crossing is at `t = s0 / (s0 - s1)` along the segment from the first endpoint, because the signed distance varies linearly along it and reaches zero there.
- How do you normalise a plane stored as an unnormalised (n, d)?Divide both `n` and `d` by `|n|`. Dividing only the normal moves the plane, because the equation `dot(n, x) = d` must be scaled on both sides to describe the same set of points.
Signed distance works like altitude measured from sea level: the plane is the sea surface, the normal points up, positive values are above the water, negative values are below it, and zero is the shoreline itself.
- The offset d is the distance from the origin even when the normal is not unit length.
- A negative signed distance means the calculation went wrong.
- The cross product of two edges points the same way whatever the vertex order.
- Ray-plane intersection never needs a guard on the denominator.
- Normalising a plane means dividing only its normal by its length.