We know how to take a point from the ship's coordinates to the world's. In a 3D engine the return trip is just as common: to know whether the lighthouse is in front of the ship or behind it, to decide what the camera sees or to check whether a shot has hit an object, we have to express the things of the world in someone else's coordinates. That requires undoing transformations, that is, inverting them. In this lesson we will see that the inverses of the basic transformations are immediate, that those of a chain are obtained by reversing the order, and that rigid transformations have an especially cheap inverse. Then we will make the most important conceptual leap of the module: one and the same matrix can be read as something that moves an object or as something that changes the coordinate system from which it is described.
The code assumes that the functions from the previous lessons (affine_matrix, transform_point, transform_direction, translation_matrix, scaling_matrix, rotation_matrix_x/y/z, is_rigid, model_matrix, compose) and the matrix ship_orientation are already defined.
Inverse of each basic transformation
The inverse of a transformation is the one that undoes it. For the three basic transformations there is nothing to compute: it is enough to think about which operation undoes which.
| Transformation | How it is undone | Inverse |
|---|---|---|
| Translation \(T(\mathbf{t})\) | By shifting the same amount in the opposite direction | \(T(-\mathbf{t})\) |
| Scaling \(S(s_x, s_y, s_z)\) | By scaling with the reciprocal factors | \(S(1/s_x,\ 1/s_y,\ 1/s_z)\) |
| Rotation \(R(\theta)\) | By rotating the same angle in the opposite direction | \(R(-\theta) = R^T\) |
A few remarks:
- Translation. Only the fourth column changes sign. It always has an inverse.
- Scaling. It requires that no factor be 0. A zero factor flattens the object (determinant 0) and, as we saw in "Definition and Properties", what has been flattened cannot be recovered.
- Rotation. Being orthogonal, its inverse is its transpose, as we established in "Inverse, Transpose and Orthogonal Matrices". We do not even need to know the angle or the axis: rows and columns are swapped, without a single arithmetic operation.
Example. The inverse of the ship's orientation, \(N = R_y(53.13°)\), is \(R_y(-53.13°)\), which coincides with \(N^T\):
\[ N^{-1} = N^T = \begin{pmatrix} 0.6 & 0 & -0.8 \ 0 & 1 & 0 \ 0.8 & 0 & 0.6 \end{pmatrix} \]
import numpy as np
t = translation_matrix(8.0, 3.0, 6.0)
s = scaling_matrix(4.0, 1.0, 2.0)
r = rotation_matrix_y(np.arctan2(0.8, 0.6))
print(np.allclose(np.linalg.inv(t), translation_matrix(-8.0, -3.0, -6.0)))
print(np.allclose(np.linalg.inv(s), scaling_matrix(0.25, 1.0, 0.5)))
print(np.allclose(np.linalg.inv(r), r.T))Inverse of a composition
To undo a chain we have to undo its steps in reverse order, starting with the last one applied. It is the socks-and-shoes rule from Module 1:
\[ (A,B)^{-1} = B^{-1} A^{-1} \qquad\qquad (A,B,C)^{-1} = C^{-1} B^{-1} A^{-1} \]
Applied to the model matrix, which scales, rotates and translates:
\[ (T,R,S)^{-1} = S^{-1},R^{-1},T^{-1} = S(1/s_x, 1/s_y, 1/s_z);R^T;T(-\mathbf{t}) \]
Read from right to left, the inverse does just what you would expect: first it removes the translation, then it undoes the rotation and finally it undoes the scale.
Example. The matrix of the ship's hull was \(T(8, 3, 6),N,S(4, 1, 2)\), and it took the local vertex \((1, 0, 0)\) to the wing tip, \((10.4, 3, 2.8)\). Let's travel the path in reverse:
\[ (10.4, 3, 2.8) ;\xrightarrow{\ T(-8, -3, -6)\ }; (2.4, 0, -3.2) ;\xrightarrow{\ N^T\ }; (4, 0, 0) ;\xrightarrow{\ S(0.25, 1, 0.5)\ }; (1, 0, 0) \]
The middle step: \(N^T (2.4, 0, -3.2) = (0.6 \cdot 2.4 + 0.8 \cdot 3.2,\ 0,\ 0.8 \cdot 2.4 - 0.6 \cdot 3.2) = (1.44 + 2.56,\ 0,\ 1.92 - 1.92) = (4, 0, 0)\).
ship_rotation = affine_matrix(ship_orientation, [0.0, 0.0, 0.0])
hull_model = compose(t, ship_rotation, s)
hull_inverse = compose(scaling_matrix(0.25, 1.0, 0.5),
ship_rotation.T,
translation_matrix(-8.0, -3.0, -6.0))
print(np.allclose(hull_inverse, np.linalg.inv(hull_model)))
print(np.round(transform_point(hull_inverse, [10.4, 3.0, 2.8]), 6))
# The classic mistake: inverting each piece but keeping the order
wrong = compose(translation_matrix(-8.0, -3.0, -6.0),
ship_rotation.T,
scaling_matrix(0.25, 1.0, 0.5))
print(np.round(transform_point(wrong, [10.4, 3.0, 2.8]), 6))Inverting the pieces without reversing the order gives a result that has nothing to do with the right one.
Fast inverse of a rigid transformation
The most common case in an engine is inverting a rigid transformation: a rotation followed by a translation, with no scale. That is what the matrices of the ship, the camera and almost any moving object look like. Its matrix is \(M = T(\mathbf{t}),R\), which we write in block form as \([R \mid \mathbf{t}]\).
Applying the composition rule:
\[ M^{-1} = R^{-1},T(-\mathbf{t}) = R^T,T(-\mathbf{t}) \]
What does this matrix do to a point \(\mathbf{p}\)? First it subtracts \(\mathbf{t}\) and then it applies \(R^T\): \(R^T(\mathbf{p} - \mathbf{t}) = R^T\mathbf{p} - R^T\mathbf{t}\). That is, a linear part \(R^T\) and a translation \(-R^T\mathbf{t}\):
\[ \begin{pmatrix} R & \mathbf{t} \ \mathbf{0}^T & 1 \end{pmatrix}^{-1} = \begin{pmatrix} R^T & -R^T\mathbf{t} \ \mathbf{0}^T & 1 \end{pmatrix} \]
The recipe, in two steps:
- Transpose the 3×3 block.
- Multiply that transposed block by the translation and flip the sign.
Watch out for the typical mistake: the new translation is not \(-\mathbf{t}\), but \(-R^T\mathbf{t}\). The displacement has to be undone as expressed in the rotated axes.
Example by hand: the ship
For \(M_{\text{ship}}\), with \(R = N\) and \(\mathbf{t} = (8, 3, 6)\):
\[ N^T \mathbf{t} = \begin{pmatrix} 0.6 & 0 & -0.8 \ 0 & 1 & 0 \ 0.8 & 0 & 0.6 \end{pmatrix} \begin{pmatrix} 8 \ 3 \ 6 \end{pmatrix} = \begin{pmatrix} 4.8 - 4.8 \ 3 \ 6.4 + 3.6 \end{pmatrix} = \begin{pmatrix} 0 \ 3 \ 10 \end{pmatrix} \]
\[ M_{\text{ship}}^{-1} = \begin{pmatrix} 0.6 & 0 & -0.8 & 0 \\ 0 & 1 & 0 & -3 \\ 0.8 & 0 & 0.6 & -10 \\ 0 & 0 & 0 & 1 \end{pmatrix} \]
def rigid_inverse(m):
"""Inverse of a rigid 4x4 matrix [R | t]: returns [R^T | -R^T t].
Only valid if the 3x3 block is a rotation (no scale, no shear).
"""
m = np.asarray(m, dtype=float)
r = m[:3, :3]
t = m[:3, 3]
return affine_matrix(r.T, -r.T @ t)
ship_world = model_matrix([8.0, 3.0, 6.0], ship_orientation)
print(np.round(rigid_inverse(ship_world), 6))
print(np.allclose(rigid_inverse(ship_world), np.linalg.inv(ship_world)))
print(np.allclose(rigid_inverse(hull_model), np.linalg.inv(hull_model)))The last line is a warning: hull_model includes the scale \((4, 1, 2)\), it is not rigid, and the fast formula gives a wrong result. Before using it you have to be sure (is_rigid).
Compared with np.linalg.inv
rigid_inverse(m) |
np.linalg.inv(m) |
|
|---|---|---|
| Validity | Rigid matrices only | Any invertible matrix |
| Method | One transpose and one matrix-vector product | General Gaussian elimination |
| Cost | Minimal: a handful of multiplications | Higher |
| Accuracy | The inverse rotation is exact (numbers are merely rearranged) | Introduces small rounding errors |
| Can it fail? | No: a rigid matrix always has an inverse | Yes, with singular or ill-conditioned matrices |
In Python, with matrices this small, the difference in speed is barely noticeable, because the time goes into interpreter overhead; in a compiled engine that inverts thousands of matrices per frame it does matter. But the main reason to prefer it is a different one: it is exact and structural. The inverse of a rigid matrix computed this way is guaranteed to be rigid, with no traces of scale or shear slipped in by rounding.
For an arbitrary affine matrix \([A \mid \mathbf{t}]\) with \(A\) invertible, the general formula is analogous, \([A^{-1} \mid -A^{-1}\mathbf{t}]\); the rigid version is the case in which \(A^{-1}\) comes for free.
Two readings of the same matrix: active and passive
So far we have described matrices as something that moves things. But back in Module 1 we already used \(N\) in another way: as the matrix that "converts ship coordinates into world coordinates". Both descriptions are correct, and telling them apart is one of the most valuable skills in 3D graphics.
| Active reading | Passive reading | |
|---|---|---|
| What changes | The object: it moves, rotates, is deformed | The coordinate system: the object does not move |
| What stays fixed | The coordinate system | The object |
| What \(\mathbf{v}' = M\mathbf{v}\) means | \(\mathbf{v}'\) is another point, the result of moving \(\mathbf{v}\) | \(\mathbf{v}'\) is the same point, described with different numbers |
| Usual name | Transformation | Change of coordinate system |
Let's see it with \(M_{\text{ship}}\) applied to \((1, 2, 0)\), which gives \((8.6, 5, 5.2)\):
- Active reading. There is a freshly loaded ship model, sitting at the origin and aligned with the world axes, with its antenna at \((1, 2, 0)\). The matrix rotates it 53.13° and translates it: the antenna travels to \((8.6, 5, 5.2)\).
- Passive reading. The ship is already in place and nobody moves. The pilot, measuring from the seat with the ship's axes, says the antenna is at \((1, 2, 0)\). The lighthouse controller, measuring from the world origin, says it is at \((8.6, 5, 5.2)\). The matrix translates from one language to the other.
The numbers and the operation are identical; only the story changes. The rule for the passive reading is:
A matrix whose columns are the axes of a local system (and whose fourth column is its origin), all expressed in world coordinates, converts local coordinates into world coordinates. Its inverse converts world coordinates into local ones.
Moving the object or moving the system, in the opposite direction
The two readings are tied together by an "opposite directions" relationship: moving an object with \(M\) produces the same coordinates as leaving it still and moving the reference system with \(M^{-1}\). If you are on a train and the platform seems to slide backward, you cannot tell, just from what you see, whether the train is moving forward or the platform is moving back.
This idea is the basis of the camera in any engine: there is no operation that "moves the camera"; what is done instead is to move the whole world in the opposite direction. Shifting the camera 2 meters to the right is equivalent to shifting the entire scene 2 meters to the left. The matrix that achieves this is the inverse of the camera's world matrix, and it is called the view matrix; we will build it formally in Module 5.
Change of basis in 3D
Let's begin with the simplest version of a change of system: the two systems share an origin and differ only in their axes. That is a change of basis, and we already know it.
In "Vectors and Vector Spaces" we defined the coordinates of a vector with respect to a basis \({\mathbf{b}_1, \mathbf{b}_2, \mathbf{b}_3}\) as the coefficients \((c_1, c_2, c_3)\) that rebuild it:
\[ \mathbf{v} = c_1,\mathbf{b}_1 + c_2,\mathbf{b}_2 + c_3,\mathbf{b}_3 \]
If we place the basis vectors as the columns of a matrix \(P\)—the change-of-basis matrix—that linear combination is a matrix-vector product:
\[ \mathbf{v}{\text{standard}} = P,\mathbf{v}{\text{basis}} \qquad\Longleftrightarrow\qquad \mathbf{v}{\text{basis}} = P^{-1},\mathbf{v}{\text{standard}} \]
| Direction | Operation | What it does |
|---|---|---|
| From the new basis to the standard one | \(P\) | Rebuilds the vector: a linear combination of the basis vectors |
| From the standard basis to the new one | \(P^{-1}\) | Finds the coefficients: solves the system \(P,\mathbf{c} = \mathbf{v}\) |
Two cases we have already handled, now seen in this light:
- The ship basis in the top-down view, \(\mathbf{u}_1 = (1, 1)\), \(\mathbf{u}_2 = (-1, 1)\), with \(P = U = \begin{pmatrix} 1 & -1 \ 1 & 1 \end{pmatrix}\). The beacon located at \((3, 1)\) has coordinates \(U^{-1}(3, 1) = (2, -1)\) in that basis, and conversely, \(U,(2, -1) = (3, 1)\). Since \(U\) is not orthogonal, its inverse really has to be computed.
- The ship's axes in 3D, with \(P = N\). Since \(N\) is orthogonal, \(P^{-1} = N^T\): going from the world axes to the ship's costs one transpose. When the basis is orthonormal, the change of basis is a rotation (or a rotation with a reflection).
Example. The ship's velocity is \((2, 0, -1)\) in the world axes. How does the pilot perceive it?
\[ N^T \begin{pmatrix} 2 \ 0 \ -1 \end{pmatrix} = \begin{pmatrix} 0.6 \cdot 2 + (-0.8)(-1) \ 0 \ 0.8 \cdot 2 + 0.6 \cdot (-1) \end{pmatrix} = \begin{pmatrix} 2 \ 0 \ 1 \end{pmatrix} \]
In its own axes, the ship advances 1 m/s along its longitudinal axis and slides 2 m/s to its right: it is "drifting". It is the same vector as always—neither the direction nor the speed at which the ship moves has changed—described with different numbers.
From local to world and from world to local
A complete coordinate system is not just a basis: it also has an origin. The ship's local system is made up of its three axes (the columns of \(N\)) and its origin (the point \((8, 3, 6)\)). Those four pieces of data are precisely the four columns of its world matrix. Therefore:
\[ \mathbf{p}{\text{world}} = M,\mathbf{p}{\text{local}} \qquad\qquad \mathbf{p}{\text{local}} = M^{-1},\mathbf{p}{\text{world}} \]
| Conversion | Matrix | With points (\(w = 1\)) | With directions (\(w = 0\)) |
|---|---|---|---|
| Local → world | \(M = [R \mid \mathbf{t}]\) | \(R,\mathbf{p} + \mathbf{t}\) | \(R,\mathbf{d}\) |
| World → local | \(M^{-1} = [R^T \mid -R^T\mathbf{t}]\) | \(R^T(\mathbf{p} - \mathbf{t})\) | \(R^T\mathbf{d}\) |
The world-to-local formula is easy to understand in two stages: \(\mathbf{p} - \mathbf{t}\) is the displacement from the object's origin to the point, measured in the world axes; \(R^T\) expresses that displacement in the object's axes. With directions only the second step is needed, which is the change of basis from the previous section.
Where is the lighthouse as seen from the ship?
The base of the lighthouse is at the world origin, \((0, 0, 0)\). Its coordinates in the ship's system are:
\[ M_{\text{ship}}^{-1} \begin{pmatrix} 0 \ 0 \ 0 \ 1 \end{pmatrix} = \begin{pmatrix} 0 \ -3 \ -10 \ 1 \end{pmatrix} \]
There is nothing to compute: the world origin is transformed into the fourth column of the inverse, which we already had. The interpretation, axis by axis:
- \(x = 0\): neither to the right nor to the left.
- \(y = -3\): three meters below.
- \(z = -10\): ten meters in the direction opposite to the longitudinal axis.
For the pilot, the lighthouse is exactly behind the ship, 10 meters back and 3 below. A fact that is impossible to see at a glance in world coordinates and obvious in local ones: that is the point of changing systems.
def local_to_world(model, p_local):
"""Converts a point from local coordinates to world coordinates."""
return transform_point(model, p_local)
def world_to_local(model, p_world):
"""Converts a world point to the local coordinates of the object `model`."""
if is_rigid(model):
model_inverse = rigid_inverse(model) # fast, exact path
else:
model_inverse = np.linalg.inv(model) # general case (with scale)
return transform_point(model_inverse, p_world)
print(np.round(world_to_local(ship_world, [0.0, 0.0, 0.0]), 6)) # base of the lighthouse
print(np.round(world_to_local(ship_world, [0.0, 12.0, 0.0]), 6)) # lighthouse lamp
print(np.round(world_to_local(ship_world, [8.6, 5.0, 5.2]), 6)) # the antenna
print(np.round(local_to_world(ship_world, [1.0, 2.0, 0.0]), 6)) # and backThe lamp is also behind, but 9 meters above; and the antenna, which in the world is at \((8.6, 5, 5.2)\), recovers its local coordinates \((1, 2, 0)\).
For directions we use transform_direction with the matrix or with its inverse; for example, transform_direction(rigid_inverse(ship_world), [2.0, 0.0, -1.0]) returns the velocity \((2, 0, 1)\) that we computed by hand.
Between two local systems: from the ship to the camera
What if we want the antenna's coordinates not in the world, but relative to the camera? There is no direct route, but the world serves as a connecting station: first we go up from the ship to the world and then down from the world to the camera.
graph LR
A[Ship<br/>coordinates] -- "M_ship" --> B[World<br/>coordinates]
B -- "inverse of M_camera" --> C[Camera<br/>coordinates]
Composing (from right to left):
\[ \mathbf{p}{\text{camera}} = M{\text{camera}}^{-1};M_{\text{ship}};\mathbf{p}_{\text{ship}} \]
In general, the matrix that converts coordinates in a system \(A\) into coordinates in another system \(B\) is \(M_B^{-1},M_A\).
Example. The camera is at \((0, 5, 20)\) and, in its initial position, is not rotated: it looks toward \(-Z\), which is the viewing direction by convention. Its world matrix is \(T(0, 5, 20)\) and its inverse, \(T(0, -5, -20)\). The antenna:
\[ (1, 2, 0) ;\xrightarrow{\ M_{\text{ship}}\ }; (8.6, 5, 5.2) ;\xrightarrow{\ T(0, -5, -20)\ }; (8.6,\ 0,\ -14.8) \]
For the camera, the antenna is 8.6 meters to the right, exactly at its own height and 14.8 meters in front (a negative \(z\) coordinate means "in front", because the camera looks toward \(-Z\)).
def between_systems(source_model, target_model):
"""Matrix that converts coordinates from the source system to the target system."""
return np.linalg.inv(target_model) @ source_model
camera_world = translation_matrix(0.0, 5.0, 20.0)
ship_to_camera = between_systems(ship_world, camera_world)
print(np.round(ship_to_camera, 6))
print(np.round(transform_point(ship_to_camera, [1.0, 2.0, 0.0]), 6)) # antenna
print(np.round(transform_point(ship_to_camera, [0.0, 0.0, 0.0]), 6)) # center of the ship[[ 0.6 0. 0.8 8. ] [ 0. 1. 0. -2. ] [ -0.8 0. 0.6 -14. ] [ 0. 0. 0. 1. ]] [ 8.6 0. -14.8] [ 8. -2. -14.]
With a camera that also rotates to follow the ship, the idea is exactly the same; what changes is how its matrix is built. That construction—the view matrix and the function known as look-at—belongs to Module 5. What you should take away from here is the overall scheme: the whole graphics pipeline is a succession of changes of coordinate system, and each one is a matrix.
Expressing a transformation in another basis
So far we have changed the system for the coordinates of points and vectors. One last question remains: if what we have is a transformation, how does its matrix change when we change basis?
Let \(M\) be the matrix of a transformation in the standard basis and \(P\) the change-of-basis matrix (columns = vectors of the new basis). To apply the transformation to a vector given in coordinates of the new basis, we repeat the "go, do and come back" pattern we already used with pivots:
- Go to the standard basis: \(P\).
- Do the transformation there: \(M\).
- Come back to the new basis: \(P^{-1}\).
\[ M' = P^{-1},M,P \qquad\Longleftrightarrow\qquad M = P,M',P^{-1} \]
\(M\) and \(M'\) describe the same geometric transformation with different numbers. Matrices related in this way are called similar, and they share everything that belongs to the transformation rather than to the coordinates: the determinant and the eigenvalues, among other things.
Diagonalization, reread
This formula is not new. In "Eigenvalues and Eigenvectors" we wrote \(A = P,D,P^{-1}\), or equivalently, \(D = P^{-1} A,P\), with the eigenvectors as the columns of \(P\). Now we can say what it meant: diagonalizing is changing to the basis in which the transformation looks simplest. The "boost" matrix \(A = \begin{pmatrix} 3 & 1 \ 1 & 3 \end{pmatrix}\), expressed in the basis of its eigenvectors \((1, 1)\) and \((-1, 1)\)—which turn out to be the vectors of \(U\)—is a simple scaling with factors 4 and 2.
Example: stretching the ship along its axis
The "Stellar Lighthouse" team wants a "hyperspace jump" effect that stretches everything to three times its length along the ship's longitudinal axis. In the ship's axes the transformation is trivial: \(M' = S(1, 1, 3)\), a scaling along its Z axis. But the effect must be applied to objects described in the world axes. The matrix in that basis is \(M = P,M',P^{-1}\) with \(P = N\):
\[ M = N \begin{pmatrix} 1 & 0 & 0 \ 0 & 1 & 0 \ 0 & 0 & 3 \end{pmatrix} N^T = \begin{pmatrix} 2.28 & 0 & 0.96 \ 0 & 1 & 0 \ 0.96 & 0 & 1.72 \end{pmatrix} \]
At first glance nobody would say that matrix is a scaling. But it is, and we can check it:
- Applied to the longitudinal axis \((0.8, 0, 0.6)\) it gives \((2.4, 0, 1.8)\): three times as long.
- Applied to the "right" axis \((0.6, 0, -0.8)\) it leaves it unchanged.
- Its determinant is 3, the same as that of \(S(1, 1, 3)\).
- It is symmetric, and its eigenvalues are 1, 1 and 3, with the ship's axes as eigenvectors. Diagonalizing it would mean traveling this same path in the opposite direction.
n = ship_orientation
stretch_local = np.diag([1.0, 1.0, 3.0])
stretch_world = n @ stretch_local @ n.T # P M' P^-1, with P^-1 = P^T
print(np.round(stretch_world, 6))
print(np.round(stretch_world @ n[:, 2], 6)) # longitudinal axis: tripled
print(np.round(stretch_world @ n[:, 0], 6)) # right axis: unchanged
print(np.round(n.T @ stretch_world @ n, 6) + 0.0) # back again: P^-1 M P
print(np.round(np.linalg.eigvalsh(stretch_world), 6))[[2.28 0. 0.96] [0. 1. 0. ] [0.96 0. 1.72]] [2.4 0. 1.8] [ 0.6 0. -0.8] [[1. 0. 0.] [0. 1. 0.] [0. 0. 3.]] [1. 1. 3.]
n[:, 2] is the third column of the matrix and np.linalg.eigvalsh computes the eigenvalues of a symmetric matrix. The + 0.0 is a cosmetic detail: it turns into 0. the -0. values that rounding sometimes leaves behind.
The same pattern works for any transformation that is "easy in an object's axes": a rotation about the ship's longitudinal axis, for example, would be \(N,R_z(\theta),N^T\). In Module 4 we will obtain a direct formula for rotating about an arbitrary axis, but the underlying idea will be this very one.
Common Mistakes and Tips
- Inverting the pieces without reversing the order. \((T,R,S)^{-1} = S^{-1} R^{-1} T^{-1}\), not \(T^{-1} R^{-1} S^{-1}\).
- Believing that the inverse of \([R \mid \mathbf{t}]\) is \([R^T \mid -\mathbf{t}]\). The translation of the inverse is \(-R^T\mathbf{t}\). It only coincides with \(-\mathbf{t}\) if there is no rotation.
- Using
rigid_inversewith matrices that have scale. It raises no error: it silently returns a wrong matrix. Check withis_rigidor usenp.linalg.inv. - Using the transpose as the inverse of a 4×4 matrix with translation. The transpose only inverts the rotation block. Transposing the full 4×4 matrix would send the translation to the last row, which would no longer be \((0, 0, 0, 1)\).
- Getting the direction of a change-of-system matrix wrong. The matrix with the object's axes in its columns goes from local to world. Naming variables with the direction built in (
ship_to_world,world_to_ship) prevents most mistakes. - Subtracting the position after rotating. From world to local it is \(R^T(\mathbf{p} - \mathbf{t})\): subtract first, then rotate.
- Converting directions as if they were points. A velocity or an axis must not pick up the translation on the way there or on the way back: \(w = 0\).
- Writing \(P,M,P^{-1}\) when \(P^{-1} M,P\) is called for. Read the chain from right to left and ask yourself which coordinates the vector arrives in and which it must leave in.
- Tip: after computing any inverse, do the round-trip test:
np.allclose(m @ m_inv, np.eye(4)), or convert a known point and check that it returns to its original value.
Exercises
Exercise 1. A surveillance drone has the world matrix \(E = T(2, 0, -5),R_y(90°)\).
a) Write \(E\). b) Compute its inverse by hand with the formula for rigid matrices. c) Check that \(E\) takes the local point \((1, 0, 0)\) to a point in the world and that \(E^{-1}\) returns it to where it was.
Exercise 2. With the ship's world matrix, \(M_{\text{ship}}\), and its inverse computed in the lesson:
a) An escort drone is at the point \((10, 3, 6)\) in the world. Where does the pilot see it?
b) Where does the pilot see the camera, located at \((0, 5, 20)\)? Interpret the result.
c) Check both results with world_to_local.
Exercise 3. The ship's antenna has local matrix \(T(1, 2, 0),R_x(90°)\) relative to the ship, and its tip is at \((0, 1, 0)\) in its own coordinates. The camera has world matrix \(T(0, 5, 20)\).
a) Write, as a product of matrices, the matrix that converts antenna coordinates into camera coordinates. b) Compute by hand the coordinates of the tip relative to the camera (in "Composition of Transformations" we found that its position in the world is \((9.4, 5, 5.8)\)). c) Write the inverse of the matrix from part a) as a product of matrices and say which conversion it performs.
Solutions
Solution 1.
a) The block of \(R_y(90°)\) with the translation in the fourth column:
\[ E = \begin{pmatrix} 0 & 0 & 1 & 2 \ 0 & 1 & 0 & 0 \ -1 & 0 & 0 & -5 \ 0 & 0 & 0 & 1 \end{pmatrix} \]
b) We transpose the 3×3 block and compute \(-R^T\mathbf{t}\):
\[ R^T = \begin{pmatrix} 0 & 0 & -1 \ 0 & 1 & 0 \ 1 & 0 & 0 \end{pmatrix} \qquad R^T \mathbf{t} = \begin{pmatrix} 0 & 0 & -1 \ 0 & 1 & 0 \ 1 & 0 & 0 \end{pmatrix} \begin{pmatrix} 2 \ 0 \ -5 \end{pmatrix} = \begin{pmatrix} 5 \ 0 \ 2 \end{pmatrix} \]
\[ E^{-1} = \begin{pmatrix} 0 & 0 & -1 & -5 \ 0 & 1 & 0 & 0 \ 1 & 0 & 0 & -2 \ 0 & 0 & 0 & 1 \end{pmatrix} \]
c) \(E,(1, 0, 0, 1)\): first column plus fourth, \((0 + 2,\ 0,\ -1 - 5) = (2, 0, -6)\). Back again: \(E^{-1}(2, 0, -6, 1) = (0 + 0 + 6 - 5,\ \ 0,\ \ 2 + 0 + 0 - 2) = (1, 0, 0)\) ✓.
e = compose(translation_matrix(2.0, 0.0, -5.0), rotation_matrix_y(np.radians(90.0)))
print(np.round(rigid_inverse(e), 6))
print(np.round(transform_point(rigid_inverse(e), [2.0, 0.0, -6.0]), 6) + 0.0)Typical mistake: putting \((-2, 0, 5)\) as the translation of the inverse. With that matrix, the drone's position in the world, \((2, 0, -5)\), would not return to the local origin \((0, 0, 0)\), but to \((3, 0, 7)\).
Solution 2.
a) We apply \(M_{\text{ship}}^{-1}\) to the point \((10, 3, 6)\), or equivalently, \(N^T(\mathbf{p} - \mathbf{t})\) with \(\mathbf{p} - \mathbf{t} = (2, 0, 0)\):
\[ N^T \begin{pmatrix} 2 \ 0 \ 0 \end{pmatrix} = \begin{pmatrix} 1.2 \ 0 \ 1.6 \end{pmatrix} \]
The pilot sees the drone at the same height, 1.2 m to the right and 1.6 m ahead. It is still 2 meters away (\(1.2^2 + 1.6^2 = 4\)): a rigid change of system does not alter distances.
b) \(\mathbf{p} - \mathbf{t} = (0, 5, 20) - (8, 3, 6) = (-8, 2, 14)\):
\[ N^T \begin{pmatrix} -8 \ 2 \ 14 \end{pmatrix} = \begin{pmatrix} 0.6 \cdot (-8) - 0.8 \cdot 14 \ 2 \ 0.8 \cdot (-8) + 0.6 \cdot 14 \end{pmatrix} = \begin{pmatrix} -16 \ 2 \ 2 \end{pmatrix} \]
The camera is 16 m to the left of the ship, 2 m above and only 2 m ahead: practically alongside it. The camera is seeing the ship side-on.
c)
print(np.round(world_to_local(ship_world, [10.0, 3.0, 6.0]), 6))
print(np.round(world_to_local(ship_world, [0.0, 5.0, 20.0]), 6))Solution 3.
a) Antenna → ship → world → camera, written from right to left:
\[ M_{\text{antenna} \to \text{camera}} = M_{\text{camera}}^{-1};M_{\text{ship}};M_{\text{antenna}} = T(0, -5, -20);\big[T(8, 3, 6),N\big];\big[T(1, 2, 0),R_x(90°)\big] \]
b) The first two legs are already done: the tip is at \((9.4, 5, 5.8)\) in the world. All that is left is to apply \(T(0, -5, -20)\):
\[ (9.4,\ 5 - 5,\ 5.8 - 20) = (9.4,\ 0,\ -14.2) \]
The tip of the antenna is level with the camera, 9.4 m to its right and 14.2 m in front.
c) Reversing the order and inverting each factor:
\[ M_{\text{camera} \to \text{antenna}} = M_{\text{antenna}}^{-1};M_{\text{ship}}^{-1};M_{\text{camera}} \]
It converts camera coordinates into antenna coordinates: it goes up from the camera to the world, down from the world to the ship and from the ship to the antenna.
antenna_local = compose(translation_matrix(1.0, 2.0, 0.0),
rotation_matrix_x(np.radians(90.0)))
antenna_to_camera = between_systems(compose(ship_world, antenna_local), camera_world)
print(np.round(transform_point(antenna_to_camera, [0.0, 1.0, 0.0]), 6))
camera_to_antenna = compose(rigid_inverse(antenna_local),
rigid_inverse(ship_world), camera_world)
print(np.allclose(camera_to_antenna, np.linalg.inv(antenna_to_camera)))Conclusion
The basic transformations are inverted without any computation: a translation with the opposite vector, a scaling with the reciprocal factors and a rotation with its transpose. A chain is inverted factor by factor and in reverse order, and a rigid transformation \([R \mid \mathbf{t}]\) has the immediate inverse \([R^T \mid -R^T\mathbf{t}]\). Every matrix admits two readings: active, in which it moves the object, and passive, in which it changes the coordinate system from which the object is described; that is why an object's world matrix converts local coordinates into world coordinates and its inverse goes the other way. Chaining the two takes us from one local system to another, and the formula \(M' = P^{-1} M,P\) expresses one and the same transformation in another basis, which gives diagonalization its geometric meaning. mini3d adds rigid_inverse, local_to_world, world_to_local and between_systems.
This lesson brings Module 2 to a close. You now know what a linear transformation is, how its matrix is built, how translations, scalings and rotations are written in 4×4 format, how they are composed into model matrices and hierarchies, and how they are undone to change coordinate systems: you have the complete machinery for moving objects around a scene. What we have not yet done is measure that scene: rigorously compute distances and angles, describe lines and planes, or work out whether a ray hits an object. That is what Module 3, "Geometry in 3D Space", is about; it begins with the lesson "Coordinates and Planes", where we will study in detail the coordinate systems of space, the coordinate planes and the octants into which they divide the scene.
