TExcelTAD
Home › Math for Games › Projection and Reflection

Projection and Reflection

Two formulas built from the dot product show up constantly in gameplay code: projecting a vector onto a direction (and its partner, removing that part) and reflecting a vector off a surface. Sliding along walls, bouncing balls, ricochets and keeping a character glued to a slope are all one or two lines once you know them.

Projection onto a direction

If n̂ is a unit vector, the part of v that points along n̂ is:

text
proj(v, n̂) = (v · n̂) * n̂

v · n̂ is how far v reaches along n̂ (a number); multiplying by n̂ turns it back into a vector. What is left over, v - proj(v, n̂), is the part of v perpendicular to n̂. Engines call this "project on plane" (Unity's Vector3.ProjectOnPlane, Unreal's FVector::VectorPlaneProject).

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; }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
    return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}

Vec3 project(Vec3 v, Vec3 n) { return n * dot(v, n); }          // n must be unit length
Vec3 projectOnPlane(Vec3 v, Vec3 n) { return v - project(v, n); }

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec3 velocity{3, -4, 1};
    Vec3 up{0, 1, 0};
    std::cout << "along up:     " << project(velocity, up) << "\n";
    std::cout << "on the floor: " << projectOnPlane(velocity, up) << "\n";

    // A diagonal direction works the same way.
    double s = 1 / std::sqrt(2.0);
    Vec3 diag{s, s, 0};
    std::cout << "along diag:   " << project(velocity, diag) << "\n";
    std::cout << "rest:         " << projectOnPlane(velocity, diag) << "\n";
}
along up:     (0.000, -4.000, 0.000)
on the floor: (3.000, 0.000, 1.000)
along diag:   (-0.500, -0.500, 0.000)
rest:         (3.500, -3.500, 1.000)

Sliding along a wall

When a character walks into a wall, you don't want it to stop dead: you want it to slide. Remove the part of the movement that goes into the wall (along the wall's normal) and keep the rest. Only remove it if the movement actually points into the wall (dot < 0), or the character would get sucked toward walls it is walking away from.

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

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

Vec2 slide(Vec2 move, Vec2 wallNormal) {
    double into = dot(move, wallNormal);
    if (into >= 0) return move;                 // moving away from the wall: keep everything
    return move - wallNormal * into;            // drop the part that goes into the wall
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 normal{-1, 0};                         // a wall on the right, facing left
    Vec2 moves[] = {{1, 1}, {2, 0}, {-1, 1}};
    for (Vec2 m : moves) {
        Vec2 r = slide(m, normal);
        std::cout << "move (" << m.x << ", " << m.y << ") -> (" << z0(r.x) << ", " << z0(r.y) << ")\n";
    }
}
move (1.000, 1.000) -> (0.000, 1.000)
move (2.000, 0.000) -> (0.000, 0.000)
move (-1.000, 1.000) -> (-1.000, 1.000)

Reflection

A ball bouncing off a surface keeps the part of its velocity that runs along the surface and flips the part that goes into it:

text
reflect(v, n̂) = v - 2 * (v · n̂) * n̂

Real bounces lose energy, so games usually scale the flipped part by a restitution factor e between 0 (no bounce) and 1 (perfect bounce): v - (1 + e) * (v · n̂) * n̂.

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

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

Vec2 reflect(Vec2 v, Vec2 n) { return v - n * (2 * dot(v, n)); }
Vec2 bounce(Vec2 v, Vec2 n, double e) { return v - n * ((1 + e) * dot(v, n)); }

void show(const char* label, Vec2 v) {
    std::cout << label << "(" << z0(v.x) << ", " << z0(v.y) << ")\n";
}

int main() {
    std::cout << std::fixed << std::setprecision(3);
    Vec2 floorN{0, 1};
    Vec2 v{3, -4};
    show("incoming       ", v);
    show("reflect        ", reflect(v, floorN));
    show("bounce e=0.5   ", bounce(v, floorN, 0.5));
    show("bounce e=0     ", bounce(v, floorN, 0.0));

    // A 45-degree ramp: normal (-1, 1) normalized.
    double s = 1 / std::sqrt(2.0);
    show("off 45 ramp    ", reflect({4, 0}, {-s, s}));
}
incoming       (3.000, -4.000)
reflect        (3.000, 4.000)
bounce e=0.5   (3.000, 2.000)
bounce e=0     (3.000, 0.000)
off 45 ramp    (0.000, 4.000)

With e = 0 the ball keeps only its sliding speed: that is the wall-slide formula again. A ball moving right at 4 m/s that hits a 45° ramp leaves going straight up, as you would expect from a mirror.

Tip: Both formulas assume n̂ is unit length. If you pass the raw cross product of two triangle edges, the result is scaled by the square of its length, and objects will fly off at absurd speeds. Normalize normals once when you build the mesh or the collider.

Test yourself

With unit normal n, what does v - n * dot(v, n) give you?

  1. The reflection of v
  2. The part of v that lies in the plane (perpendicular to n)
  3. The length of v
  4. The part of v along n

What is reflect((2, -3), (0, 1))?

  1. (2, 3)
  2. (-2, -3)
  3. (-2, 3)
  4. (2, -3)

Why does the wall-slide function check dot(move, normal) < 0 first?

  1. To avoid dividing by zero
  2. So movement away from the wall is left unchanged
  3. Because dot products are always negative
  4. To normalize the move vector