TExcelTAD
Home › Math for Games › Ray–Plane

Ray–Plane

A ray is a starting point and a direction: P(t) = origin + dir * t, for t >= 0. Every "raycast" in an engine answers one question: for which t does the ray first touch something? This lesson starts with the simplest target, an infinite plane: the ground under the mouse cursor, a water surface, a mirror, a wall.

Describing a plane

A plane is a unit normal n̂ plus a distance d from the origin: the points p with n̂ · p = d. Equivalently, a normal and any point q on the plane, with d = n̂ · q. The signed distance of any point to the plane is n̂ · p - d: positive on the side the normal points to, negative behind.

cpp · try it yourself
#include <iostream>
#include <iomanip>

struct Vec3 { double x, y, z; };
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }

struct Plane { Vec3 n; double d; };      // n must be unit length

double signedDistance(const Plane& p, Vec3 point) { return dot(p.n, point) - p.d; }

int main() {
    std::cout << std::fixed << std::setprecision(2);
    Plane water{{0, 1, 0}, 2.5};          // horizontal plane at height y = 2.5
    Vec3 things[] = {{0, 4, 0}, {3, 2.5, -7}, {1, 1, 1}};
    for (Vec3 t : things) {
        double s = signedDistance(water, t);
        std::cout << "y=" << t.y << ": signed distance " << std::setw(5) << s
                  << (s > 0 ? "  above" : s < 0 ? "  underwater" : "  on the surface") << "\n";
    }
}
y=4.00: signed distance  1.50  above
y=2.50: signed distance  0.00  on the surface
y=1.00: signed distance -1.50  underwater

The signed distance alone is useful: "is the player underwater?", "which side of the portal is the camera on?", and the frustum culling test (a sphere is outside if its signed distance to any frustum plane is less than -radius).

Ray–plane intersection

Substitute the ray into the plane equation and solve for t:

text
n̂ · (origin + dir * t) = d
t = (d - n̂ · origin) / (n̂ · dir)

Two special cases: if n̂ · dir is (nearly) zero, the ray runs parallel to the plane and never hits it; if t < 0, the plane is behind the ray's origin.

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>
#include <optional>

struct Vec3 { double x, y, z; };
Vec3 operator+(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
Vec3 operator*(Vec3 v, double s) { return {v.x * s, v.y * s, v.z * s}; }
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
struct Plane { Vec3 n; double d; };
struct Ray { Vec3 origin, dir; };

std::optional<double> rayPlane(const Ray& r, const Plane& p) {
    double denom = dot(p.n, r.dir);
    if (std::fabs(denom) < 1e-9) return std::nullopt;        // parallel
    double t = (p.d - dot(p.n, r.origin)) / denom;
    if (t < 0) return std::nullopt;                           // behind the origin
    return t;
}

double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Plane ground{{0, 1, 0}, 0};
    Ray rays[] = {
        {{0, 10, 10}, {0, -0.6, -0.8}},     // a camera looking down at the ground
        {{0, 10, 10}, {0, 0, -1}},          // looking straight ahead: parallel
        {{0, 10, 10}, {0, 0.6, -0.8}},      // looking up at the sky
    };
    for (const Ray& r : rays) {
        auto t = rayPlane(r, ground);
        if (!t) { std::cout << "no hit\n"; continue; }
        Vec3 h = r.origin + r.dir * *t;
        std::cout << "hit at t=" << *t << ": (" << z0(h.x) << ", " << z0(h.y) << ", " << z0(h.z) << ")\n";
    }
}
hit at t=16.667: (0.000, 0.000, -3.333)
no hit
no hit

Because dir is unit length here, t is the actual distance traveled along the ray. Engines' raycasts usually return that distance alongside the hit point and normal.

Placing objects on a slope

The ground in a strategy game is often a tilted plane per terrain triangle. The same function works for any normal. Here the build cursor follows the mouse ray onto a ramp, and the building is aligned with the slope's normal:

cpp · try it yourself
#include <iostream>
#include <iomanip>
#include <cmath>

struct Vec3 { double x, y, z; };
Vec3 operator+(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
Vec3 operator*(Vec3 v, double s) { return {v.x * s, v.y * s, v.z * s}; }
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
Vec3 normalize(Vec3 v) { return v * (1 / std::sqrt(dot(v, v))); }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec3 n = normalize({0, 2, 1});           // a ramp rising toward -Z
    Vec3 pointOnRamp{0, 0, 0};
    double d = dot(n, pointOnRamp);

    Vec3 origin{2, 20, 5}, dir = normalize({0, -1, -0.3});
    double t = (d - dot(n, origin)) / dot(n, dir);
    Vec3 hit = origin + dir * t;
    std::cout << "cursor on ramp at (" << z0(hit.x) << ", " << z0(hit.y) << ", " << z0(hit.z) << ")\n";
    std::cout << "check: signed distance " << z0(dot(n, hit) - d) << "\n";
    std::cout << "slope angle " << std::acos(n.y) * 180 / 3.141592653589793 << " deg (too steep to build if > 30)\n";
}
cursor on ramp at (2.000, 0.435, -0.870)
check: signed distance 0.000
slope angle 26.565 deg (too steep to build if > 30)

acos(n.y) is the slope angle, because n.y = n̂ · up. Character controllers use the same test to decide whether a surface is walkable ground or a wall (Unity's CharacterController.slopeLimit, Unreal's WalkableFloorAngle, Godot's floor_max_angle).

Tip: Engines also offer an analytic Plane type: Unity's Plane.Raycast(ray, out float enter) is a great way to get "where is the mouse on the ground?" without needing a collider on the ground at all.

Test yourself

What does dot(n, dir) == 0 mean for a ray–plane test?

  1. The ray hits at t = 0
  2. The ray is parallel to the plane
  3. The ray starts on the plane
  4. The plane is behind the ray

A plane has unit normal (0, 1, 0) and d = 3. What is the signed distance of (5, 1, 2)?

  1. -2
  2. 2
  3. 1
  4. 3

How do you get the slope angle of a surface with unit normal n in a Y-up world?

  1. atan2(n.x, n.z)
  2. acos(n.y)
  3. n.y * 90
  4. asin(n.x)