Angle Between Vectors
"How many degrees is the target off to the side?" is a question for the dot product, acos, and (when you need a direction) the cross product. This lesson also shows the numeric traps hiding in acos.
Unsigned angle with acos
Rearranging a · b = |a| |b| cos θ gives:
θ = acos( (a · b) / (|a| * |b|) )The result is between 0 and π (0° to 180°). It tells you how far apart two directions are, but not which side.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <numbers>
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; }
double length(Vec3 v) { return std::sqrt(dot(v, v)); }
double angleBetween(Vec3 a, Vec3 b) {
double denom = length(a) * length(b);
if (denom < 1e-12) return 0; // zero vector: no angle
double c = std::clamp(dot(a, b) / denom, -1.0, 1.0); // keep acos in its domain
return std::acos(c);
}
int main() {
std::cout << std::fixed << std::setprecision(2);
const double toDeg = 180 / std::numbers::pi;
std::cout << "x vs y: " << angleBetween({1, 0, 0}, {0, 1, 0}) * toDeg << "\n";
std::cout << "x vs (1,1,0): " << angleBetween({1, 0, 0}, {1, 1, 0}) * toDeg << "\n";
std::cout << "x vs -x: " << angleBetween({1, 0, 0}, {-3, 0, 0}) * toDeg << "\n";
std::cout << "(1,2,2) vs (2,1,-2): " << angleBetween({1, 2, 2}, {2, 1, -2}) * toDeg << "\n";
}x vs y: 90.00
x vs (1,1,0): 45.00
x vs -x: 180.00
(1,2,2) vs (2,1,-2): 90.00
Why the clamp matters
Floating-point rounding can make the cosine of two nearly identical unit vectors come out as 1.0000000000000002. acos of anything above 1 is NaN, and NaN spreads through every later calculation silently: a camera with a NaN rotation simply disappears. Clamping to [-1, 1] costs nothing and removes the problem.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
int main() {
std::cout << std::boolalpha << std::setprecision(9);
// Normalize a direction in float, then dot it with itself.
float x = 0.1f, y = 0.5f, z = 0.2f;
float len = std::sqrt(x * x + y * y + z * z);
x /= len; y /= len; z /= len;
float d = x * x + y * y + z * z;
std::cout << "dot of a 'unit' vector with itself: " << d << "\n";
std::cout << "acos(d) is NaN: " << std::isnan(std::acos(d)) << "\n";
std::cout << "acos(clamp(d)) is NaN: " << std::isnan(std::acos(std::clamp(d, -1.0f, 1.0f))) << "\n";
}dot of a 'unit' vector with itself: 1.00000024
acos(d) is NaN: true
acos(clamp(d)) is NaN: false
The vector was normalized, yet its length squared came out slightly above 1, and the unclamped acos is NaN. Which numbers trigger it depends on the inputs, which is exactly why you always clamp.
Signed angle in 2D
For a turn direction, combine the two products. In 2D, atan2(cross2(a, b), dot(a, b)) gives the signed angle from a to b in (-π, π], positive counter-clockwise. It needs no normalizing, no clamp, and it stays accurate for tiny angles where acos is imprecise.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Vec2 { double x, y; };
double dot(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; }
double signedAngle(Vec2 from, Vec2 to) { return std::atan2(cross2(from, to), dot(from, to)); }
int main() {
std::cout << std::fixed << std::setprecision(1);
const double toDeg = 180 / std::numbers::pi;
Vec2 forward{0, 1};
Vec2 tests[] = {{1, 1}, {-1, 1}, {1, -3}, {-2, -2}, {3, 0}};
for (Vec2 t : tests)
std::cout << "to (" << t.x << ", " << t.y << "): " << std::setw(6) << signedAngle(forward, t) * toDeg << " deg\n";
}to (1.0, 1.0): -45.0 deg
to (-1.0, 1.0): 45.0 deg
to (1.0, -3.0): -161.6 deg
to (-2.0, -2.0): 135.0 deg
to (3.0, 0.0): -90.0 deg
Signed angle in 3D
In 3D, "left" or "right" only makes sense relative to an axis, usually the world up. The cross product of the two directions points along that axis (or against it); its dot with the axis gives the sign. Unity's Vector3.SignedAngle(from, to, axis) works this way.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
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; }
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(dot(v, v)); }
double signedAngle(Vec3 from, Vec3 to, Vec3 axis) {
Vec3 c = cross(from, to);
double unsignedAngle = std::atan2(length(c), dot(from, to));
return dot(c, axis) < 0 ? -unsignedAngle : unsignedAngle;
}
int main() {
std::cout << std::fixed << std::setprecision(1);
const double toDeg = 180 / std::numbers::pi;
Vec3 up{0, 1, 0};
Vec3 forward{0, 0, -1}; // right-handed, Y-up: forward is -Z
Vec3 left{-1, 0, 0}, right{1, 0, 0}, frontRight{1, 0.5, -1};
std::cout << "to left: " << signedAngle(forward, left, up) * toDeg << "\n";
std::cout << "to right: " << signedAngle(forward, right, up) * toDeg << "\n";
std::cout << "to front-right: " << signedAngle(forward, frontRight, up) * toDeg << "\n";
}to left: 90.0
to right: -90.0
to front-right: -48.2
Positive means counter-clockwise when you look down the axis from above, which in our right-handed Y-up world is a turn to the left. Note that frontRight also points a little upward, so its angle includes that tilt. To get a pure yaw angle, project both vectors onto the ground plane first (the Projection lesson).
Tip: Prefer comparing dot products to thresholds (
dot > cos(30°)) over computing angles withacosand comparing degrees. It is faster, has no NaN risk, and reads just as well once you are used to it.
Test yourself
What range does acos return?
- -π to π
- 0 to π
- 0 to 2π
- -1 to 1
Why clamp the cosine to [-1, 1] before calling acos?
- To convert to degrees
- Rounding can push it slightly outside, and acos then returns NaN
- acos only accepts integers
- To make the angle signed
Which expression gives the signed 2D angle from a to b?
- acos(dot(a, b))
- atan2(cross2(a, b), dot(a, b))
- atan(dot(a, b))
- cross2(a, b) / dot(a, b)