Transform Hierarchies
Objects in a scene are arranged in a tree: a sword is a child of a hand, the hand of an arm, the arm of a character, the character of a moving ship. Each node stores a local transform, relative to its parent. Its world transform is its parent's world transform times its local one:
world(node) = world(parent) * local(node)
world(root) = local(root)Move the ship, and everything on it moves along without touching a single child. That is the whole point of parenting.
Computing world transforms
Walk from the root down, multiplying as you go. The program uses 2D transforms (a 3×3 homogeneous matrix: rotation about z plus translation) because they are short to print, but the code is identical in 3D with Mat4.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
#include <string>
#include <vector>
struct Mat3 { double m[3][3]{}; };
Mat3 operator*(const Mat3& A, const Mat3& B) {
Mat3 R;
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
for (int k = 0; k < 3; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
return R;
}
// 2D transform: rotate by angle (degrees), then move to (x, y).
Mat3 trs2d(double x, double y, double deg) {
double a = deg * std::numbers::pi / 180, c = std::cos(a), s = std::sin(a);
return {{{c, -s, x}, {s, c, y}, {0, 0, 1}}};
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
struct Node { std::string name; int parent; Mat3 local; };
int main() {
std::cout << std::fixed << std::setprecision(3);
// A robot arm: base at (0,0), upper arm 2 long, forearm 1.5 long, hand at its end.
std::vector<Node> nodes = {
{"base", -1, trs2d(0, 0, 90)}, // arm points straight up
{"elbow", 0, trs2d(2, 0, -90)}, // 2 units along the base's x; bend right
{"hand", 1, trs2d(1.5, 0, 0)},
};
std::vector<Mat3> world(nodes.size());
for (size_t i = 0; i < nodes.size(); i++) { // parents come before children
world[i] = nodes[i].parent < 0 ? nodes[i].local : world[nodes[i].parent] * nodes[i].local;
std::cout << nodes[i].name << " at (" << z0(world[i].m[0][2]) << ", " << z0(world[i].m[1][2]) << ")\n";
}
nodes[0].local = trs2d(0, 0, 45); // rotate only the base...
for (size_t i = 0; i < nodes.size(); i++)
world[i] = nodes[i].parent < 0 ? nodes[i].local : world[nodes[i].parent] * nodes[i].local;
std::cout << "after turning the base to 45 deg, hand at ("
<< z0(world[2].m[0][2]) << ", " << z0(world[2].m[1][2]) << ")\n";
}base at (0.000, 0.000)
elbow at (0.000, 2.000)
hand at (1.500, 2.000)
after turning the base to 45 deg, hand at (2.475, 0.354)
The world position of a node is simply the translation column of its world matrix. Turning the base swung the elbow and hand with it, even though their local transforms never changed. This is forward kinematics, and it is how skeletal animation poses a character from joint rotations.
Re-parenting without jumping
When a character picks up an object, the object becomes a child of the hand. If you just copy its old local transform under the new parent, it teleports. To keep it where it is in the world, compute a new local transform:
newLocal = inverse(world(newParent)) * world(object)Engines expose this as an option: Unity's transform.SetParent(parent, worldPositionStays: true), Godot's reparent(new_parent, keep_global_transform = true), Unreal's AttachToComponent with KeepWorldTransform rules.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numbers>
struct Mat3 { double m[3][3]{}; };
Mat3 operator*(const Mat3& A, const Mat3& B) {
Mat3 R;
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
for (int k = 0; k < 3; k++) R.m[r][c] += A.m[r][k] * B.m[k][c];
return R;
}
Mat3 trs2d(double x, double y, double deg) {
double a = deg * std::numbers::pi / 180, c = std::cos(a), s = std::sin(a);
return {{{c, -s, x}, {s, c, y}, {0, 0, 1}}};
}
// Inverse of a rigid 2D transform: transpose the rotation, rotate the negated translation.
Mat3 inverseRigid(const Mat3& M) {
double c = M.m[0][0], s = M.m[1][0], x = M.m[0][2], y = M.m[1][2];
return {{{c, s, -(c * x + s * y)}, {-s, c, -(-s * x + c * y)}, {0, 0, 1}}};
}
double z0(double v) { return std::fabs(v) < 5e-4 ? 0.0 : v; }
void show(const char* label, const Mat3& M) {
double deg = std::atan2(M.m[1][0], M.m[0][0]) * 180 / std::numbers::pi;
std::cout << label << "pos (" << z0(M.m[0][2]) << ", " << z0(M.m[1][2]) << ") angle " << z0(deg) << "\n";
}
int main() {
std::cout << std::fixed << std::setprecision(3);
Mat3 handWorld = trs2d(3, 1, 90);
Mat3 swordWorld = trs2d(4, 4, 0); // lying on the floor
Mat3 naiveLocal = swordWorld; // just copy: wrong
show("naive: world ", handWorld * naiveLocal);
Mat3 keptLocal = inverseRigid(handWorld) * swordWorld;
show("kept: local ", keptLocal);
show("kept: world ", handWorld * keptLocal); // same as before parenting
}naive: world pos (-1.000, 5.000) angle 90.000
kept: local pos (3.000, -1.000) angle -90.000
kept: world pos (4.000, 4.000) angle 0.000
Hierarchies and performance
Every frame, an engine must update world matrices for every node whose local transform (or any ancestor's) changed. Engines keep a dirty flag per node and recompute only dirty subtrees, and they store nodes so parents come before children in memory (as the vector above does), which lets a single forward pass update everything.
Tip: Deep hierarchies cost CPU time and make bugs harder to find. Keep gameplay objects shallow, and avoid parenting physics bodies to moving non-physics objects: the physics engine and the hierarchy will fight over who controls the position.
Test yourself
How is a child's world matrix computed?
- local(child) * world(parent)
- world(parent) * local(child)
- world(parent) + local(child)
- inverse(world(parent)) * local(child)
An object must keep its world position when you attach it to a new parent. What is its new local matrix?
- Its old world matrix
- inverse(world(newParent)) * world(object)
- world(newParent) * world(object)
- The identity
You rotate only the root of a hierarchy. What happens to the children's local transforms?
- They are all recomputed
- Nothing; they stay the same while their world transforms change
- They are set to identity
- They rotate in the opposite direction