Look Rotation
"Make this object face that point" is the most common rotation request in gameplay code: turrets, cameras, characters turning toward a speaker, billboards facing the camera. Engines call it look rotation or look-at: Unity's Quaternion.LookRotation(forward, up) and transform.LookAt, Unreal's UKismetMathLibrary::FindLookAtRotation and FRotationMatrix::MakeFromX, Godot's look_at and Basis.looking_at, Three.js's Object3D.lookAt, GLM's glm::quatLookAt.
Building a basis from forward and up
A rotation is fully described by where it sends the three axes. Given a desired forward and an approximate up hint (usually world up), build a right-handed, orthonormal basis with two cross products, exactly as the Cross Product lesson did for a camera:
f = normalize(target - eye) forward
r = normalize(cross(f, upHint)) right
u = cross(r, f) true up (perpendicular to both)In this course's convention an object looks down its local -Z, so the rotation matrix has columns r, u, -f.
#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 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) { return v * (1 / std::sqrt(dot(v, v))); }
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) << ")"; }
struct Basis { Vec3 right, up, back; }; // the columns of the rotation matrix
Basis lookRotation(Vec3 forward, Vec3 upHint) {
Vec3 f = normalize(forward);
Vec3 r = normalize(cross(f, upHint));
Vec3 u = cross(r, f);
return {r, u, f * -1};
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Vec3 turret{0, 1, 0}, target{4, 3, -4};
Basis b = lookRotation(target - turret, {0, 1, 0});
std::cout << "right " << b.right << "\n";
std::cout << "up " << b.up << "\n";
std::cout << "back " << b.back << "\n";
std::cout << "checks: r.u=" << z0(dot(b.right, b.up)) << " r.b=" << z0(dot(b.right, b.back))
<< " u.b=" << z0(dot(b.up, b.back)) << "\n";
}right (0.707, 0.000, 0.707)
up (-0.236, 0.943, 0.236)
back (-0.667, -0.333, 0.667)
checks: r.u=0.000 r.b=0.000 u.b=0.000
The three dot products are zero: the axes are perpendicular, so this is a valid rotation (and each is unit length).
Matrix to quaternion
Engines store the result as a quaternion. Converting a rotation matrix to a quaternion uses the matrix's trace (the sum of the diagonal). The robust version picks the largest diagonal term to avoid dividing by a small number:
#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 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 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) { return v * (1 / std::sqrt(dot(v, v))); }
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) << ")"; }
struct Quat { double w, x, y, z; };
Vec3 rotate(Quat q, Vec3 v) { Vec3 u{q.x, q.y, q.z}; Vec3 t = cross(u, v) * 2; return v + t * q.w + cross(u, t); }
// m[row][col] of a rotation matrix -> unit quaternion
Quat fromMatrix(const double m[3][3]) {
double tr = m[0][0] + m[1][1] + m[2][2];
if (tr > 0) {
double s = std::sqrt(tr + 1) * 2;
return {s / 4, (m[2][1] - m[1][2]) / s, (m[0][2] - m[2][0]) / s, (m[1][0] - m[0][1]) / s};
} else if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) {
double s = std::sqrt(1 + m[0][0] - m[1][1] - m[2][2]) * 2;
return {(m[2][1] - m[1][2]) / s, s / 4, (m[0][1] + m[1][0]) / s, (m[0][2] + m[2][0]) / s};
} else if (m[1][1] > m[2][2]) {
double s = std::sqrt(1 + m[1][1] - m[0][0] - m[2][2]) * 2;
return {(m[0][2] - m[2][0]) / s, (m[0][1] + m[1][0]) / s, s / 4, (m[1][2] + m[2][1]) / s};
} else {
double s = std::sqrt(1 + m[2][2] - m[0][0] - m[1][1]) * 2;
return {(m[1][0] - m[0][1]) / s, (m[0][2] + m[2][0]) / s, (m[1][2] + m[2][1]) / s, s / 4};
}
}
Quat lookRotation(Vec3 forward, Vec3 upHint) {
Vec3 f = normalize(forward), r = normalize(cross(f, upHint)), u = cross(r, f);
double m[3][3] = {{r.x, u.x, -f.x}, {r.y, u.y, -f.y}, {r.z, u.z, -f.z}}; // columns r, u, -f
return fromMatrix(m);
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Vec3 dirs[] = {{1, 0, 0}, {0, 0, 1}, {4, 2, -4}};
for (Vec3 d : dirs) {
Quat q = lookRotation(d, {0, 1, 0});
std::cout << "look along " << d << ": q=[" << z0(q.w) << " " << z0(q.x) << " " << z0(q.y) << " " << z0(q.z)
<< "], local forward -> " << rotate(q, {0, 0, -1}) << "\n";
}
}look along (1.000, 0.000, 0.000): q=[0.707 0.000 -0.707 0.000], local forward -> (1.000, 0.000, 0.000)
look along (0.000, 0.000, 1.000): q=[0.000 0.000 1.000 0.000], local forward -> (0.000, 0.000, 1.000)
look along (4.000, 2.000, -4.000): q=[0.911 0.156 -0.377 0.065], local forward -> (0.667, 0.333, -0.667)
Rotating the local forward (0, 0, -1) by the result gives back the normalized look direction every time, including the 180° case (0, 0, 1) where the trace is negative.
When up and forward are parallel
If you ask an object to look straight up while the hint is also up, cross(f, upHint) is zero and the basis is undefined. Unity logs "Look rotation viewing vector is zero" for a zero forward and quietly picks something for parallel vectors; your own code must handle it. Typical fixes: use a different hint (the object's current up, or world forward) when |dot(f, upHint)| is close to 1.
#include <iostream>
#include <iomanip>
#include <cmath>
struct Vec3 { double x, y, 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 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) { return v * (1 / std::sqrt(dot(v, v))); }
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 safeRight(Vec3 forward, Vec3 upHint) {
Vec3 f = normalize(forward);
if (std::fabs(dot(f, upHint)) > 0.999) upHint = {0, 0, -1}; // looking straight up/down: use world forward
return normalize(cross(f, upHint));
}
int main() {
std::cout << std::fixed << std::setprecision(3);
std::cout << "level: right = " << safeRight({0, 0, -1}, {0, 1, 0}) << "\n";
std::cout << "straight up: right = " << safeRight({0, 1, 0}, {0, 1, 0}) << "\n";
std::cout << "straight down: right = " << safeRight({0, -1, 0}, {0, 1, 0}) << "\n";
}level: right = (1.000, 0.000, 0.000)
straight up: right = (-1.000, 0.000, 0.000)
straight down: right = (1.000, 0.000, 0.000)
Billboards
Particles, health bars and distant trees are flat quads that always face the camera: billboards. A screen-aligned billboard simply copies the camera's rotation. An axis-aligned billboard (a tree that should stay upright) only yaws: flatten the direction to the camera onto the ground plane first, then look along it with world up.
Tip: Look rotation sets all three axes at once, so it overwrites any roll the object had. For a character that should only turn horizontally toward a target, zero the
yof the direction before calling it, or the character will tilt forward when the target is below it.
Test yourself
Which inputs does a look rotation need?
- Only a target point
- A forward direction and an up hint
- Three Euler angles
- A quaternion and a matrix
What goes wrong when forward is parallel to the up hint?
- Nothing
- The cross product is zero, so the right axis is undefined
- The quaternion becomes negative
- The object scales up
A character should turn toward an enemy below it without leaning forward. What should you do?
- Use slerp with t = 1
- Set the direction's vertical component to zero before the look rotation
- Use a negative up hint
- Use Euler angles