Cross Product
The cross product of two 3D vectors returns a new vector that is perpendicular to both. Games use it to build surface normals, to build a camera's right and up axes, and (in its 2D form) to tell whether something is to your left or right.
a × b = (ay*bz - az*by,
az*bx - ax*bz,
ax*by - ay*bx)
|a × b| = |a| * |b| * sin(θ)Unlike the dot product, the order matters: b × a = -(a × b).
Direction: the right-hand rule
In a right-handed coordinate system, point your right hand's fingers along a, curl them toward b, and your thumb points along a × b. So X × Y = Z, Y × Z = X and Z × X = Y.
#include <iostream>
#include <iomanip>
struct Vec3 { double x, y, z; };
Vec3 cross(Vec3 a, Vec3 b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
double dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << v.x << ", " << v.y << ", " << v.z << ")";
}
int main() {
Vec3 X{1, 0, 0}, Y{0, 1, 0}, Z{0, 0, 1};
std::cout << "X x Y = " << cross(X, Y) << "\n";
std::cout << "Y x X = " << cross(Y, X) << "\n";
std::cout << "Y x Z = " << cross(Y, Z) << "\n";
Vec3 a{2, 1, 0}, b{0, 1, 3};
Vec3 c = cross(a, b);
std::cout << "a x b = " << c << "\n";
std::cout << "c . a = " << dot(c, a) << ", c . b = " << dot(c, b) << " (perpendicular)\n";
}X x Y = (0, 0, 1)
Y x X = (0, 0, -1)
Y x Z = (1, 0, 0)
a x b = (3, -6, 2)
c . a = 0, c . b = 0 (perpendicular)
Note: The formula is the same in left-handed engines such as Unity and Unreal; only the picture changes, because their axes are arranged differently. In Unity
Vector3.Cross(Vector3.right, Vector3.up)still returns(0, 0, 1), but there+Zpoints away from the viewer, so you would use your left hand to picture it.
Triangle normals
A triangle A, B, C has two edges B - A and C - A. Their cross product is perpendicular to the triangle: its normal. Its length is twice the triangle's area, which is handy too. The winding order (A, B, C counter-clockwise or clockwise as seen from the camera) decides which side the normal points to, which is why renderers use winding to decide what is the "front face".
#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 cross(Vec3 a, Vec3 b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
double length(Vec3 v) { return std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z); }
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; } // print 0.000, never -0.000
std::ostream& operator<<(std::ostream& os, Vec3 v) {
return os << "(" << z0(v.x) << ", " << z0(v.y) << ", " << z0(v.z) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
// A ramp: a triangle tilted up toward -Z.
Vec3 A{0, 0, 0}, B{2, 0, 0}, C{0, 1, -2};
Vec3 n = cross(B - A, C - A);
double len = length(n);
std::cout << "raw normal " << n << "\n";
std::cout << "unit normal " << Vec3{n.x / len, n.y / len, n.z / len} << "\n";
std::cout << "area " << len * 0.5 << "\n";
Vec3 flipped = cross(C - A, B - A); // reversed winding
std::cout << "flipped " << flipped << "\n";
}raw normal (0.000, 4.000, 2.000)
unit normal (0.000, 0.894, 0.447)
area 2.236
flipped (0.000, -4.000, -2.000)
2D cross: left or right?
In 2D there is no third axis to point along, but the z component of the 3D cross product is still useful on its own:
cross2(a, b) = ax*by - ay*bxIt is positive when b is counter-clockwise from a (to the left, with y up), negative when it is clockwise (to the right), and zero when they are parallel. A homing missile uses its sign to decide which way to steer; its magnitude (|a| |b| sin θ) shrinks as the target lines up.
#include <iostream>
struct Vec2 { double x, y; };
Vec2 operator-(Vec2 a, Vec2 b) { return {a.x - b.x, a.y - b.y}; }
double cross2(Vec2 a, Vec2 b) { return a.x * b.y - a.y * b.x; }
const char* turnDirection(Vec2 pos, Vec2 forward, Vec2 target) {
double c = cross2(forward, target - pos);
if (c > 1e-9) return "turn left";
if (c < -1e-9) return "turn right";
return "straight ahead (or straight behind)";
}
int main() {
Vec2 missile{0, 0}, forward{1, 0};
Vec2 targets[] = {{5, 2}, {5, -2}, {8, 0}, {-3, 0}};
for (Vec2 t : targets)
std::cout << "target (" << t.x << ", " << t.y << "): " << turnDirection(missile, forward, t) << "\n";
}target (5, 2): turn left
target (5, -2): turn right
target (8, 0): straight ahead (or straight behind)
target (-3, 0): straight ahead (or straight behind)
The last case shows why the sign alone is not enough: a target directly behind also gives zero. Combine it with the dot product (dot < 0 means behind) to pick a side.
Building a coordinate frame
Given a forward direction and the world up (0, 1, 0), two cross products give a full set of perpendicular axes. This is how a camera's "look-at" works, and you will meet it again in the Look Rotation lesson.
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec3 { double x, y, z; };
Vec3 cross(Vec3 a, Vec3 b) {
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
Vec3 normalize(Vec3 v) { double l = std::sqrt(v.x * v.x + v.y * v.y + v.z * v.z); return {v.x / l, v.y / l, v.z / l}; }
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) << ")";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Vec3 forward = normalize({1, 0, -1}); // looking right and into the screen
Vec3 worldUp{0, 1, 0};
Vec3 right = normalize(cross(forward, worldUp));
Vec3 up = cross(right, forward); // already unit length
std::cout << "forward " << forward << "\n";
std::cout << "right " << right << "\n";
std::cout << "up " << up << "\n";
}forward (0.707, 0.000, -0.707)
right (0.707, 0.000, 0.707)
up (0.000, 1.000, 0.000)
If forward is parallel to worldUp (looking straight up or down), the first cross product is zero and the frame breaks. Cameras handle that case by choosing a different helper axis.
Test yourself
What is cross(a, b) perpendicular to?
- Only a
- Only b
- Both a and b
- Neither; it is parallel to a + b
How does cross(b, a) relate to cross(a, b)?
- They are equal
- It is the negation
- It is the normalized version
- It is always zero
In 2D with y up, cross2(forward, toTarget) > 0. Where is the target?
- To the right
- To the left
- Directly behind
- Directly ahead