A drone's camera says a cone is 4.31 m straight ahead of the lens. Chain the camera, body and world frames in the right order and the number lands on the cone. Swap two matrices and it lands 4.58 m away.
Turn the drone, then pick how to convert the camera's reading. Watch the purple guess land on the pink cone, or miss it. Then we build, piece by piece, frames, rotation matrices, Euler angles, axis-angle, quaternions and the 4 by 4 transform behind it.
You need matrix times vector, sine and cosine and a little Python. We build the rest from zero.
Lecture 1 of 14 in our follow-along of MIT 16.485, Visual Navigation for Autonomous Vehicles (VNAV, taught by Luca Carlone). Every idea is re-taught here in our own words and devices. Next: Lecture 2, Lie groups. The same ideas in 2D: AA 274A Lesson 2.
Illustrative scene in metres: drone at (1, 1, 1.5), camera 0.2 m ahead and 0.1 m below the body centre, tilted 20° down; cone at (5, 2.5, 0). Exact maths. Drag sideways on the scene to orbit the view.
Chapter 0
Turn the drone and read one cone with three rulers
A drone hovers 1.5 m above a field. Its camera reports a cone "4.3 m ahead". The planner wants to fly around the cone, so it needs the cone on the map. Ahead of what? The camera, the drone and the field each have their own idea of "ahead".
Each of them carries a frame: an origin and three axis directions. The world frame W is fixed to the field. The body frame B rides on the drone. The camera frame C rides on the lens. One cone has three different lists of numbers, one per frame.
The dashed staircase walks from the chosen frame's origin to the cone along that frame's own axes. Its three legs are the cone's coordinates. Turn the drone and watch which numbers change.
Illustrative scene, the same as the hero's. Exact maths. Drag sideways to orbit.
Work the number. At a yaw of 20°, the cone is (5.00, 2.50, 0.00) in W, (4.27, 0.04, −1.50) in B and (−0.04, −0.08, 4.31) in C. Turn to 30° and the body numbers become (4.21, −0.70, −1.50). The world numbers never move. The distance, 4.31 m, is the same in every frame.
Symptom: the drone flies toward a spot that is right only when it faces east.
Measure: log one landmark's world position at two headings. A real landmark gives the same numbers both times.
Fix: tag every vector with its frame, and convert with a rotation before you mix two frames.
Chapter 1
Switch between the body and camera conventions and test each with the right-hand rule
A frame is a ruler you can carry. It has an origin and three axes of length 1 that meet at right angles. We write the frame as a small letter before the point. Wp (W-p) is point p measured in the world frame. Cp (C-p) is the same point measured in the camera frame.
VNAV uses two conventions. The body frame is forward-left-up: x out of the nose, y out of the left side, z up. The camera frame is right-down-forward: x to the right of the image, y down the image, z out of the lens. The camera choice matches how pixels count: columns go right, rows go down. Lecture 5 builds the image on it.
The warm arc curls from x toward y. The dashed arrow is x × y, the way your right thumb points. A frame is right-handed when its z arrow lies on the dashed arrow.
Coordinates are in the body frame. Exact maths. Drag sideways to orbit.
Work the number. In body coordinates the camera's x axis is (0, −1, 0), to the drone's right. Its y axis is (0, 0, −1), down. Their cross product is (1, 0, 0), straight out of the nose. That is where the lens looks, so the camera's z axis is (1, 0, 0) and the frame is right-handed.
Subtraction is the one operation that works in a single frame. The displacement from p1 to p2 is the second point minus the first, both in the same frame:
Subtract a camera vector from a world vector and you get three numbers with no meaning. The frame letter on each side must match.
Symptom: a camera point lands mirrored on the map, left for right.
Measure: build the 3 by 3 matrix of the three axes and take its determinant. A right-handed frame gives +1, a mirrored one gives −1.
Fix: flip the sign of the wrong axis in the calibration file.
Chapter 2
Turn a frame and watch its three axis arrows fill the columns of a matrix
Two frames share an origin but point in different directions. How do we write that difference down? Ask where B's three axis arrows point, measured in W. Call those arrows WxB, WyB and WzB. Stack them side by side as the three columns of a matrix. That matrix is the rotation matrix RWB, the orientation of B in W.
Why columns? A point in B is a mix of B's three arrows, one step along each. Add those three moves in world coordinates and you have the point in W. A matrix times a vector does exactly that sum.
W-p equals R-W-B times B-p, where R-W-B has the columns W-x-B, W-y-B and W-z-B.
Take B-p's three numbers. Walk that far along each of B's arrows, using the arrows' world numbers. Add the three walks. The sum is W-p.
The drone yaws 90°. Its x arrow points along world y, (0, 1, 0); its y arrow along world minus x, (−1, 0, 0); z stays (0, 0, 1). A point 2 m to the drone's left, B-p = (0, 2, 0), becomes W-p = 2 × (−1, 0, 0) = (−2, 0, 0).
Teal arrows are B's axes; the grey ones are W's. The matrix shows each teal arrow as a column. Then break it: stretch an arrow or mirror one, and watch the two checks fail.
Exact maths. The pink point is B-p = (0, 2, 0), 2 m to the body's left. Drag sideways to orbit.
Not every 3 by 3 table is a rotation. A turn keeps the arrows length 1 and square to each other, and it keeps them right-handed. Those two facts define the set SO(3), the special orthogonal group: every 3D rotation and nothing else.
R-transpose times R equals I, and the determinant of R equals plus one.
Dot each column of R with itself: you must get 1. Dot two different columns: you must get 0. Then the box of the three columns must have volume plus 1, not minus 1.
A 30° yaw has columns (0.866, 0.5, 0) and (−0.5, 0.866, 0). First with itself: 0.866² + 0.5² = 1. First with second: 0.866 × (−0.5) + 0.5 × 0.866 = 0. So R-transpose is the inverse: undoing a turn costs nothing.
Count the freedom. R has 9 numbers. R-transpose R = I is 6 separate equations, three lengths and three right angles. That leaves 3 free numbers, the 3 degrees of freedom of a turn in space.
Symptom: a map slowly stretches or shears as the drone flies.
Measure: print the largest entry of R-transpose R minus I. Above 10−6, your matrix has drifted off SO(3).
Fix: re-orthonormalise R after many products, for example with an SVD: replace R by U V-transpose.
Chapter 3
Play two quarter turns in both orders and watch the frame end in two places
Calibration gives the camera's orientation on the drone, RBC. The estimator gives the drone's orientation in the world, RWB. Chain them to get the camera in the world, RWC.
R-W-C equals R-W-B times R-B-C.
Read right to left. R-B-C turns camera numbers into body numbers. R-W-B then turns body numbers into world numbers. The two inner letters, B and B, touch and cancel; the outer letters W and C name the result.
The drone yaws 90° and the camera looks along the nose. R-B-C sends the camera's z axis to body (1, 0, 0); R-W-B sends that to world (0, 1, 0). A cone 3 m down the lens, C-p = (0, 0, 3), sits 3 m along world y. Swap the order and the lens "looks" along world x: 90° wrong.
To go back, use the transpose. The world seen from the body is
The warm arrow is the axis of the turn in progress. The pink tip marks the drone's left arm, its y axis. Choose an order and press Play. Each turn is about a fixed world axis.
Exact maths. The rightmost matrix acts first on the drone. Drag sideways to orbit.
Work the number. Rz(90°) Rx(90°) sends the left arm (0, 1, 0) to (0, 0, 1), straight up. Rx(90°) Rz(90°) sends it to (−1, 0, 0), straight back. Same two turns, different end. Matrix products do not commute.
Symptom: landmarks are right when the drone is level and wrong when it tilts.
Measure: compute RWB RBC and RBC RWB for one logged pose. If your code matches the second, you swapped the order.
Fix: name every matrix by its two frames, and write chains so the inner letters touch.
Chapter 4
Read one matrix two ways and see the signs flip
A rotation matrix has two readings, and code often mixes them. In the active reading, R moves a point inside one frame. In the passive reading, the point stays still and R re-expresses it in a turned frame. The numbers differ by the sign of the angle.
Take the point (1, 0, 0) and a 30° turn about z. Actively, the point swings to (0.87, 0.50, 0). Passively, turn the frame B by 30° and keep the point. In B the point now looks 30° to the right: (0.87, −0.50, 0). That passive answer equals an active turn by −30°.
Seen from above. Grey is W; teal is the turned frame B. Pick a reading, then turn the angle. The dashed lines drop the pink point onto the axes that measure it.
Exact maths, turns about z only, seen from above.
Our symbols carry the reading for you. RWB is passive: B numbers in, W numbers out. Its transpose takes W numbers back into B:
The same matrix RWB, read actively, turns W's axes onto B's axes. Both readings are correct. A bug starts when one function uses one reading and its caller assumes the other.
Symptom: the estimated heading turns the opposite way to the real drone.
Measure: yaw the drone by +10° and check the sign of the logged yaw change.
Fix: decide whether each function moves points or changes frames, and write it in the name, as in R_world_body.
Chapter 5
Tilt the pitch to 90° and watch two gimbal rings fall into one plane
Nine numbers with six rules is a clumsy way to store three degrees of freedom. A pilot wants three angles. The yaw ψ (psi) turns the nose left or right. The pitch θ (theta) tips the nose up or down. The roll φ (phi) tilts the wings. Together they are Euler angles.
We use the yaw-pitch-roll order that aircraft and VNAV use: first yaw about z, then pitch about the new y, then roll about the newest x. As a product of fixed-axis matrices it reads:
R equals R-z of psi, times R-y of theta, times R-x of phi.
Build the three one-axis matrices from psi, theta and phi. Multiply them in the order z, y, x. To read the angles back, theta is minus the arcsine of entry (3,1). Psi comes from entries (2,1) and (1,1), and phi from entries (3,2) and (3,3).
With made-up angles psi = 30°, theta = 20°, phi = 10°: entry (3,1) is −0.342, so theta = arcsine of 0.342 = 20°. Psi = atan2(0.470, 0.814) = 30°. Phi = atan2(0.163, 0.925) = 10°. All three come back.
The warm rings are the three gimbals: yaw outside, pitch in the middle, roll inside. Set pitch to 90°. The roll axis lands on the yaw axis, and the two outer rings share a plane.
Exact maths, yaw-pitch-roll order. The rate factor is 1 / cos θ. Drag sideways to orbit.
At theta = 90°, Ry sends the roll axis onto minus z, the yaw axis. Now only the difference phi − psi matters. The pairs (psi, phi) = (10°, 40°), (0°, 30°) and (−10°, 20°) give the same matrix. One degree of freedom is gone, and the read-back divides by cos 90° = 0. At theta = −90° the sum phi + psi is what matters.
The trouble starts before 90°. To turn the drone about its own z axis at 1 rad/s, the yaw angle must change at 1 / cos θ rad/s. At 89° that is 57.3 rad/s, and the roll angle races at the same speed. A filter that tracks Euler angles sees huge rates from a gentle motion.
Symptom: the attitude estimate spins wildly as a drone climbs straight up.
Measure: plot 1 / cos θ from the log. Above about 10, at pitch over 84°, the angles are near lock.
Fix: store the attitude as a rotation matrix or quaternion. Use Euler angles only for display.
Chapter 6
Spin a vector about any axis and watch it split into a part that stays and a part that circles
Euler's rotation theorem says every 3D rotation is one turn about one fixed axis. So a rotation is an axis, a unit vector u, plus an angle θ. This axis-angle form has no favoured axes, so it has no gimbal lock.
How do we turn a vector v about u? Split v into two parts. The part along u does not move. The part square to u swings around a circle by θ. To swing it, we need a second direction in that circle: u × v. The skew matrix [u]× is the 3 by 3 matrix that does that cross product, so [u]× v = u × v.
R equals cos theta times I, plus sine theta times the skew of u, plus one minus cos theta, times u u-transpose.
Keep v's part along u. Take v's part square to u, multiply it by cos theta, and add sine theta times u × v. The sum is the turned vector. Rodrigues' formula writes those steps as one matrix.
Turn v = (1, 0, 0) by 30° about u = (1, 1, 0) / √2. The part along u is (0.5, 0.5, 0); the square part is (0.5, −0.5, 0); u × v = (0, 0, −0.707). Result: (0.5, 0.5, 0) + 0.866 × (0.5, −0.5, 0) + 0.5 × (0, 0, −0.707) = (0.933, 0.067, −0.354).
The warm line is the axis u. The pink arrow is v = (1, 0, 0) after the turn. The dashed circle is its path; the grey dashes show the part along u that never moves.
Exact maths, checked against SciPy's rotation vectors on 20,000 random turns. Drag sideways to orbit.
To get the angle back from any R, use its trace, the sum of its diagonal. Each term of Rodrigues' formula adds to it, and the total is 1 + 2 cos θ:
The axis is the one vector R leaves alone: R u = u. Two cases are special. At θ = 0 any axis works. At θ = 180°, u and −u give the same turn.
Symptom: recovering the axis gives NaN for tiny turns.
Measure: check whether sin θ is near zero when you divide by it.
Fix: below about 10−8 rad, return the identity; near 180°, read the axis from R + I instead.
Chapter 7
Turn a full 360° and find the drone home but the quaternion negated
Axis-angle is clear but awkward to chain. A unit quaternion packs the same axis and angle into four numbers that chain with one product. Call the axis u and the angle θ. The vector part is u scaled by the sine of half the angle; the scalar part is the cosine of half the angle.
q equals sine of theta over 2 times u, then cos of theta over 2.
Halve the angle theta. Multiply the axis u by the sine of the half angle: that is x, y, z. The cosine of the half angle is w. Sine squared plus cosine squared is 1, so q has length 1.
A 90° yaw: half angle 45°, sine and cosine both 0.707, u = (0, 0, 1). So q = (0, 0, 0.707, 0.707). Add 360° to theta and the half angle grows by 180°: q becomes (0, 0, −0.707, −0.707), the same turn.
Left, the drone turned by θ about the axis. Right, the four numbers of q as bars, and the half angle as a point on a circle. Slide to 360°: the drone is home, q is not.
Exact maths, scalar-last Hamilton quaternions, matched to SciPy on 20,000 random turns.
Every turn has two quaternions, q and −q: the double cover. When you average or blend two quaternions, first flip one so their dot product is positive. Otherwise the blend takes the long way round.
To chain two turns, multiply the quaternions with the Hamilton product. Write each as a vector part v and a scalar part s:
q-a times q-b equals s-a v-b plus s-b v-a plus v-a cross v-b, then s-a s-b minus v-a dot v-b.
For the vector part, scale each vector by the other's scalar, add them, and add their cross product. For the scalar part, multiply the scalars and subtract the dot product of the vectors.
Chain Chapter 3's turns: q-a = (0, 0, 0.707, 0.707) for z, q-b = (0.707, 0, 0, 0.707) for x. Vector part: (0.5, 0, 0) + (0, 0, 0.5) + (0, 0.5, 0) = (0.5, 0.5, 0.5). Scalar: 0.5 − 0 = 0.5. That is a 120° turn about (1, 1, 1) / √3, the same as Rz(90°) Rx(90°).
The product costs 16 multiplications against 27 for two 3 by 3 matrices. Turning a point costs the other way: 9 for a matrix, about 15 for a quaternion. To move a whole point cloud, convert q to R once and use R.
Symptom: after hours of updates, the attitude slowly shrinks points.
Measure: print the length of q. In our 100,000-step float32 test it fell to 0.9989.
Fix: divide q by its length after each update. It is one line, cheaper than fixing a matrix.
Chapter 8
Invert a pose two ways and see which one puts the world origin back
The camera is not at the drone's centre, and the drone is not at the world origin. A pose is a rotation plus a shift. Call the drone's position in the world tWB. A body point reaches the world by turning, then shifting: RWB Bp + tWB. Chains of these get messy fast.
The fix is homogeneous coordinates: append a 1 to each point, p̃ = (x, y, z, 1). Then one 4 by 4 matrix does both jobs. The set of all such matrices is SE(3), the special Euclidean group.
W-p-tilde equals T-W-B times B-p-tilde, where T-W-B has R-W-B and t-W-B on top, and zero, zero, zero, one below.
The top three rows turn B-p with R-W-B and then add t-W-B, because the appended 1 multiplies the last column. The bottom row copies the 1. Chains become products: T-W-C = T-W-B T-B-C.
The drone sits at t-W-B = (5, 0, 0), yawed 90°. The camera sits at (0.3, 0, 0.2) on the body, not turned. The camera's world position is R-W-B (0.3, 0, 0.2) + (5, 0, 0) = (0, 0.3, 0.2) + (5, 0, 0) = (5.0, 0.3, 0.2).
Now go the other way. TBW answers: where is the world, seen from the body? It is not the transpose of T. Undo the steps in reverse order: subtract the shift, then turn back.
T-B-W equals T-W-B inverse, which has R-transpose and minus R-transpose t on top, and zero, zero, zero, one below.
Transpose the rotation block. For the shift, turn t into the body frame with R-transpose, then negate it. Put the 0, 0, 0, 1 row back.
With R = Rz(90°) and t = (5, 0, 0): R-transpose t = (0, −5, 0), so the shift is (0, 5, 0). The world origin is 5 m to the drone's left. Check: the drone faces world +y, and the origin lies 5 m toward world −x, its left.
Seen from above. The purple ring is where the inverse claims the world origin is, mapped back to the map with TWB. With the right formula it sits on the grey origin.
Exact maths in a plane at constant height. The drone's y position is 0.
Symptom: a map is right at the start and drifts as the drone turns.
Measure: compute TWB times your inverse. Anything but the identity is a bug.
Fix: use [R⊤, −R⊤t; 0, 1]. It also beats a general 4 by 4 inverse on speed and rounding.
Chapter 9
Fly the drone and tilt the camera until the cone sits in the crosshair
Every visual navigation system runs this chain many times a second. The estimator gives TWB. Calibration gives TBC. A landmark in the world reaches the camera through TCW = (TWB TBC)−1. Then the camera divides by depth to make a pixel, the subject of Lecture 5.
Your goal: put the cone in the crosshair, within 2° of the lens axis. Watch the three coordinate lists. The world list never moves. The camera list reaches (0, 0, depth) when you win.
The inset is the camera's image: x right, y down, the cone drawn where the pinhole puts it. Move the drone and tilt the camera until the yellow crosshair turns green.
Illustrative scene in metres: drone at height 1.5 m and y = 1, cone at (5, 2.5, 0), image half-width 35°. Exact maths. Drag sideways to orbit.
One solution is drone x = 1.0 m, yaw 20°, roll 0° and tilt 20°: the hero's pose. The cone is then (−0.04, −0.08, 4.31) in the camera, 1.2° off the axis. Roll is the interesting knob. It turns the body about its x axis. The lens axis points 20° below that axis, so roll swings the cone away: at 25° of roll it sits 8.4° off.
Chapter 10
Compare the representations, keep the conventions, and take the code
A frame is a ruler; a rotation matrix holds a frame's three arrows; chains multiply with the inner letters touching; the transpose undoes a turn. Euler angles are for people and lock at 90° pitch. Axis-angle and quaternions have no lock; quaternions chain cheaply and carry a sign. A 4 by 4 transform holds a turn and a shift, and its inverse is [R⊤, −R⊤t].
| Representation | Numbers | Trouble spot | Chaining | Use it for |
|---|---|---|---|---|
| Rotation matrix R | 9, with 6 rules | drifts off SO(3); fix with SVD | 27 multiplications | turning many points, chains |
| Euler angles (ψ, θ, φ) | 3 | gimbal lock at θ = ±90° | via matrices, with trig | display, pilot input |
| Axis-angle (u, θ) | 3 (u θ) or 4 | axis undefined at θ = 0; u and −u at 180° | via matrices | small updates, Lecture 2 |
| Unit quaternion q | 4, with 1 rule | q and −q are the same turn | 16 multiplications | storing attitude, blending |
| Transform T in SE(3) | 12 useful, 16 stored | inverse is not the transpose | 4 by 4 product | poses, sensor chains |
python import numpy as np from scipy.spatial.transform import Rotation def skew(u): # skew(u) @ v == np.cross(u, v) return np.array([[0, -u[2], u[1]], [u[2], 0, -u[0]], [-u[1], u[0], 0]]) def rodrigues(u, th): # axis u (unit), angle th in radians u = np.asarray(u, float) / np.linalg.norm(u) return np.cos(th)*np.eye(3) + np.sin(th)*skew(u) + (1-np.cos(th))*np.outer(u, u) def qmul(a, b): # scalar-last Hamilton product va, sa, vb, sb = a[:3], a[3], b[:3], b[3] return np.append(sa*vb + sb*va + np.cross(va, vb), sa*sb - va @ vb) def make_T(R, t): T = np.eye(4); T[:3, :3] = R; T[:3, 3] = t return T def invert_T(T): # [R^T, -R^T t; 0, 1], never np.linalg.inv R, t = T[:3, :3], T[:3, 3] return make_T(R.T, -R.T @ t) Rz90 = rodrigues([0, 0, 1], np.pi/2) T_WB = make_T(Rz90, [5, 0, 0]) # drone at (5, 0, 0), yaw 90 deg T_BC = make_T(np.eye(3), [0.3, 0, 0.2]) # camera on the body print((T_WB @ T_BC)[:3, 3]) # [5. 0.3 0.2] print(invert_T(T_WB)[:3, 3]) # [0. 5. 0.] world origin, seen from B qz = np.array([0, 0, np.sin(np.pi/4), np.cos(np.pi/4)]) # 90 deg about z qx = np.array([np.sin(np.pi/4), 0, 0, np.cos(np.pi/4)]) # 90 deg about x print(qmul(qz, qx)) # [0.5 0.5 0.5 0.5]: 120 deg about (1,1,1) assert np.allclose(Rotation.from_quat(qmul(qz, qx)).as_matrix(), Rz90 @ rodrigues([1, 0, 0], np.pi/2))
SO(3) and SE(3) are groups you can also differentiate on. A filter must add a small correction to a rotation, but the sum of two rotation matrices is not a rotation. Lecture 2, Lie groups, adds the correction as a small axis-angle vector. Rodrigues' formula maps it back, and the skew matrix of this lesson becomes the tangent space.
Now press Present or Teach. Explain from memory the columns of R, the order of a chain, the lock at 90°, Rodrigues' three terms, the half angle in q and the inverse of T. Then go back to the hero and say why each wrong button misses the cone by the distance it does.