Scene Graph and Dirty Transforms
A sword is attached to a hand, the hand to an arm, the arm to a character, the character to a moving train. Each object stores its local transform relative to its parent, and its world transform is the product of all transforms up the chain:
world(sword) = world(hand) * local(sword)
= world(arm) * local(hand) * local(sword)
= ... * local(train) * local(character) * local(arm) * local(hand) * local(sword)The math is in Transform Hierarchies. The engine question is when to compute it. Recomputing every world matrix every frame is simple and wasteful: in a typical level most objects (buildings, props, terrain pieces) never move. A dirty flag marks transforms that changed; only they and their descendants are recomputed.
A scene graph with dirty flags
We use 2D affine matrices (rotation, uniform scale, translation) to keep the printout short; the structure is identical in 3D with Mat4 or position/quaternion/scale.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
struct Affine2 { // [a b tx; c d ty; 0 0 1]
double a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0;
static Affine2 trs(double x, double y, double degrees, double s) {
double r = degrees * 3.14159265358979323846 / 180.0;
return {s * std::cos(r), -s * std::sin(r), s * std::sin(r), s * std::cos(r), x, y};
}
Affine2 operator*(const Affine2& o) const {
return {a * o.a + b * o.c, a * o.b + b * o.d, c * o.a + d * o.c, c * o.b + d * o.d,
a * o.tx + b * o.ty + tx, c * o.tx + d * o.ty + ty};
}
};
struct Node {
std::string name;
int parent = -1;
std::vector<int> children;
double x = 0, y = 0, angle = 0, scale = 1; // local TRS
Affine2 world;
bool dirty = true;
};
class Scene {
public:
int add(std::string name, int parent, double x, double y, double angle = 0) {
nodes.push_back({std::move(name), parent, {}, x, y, angle, 1, {}, true});
int id = int(nodes.size() - 1);
if (parent >= 0) nodes[parent].children.push_back(id);
return id;
}
void setPosition(int id, double x, double y) { nodes[id].x = x; nodes[id].y = y; nodes[id].dirty = true; }
void setAngle(int id, double deg) { nodes[id].angle = deg; nodes[id].dirty = true; }
// Walk from the roots; a dirty parent forces its whole subtree to update.
int updateTransforms() {
int recomputed = 0;
for (std::size_t i = 0; i < nodes.size(); i++)
if (nodes[i].parent < 0) visit(int(i), Affine2{}, false, recomputed);
return recomputed;
}
std::vector<Node> nodes;
private:
void visit(int id, const Affine2& parentWorld, bool parentChanged, int& count) {
Node& n = nodes[id];
bool changed = n.dirty || parentChanged;
if (changed) {
n.world = parentWorld * Affine2::trs(n.x, n.y, n.angle, n.scale);
n.dirty = false;
count++;
}
for (int c : n.children) visit(c, n.world, changed, count);
}
};
int main() {
Scene s;
int train = s.add("train", -1, 0, 0);
int guard = s.add("guard", train, 2, 1);
int arm = s.add("arm", guard, 0.5, 1.5);
int sword = s.add("sword", arm, 1, 0);
for (int i = 0; i < 9; i++) s.add("crate" + std::to_string(i), -1, 10.0 + i, 0); // static props
std::cout << std::fixed << std::setprecision(2);
auto report = [&](const char* what) {
int n = s.updateTransforms();
const Affine2& w = s.nodes[sword].world;
std::cout << what << ": recomputed " << n << " of " << s.nodes.size() << ", sword at (" << w.tx << ", " << w.ty << ")\n";
};
report("first frame ");
report("nothing moved");
s.setAngle(arm, 90); report("arm rotated ");
s.setPosition(train, 5, 0); report("train moved ");
s.setPosition(guard, 3, 1);
s.setAngle(arm, 0); report("guard + arm ");
}first frame : recomputed 13 of 13, sword at (3.50, 2.50)
nothing moved: recomputed 0 of 13, sword at (3.50, 2.50)
arm rotated : recomputed 2 of 13, sword at (2.50, 3.50)
train moved : recomputed 4 of 13, sword at (7.50, 3.50)
guard + arm : recomputed 3 of 13, sword at (9.50, 2.50)
After the first frame, only what moved is touched. Moving the train recomputes 4 nodes (the train and its subtree), not the 9 static crates. When both the guard and its arm changed in one frame, the arm was still recomputed only once, because the update walks top-down and a node is visited once per pass.
Note: "Set dirty, compute later" also means world transforms are stale between a change and the next
updateTransforms(). Engines either run the update at fixed points in the frame (after gameplay, before physics sync and rendering), or compute world transforms lazily on read, walking up through dirty parents. Unity'sTransformand Godot'sNode3Dhide this from you; your engine must pick a rule and document it.
A flat, data-oriented layout
Pointer-based trees scatter nodes in memory. Many engines store the hierarchy as flat arrays sorted so that every parent comes before its children. Then one linear pass computes all world transforms in order, with no recursion, and the arrays stream through the cache. This is how Unity's TransformAccessArray and most ECS hierarchy systems work under the hood.
#include <iomanip>
#include <iostream>
#include <vector>
// 1D transforms (offset only) keep the example short; the ordering idea is what matters.
struct Hierarchy {
std::vector<int> parent; // parent[i] < i for every non-root i
std::vector<double> local;
std::vector<double> world;
std::vector<bool> dirty;
};
int update(Hierarchy& h) {
int recomputed = 0;
for (std::size_t i = 0; i < h.parent.size(); i++) {
int p = h.parent[i];
if (p >= 0 && h.dirty[p]) h.dirty[i] = true; // dirtiness flows down in array order
if (!h.dirty[i]) continue;
h.world[i] = (p >= 0 ? h.world[p] : 0.0) + h.local[i];
recomputed++;
}
for (std::size_t i = 0; i < h.dirty.size(); i++) h.dirty[i] = false;
return recomputed;
}
int main() {
// 0 1 2 3 4 5 6
Hierarchy h{{-1, 0, 0, 1, 1, -1, 5},
{10, 1, 2, 0.5, 0.25, 100, 3},
std::vector<double>(7, 0.0), std::vector<bool>(7, true)};
std::cout << std::fixed << std::setprecision(2);
std::cout << "initial: " << update(h) << " recomputed\n";
h.local[1] = 4; h.dirty[1] = true;
std::cout << "move node 1: " << update(h) << " recomputed\n";
for (std::size_t i = 0; i < h.world.size(); i++) std::cout << " node " << i << " world " << h.world[i] << "\n";
}initial: 7 recomputed
move node 1: 3 recomputed
node 0 world 10.00
node 1 world 14.00
node 2 world 12.00
node 3 world 14.50
node 4 world 14.25
node 5 world 100.00
node 6 world 103.00
Moving node 1 touched node 1 and its children 3 and 4: three nodes. The price of this layout is that reparenting must keep the "parent before child" order, which may mean moving a whole subtree inside the arrays. Reparenting is rare compared to updating, so the trade usually pays off.
Practical rules
- Not everything needs a parent. Deep hierarchies used only for folder-style organization in the editor cost real time at runtime. Flatten them when cooking the level.
- Static flag. Mark objects that never move as static; skip them entirely after the first update, and let the renderer and physics bake them.
- Scale with care. Non-uniform scale under a rotated parent produces shear, which a position/rotation/scale decomposition cannot represent. Many engines forbid or warn about it.
- Physics owns some transforms. For simulated bodies the physics engine writes the world transform; the scene graph must not overwrite it. Decide the direction of sync per object type (see Kinematic vs Dynamic).
Test yourself
Why does moving the train recompute the guard, arm and sword too?
- Because every node is recomputed each frame
- Because a changed parent changes the world transform of its whole subtree
- Because they share a dirty flag
- Because the crates are static
What property lets the flat-array hierarchy update in one linear pass?
- Nodes are sorted by name
- Every parent is stored before its children
- All nodes have the same parent
- Each node stores its world transform only
With dirty flags, what is the risk of reading a world transform right after setting a local position?
- It may be stale until the next transform update
- It throws an exception
- It returns the local transform
- It recomputes the whole scene