Pointing at Things: Look-At and Turn-Toward
To point something at a target, build its three axes from scratch: forward is the direction to the target, left comes from a cross product with the world's up and the object's own up is the cross product of those two. To turn there smoothly instead of snapping, slerp a step toward that orientation each frame: either a capped angle, or the fraction , which behaves the same at any frame rate.
What the symbols mean
- Forward
Unit vector from the eye toward the target.
- Left
Perpendicular to forward and to the world's up, so it stays level.
- Up
The object's own up, perpendicular to forward and left.
What the symbols mean
- Angle to go
How far the current orientation is from the target orientation.
- Top turning speed
In degrees or radians per second.
What the symbols mean
- Rate
How quickly it closes the gap, per second; λ = ln 2 / half-life.
Explanation
A look-at is a recipe for the orientation that points an object straight at a target: a camera at a player, a turret at a drone, a character's head at whoever is talking. You hand it two positions and a hint about which way is up, and it hands back three perpendicular axes for the object: forward (toward the target), left (sideways, kept level with the ground) and up (the object's own up, which tips back as forward tips up). Those three axes, side by side, are the rotation matrix. Each one takes a single line: one subtraction and two cross products. Nothing more to it than that; bear with the vectors.
Building the frame. We keep this unit's axes: up, forward, left.
- Forward is the direction from the eye to the target, made unit length: .
- Left has to be perpendicular to forward, and level, meaning perpendicular to the world's up . A cross product is perpendicular to both of its inputs, so points the right way (The Cross Product). We divide by its length because and usually aren't perpendicular, so the cross product comes out shorter than 1 (its length is the sine of the angle between them).
- Up is perpendicular to both forward and left: . No dividing this time, since two perpendicular unit vectors have a unit cross product.
Why those orders? The cross product is antisymmetric, , and the order is what keeps the frame right-handed. At rest, (left) and (up), so the recipe gives back the rest pose when the target is straight ahead. Swap one order and you get a mirror image, which isn't a rotation at all: the camera's picture comes out flipped.
The rotation that carries the object from its rest pose ( forward, left, up) to this one sends , and . A matrix's columns are where the axes go, so , the same idea as in Rotating in 2D. A graphics camera usually wants the opposite, the matrix that brings the world into the camera's view. Since 's columns are perpendicular unit vectors, , so the view matrix is together with the shift (Matrices in Computer Graphics). Many renderers point the camera down its own axis with up; the recipe is the same with the columns relabeled.
When it breaks. Look straight up and , so and "left" is . Every level direction would do, and the formula can't choose. It's gimbal lock in disguise (3D Rotation and Euler Angles). Cameras avoid it by clamping the pitch just short of , or by reusing the previous frame's left vector when gets tiny.
To a quaternion. To blend look-at orientations with slerp, convert to a quaternion. When (true for any turn of less than ):
These come from writing out the matrix of : its diagonal adds up to , and its mirror-image entries differ by , and .
Turning toward instead of snapping. A look-at gives the orientation to end up in; jumping there in one frame looks robotic. There are two standard ways to get there.
- Capped speed. Let be the angle still to go (). This frame may turn at most , so slerp the fraction of the way. Since slerp turns at a steady rate, that fraction is exactly worth of turning, and on the last frame the lands it on the target. It's the 3D version of the clamped turn in 2D. Turrets and security cameras move like this: steady, then a clean stop.
- Smooth follow. Slerp the fraction every frame. Slerp moves along the arc in proportion, so each frame leaves of the remaining angle, and after any total time the gap is , however was chopped into frames. That's the frame-rate-independent smoothing from Smoothing That Doesn't Depend on Frame Rate, done on a sphere. For a half-life (the time to close half the gap), use . Follow cameras move like this: quick at first, then easing in.
Following a moving target. When the target keeps moving, the smooth follower lags. Take a target sweeping around at a steady angular speed ; the follower's angle obeys , a first-order linear equation (Linear First-Order Equations). Try : then and the right side is , so . Once the start-up transient dies away, the follower trails by a fixed angle : faster targets or lazier smoothing mean more lag. A capped follower with catches up completely and then tracks with no lag at all. The graph further down shows both.
function lookAt(eye: Vec3, target: Vec3, up: Vec3 = [0, 0, 1]): [Vec3, Vec3, Vec3] {
const f = normalize(sub(target, eye));
const l = normalize(cross(up, f)); // breaks when f is parallel to up
const u = cross(f, l);
return [f, l, u]; // the columns of R
}
// every frame, the same at any frame rate:
q = slerp(q, quatFromColumns(lookAt(eye, target)), 1 - Math.exp(-lambda * dt));
A common mistake. Writing q = slerp(q, target, 0.1) every frame. It's tempting because it looks smooth on your machine. It isn't frame-rate independent: at 144 fps the gets applied 144 times a second and the camera catches up far faster than at 30 fps, so the game feels different on every computer. The fraction has to depend on , through .
See it
Worked examples
A camera at the origin looks at a target at , with world up . Build its look-at frame , , .
- 1
Forward first. The direction from the eye to the target is .
A turret's orientation is away from its target orientation, which stays put. The game runs at 30 frames per second. Compare two ways of turning: (a) capped at per second and (b) smooth follow with per second. How far from the target is each after s?
- 1
One frame lasts s, and s is frames.
Practice
Type your answer and check it. Solve them all to complete the lesson.
An eye sits at and looks at a target at . Find its forward unit vector .
A camera's forward vector is and the world's up is . Find its left vector .
A camera at the origin looks at a target at with world up . Find its up vector .
A turret at the origin must aim at a drone at meters. Through what angle above horizontal must the barrel tilt, in degrees?
A security camera turns toward its target orientation at a capped per second, slerping the fraction of the remaining angle each frame. The target is away and doesn't move. At 60 frames per second, how many frames does it take to get there?
A follow camera turns toward its target orientation with and a half-life of s, meaning . The target is away and stays put. How many degrees away is the camera after s?
A turret follows a drone that circles it at a steady per second, smoothing its heading by with per second. Once it has settled, by how many degrees does the turret trail the drone?
A camera uses the look-at recipe with world up . The player flies straight overhead, so . What goes wrong?
The hardest one here. Get it, and you've got this lesson.
A camera sits at the origin and must look at a target at , with world up . At rest it looks along with its left along and its up along . Build its look-at frame, write and convert it to a unit quaternion with .
Where it's used
Every third-person game camera is a look-at aimed a little above the player's head, smoothed with an exponential follow so it trails with weight instead of jittering, and every engine ships a "look rotation" function that is exactly the cross-product recipe. Characters turn their heads and eyes toward points of interest with capped look-at constraints so they don't snap their necks. Turrets, tracking antennas and pan-tilt security cameras use capped turning, because their motors have a top speed. Drone gimbals keep a camera locked on a subject with the same frame math, and the degenerate straight-up case is why many of them simply refuse to point exactly at the zenith.