After splitting a character into a head, torso, upper arm, and forearm, define a coordinate frame for each part. If layers store only world coordinates, moving the shoulder also requires explicit updates to the elbow, wrist, and hand. Local transforms and matrix multiplication organise these dependencies in a bone hierarchy.
1. Local coordinates, pivots, and world coordinates
Let the upper arm be the parent bone and the forearm be its child. Put the forearm origin at the elbow and write a local point as v_local = [x, y, 1]^T. A 2D homogeneous transform combines translation, rotation, and scale in one 3 x 3 matrix:
M = T(tx, ty) R(theta) S(sx, sy)
T = [1 0 tx] R = [cos(theta) -sin(theta) 0]
[0 1 ty] [sin(theta) cos(theta) 0]
[0 0 1] [ 0 0 1]
If the artwork origin is not already at the joint, rotate around pivot p with:
M_pivot = T(p) R(theta) T(-p)
When a part orbits the canvas origin, check these two translations first: they move the geometry to the pivot and back. Incorrect pivot coordinates or matrix order can produce similar symptoms.
2. Why child bones multiply parent matrices
A child world matrix is not its local matrix. It is the parent world matrix multiplied by the child local matrix:
M_world(child) = M_world(parent) M_local(child)
v_world = M_world(child) v_local
Matrix multiplication is not commutative. T R rotates in local space and then translates into the parent frame. R T also rotates the translation vector and usually produces a different path.
3. A two-bone calculation by hand
For this example, screen x points right and screen y points down, while a positive angle still turns upward. The screen rotation is therefore the earlier R with its angle negated: R_screen(θ) = R(-θ). Place the root at (180, 260), use an upper-bone length of 155, and rotate the parent by 25 degrees. The elbow is:
elbow.x = 180 + 155 cos(25°) = 320.48
elbow.y = 260 - 155 sin(25°) = 194.49
If the forearm adds a 40-degree local rotation, its world angle is 25° + 40° = 65°, not 40 degrees. With a length of 125, the end point is approximately:
hand.x = 320.48 + 125 cos(65°) = 373.31
hand.y = 194.49 - 125 sin(65°) = 81.20
The end-point example above rounds the elbow to two decimal places before continuing. Keeping full precision gives an elbow near (320.477707, 194.494169) and a hand near (373.304990, 81.205696), which rounds at the end to (373.30, 81.21). Use the diagram to compare local and world angles; numerical comparisons need the same coordinate convention and rounding policy.
Matrices · FK
Skeletal Transforms and Forward Kinematics
Derive parent-child world coordinates from pivots, local space, and homogeneous matrices.
4. Binding SVG layers to bones
If the asset already contains <g id="left-forearm">, store each part’s parent ID, pivot, initial rotation, and layer order in a small manifest. At runtime, update the outer transform rather than rewriting path data every frame.
{
"id": "left-forearm",
"parent": "left-upper-arm",
"pivot": [42, 18],
"rotation": 0
}
Keep drawing order separate from bone hierarchy. The parent-child relation controls motion inheritance, while z-order decides whether the forearm appears in front of or behind the torso.
5. Common errors and a debugging order
- Wrong rotation center: verify that the pivot is in part-local coordinates and that the transform uses
T(p) R T(-p). - Child does not follow: verify
M_parent M_childinstead of applying the child local matrix directly. - Joint drifts under scale: identify which space owns non-uniform scale and avoid scaling child length twice.
- Wrong front/back order: maintain the SVG DOM drawing order separately from the bone graph.
6. Verify the hierarchy with invariants
Test the implementation through invariants. The table assumes no additional scale or constraints. In the pivot test, vary only the rotation about that pivot and hold the other transforms fixed. Choose tolerances for the floating-point precision in use:
| Test | Input | Expected result | Bug exposed |
|---|---|---|---|
| Identity pose | Every local matrix is identity | Every vertex keeps its original coordinate | Hidden default scale or canvas offset |
| Root translation only | Root moves by (20, -10) | Every descendant receives the same world offset | Missing parent multiplication |
| Parent rotation only | Parent 30°, child local angle 0° | Child world angle 30° and bone length unchanged | Local angle used as a world angle |
| Pivot rotation | Transform the pivot point itself | Its world position is unchanged by rotation | Wrong T(p) R T(-p) order |
| Length preservation | Rotation and translation only | Joint distance changes by less than tolerance | Duplicate scale or malformed matrix entries |
In debug builds, record bone_id, local angle, world angle, pivot world position, and matrix determinant. A pure 2D rotation block A should satisfy both AᵀA ≈ I and det(A) ≈ 1. The determinant alone misses area-preserving shear: [[1, 1], [0, 1]] also has determinant 1, but its columns are not orthogonal.
7. Reference update order
updateWorld(bone, parentWorld):
local = translate(bone.position)
* translate(bone.pivot)
* rotate(bone.angle)
* scale(bone.scale)
* translate(-bone.pivot)
bone.world = parentWorld * local
for child in bone.children:
updateWorld(child, bone.world)
Compute world matrices with parents before children, and let skinning and rendering read the same pose snapshot. If UI code, IK, or constraints change local channels, update the affected world matrices. A frame can contain several solve passes; each pass must use data from a consistent stage.
Rebuild from channels to reduce accumulated drift
Accumulated matrix multiplication can produce numerical drift. The determinant detects some size drift; column lengths and their dot product also test whether the rotation block remains an orthonormal basis.
An incremental animation can use this implementation: the character turns a little each frame, so multiply last frame’s matrix by a small delta rotation.
bone.matrix = bone.matrix * rotate(delta) // dangerous
This incremental update is valid in real arithmetic. Floating-point accumulation introduces rounding errors that can move the rotation block away from unit length and orthogonality. The value 1e-7 is roughly the scale of float32 machine precision near unity, not a fixed absolute error from every matrix multiplication. Actual drift depends on precision, the angle sequence, and the implementation.
Short playback can conceal this drift. A long-loop test should track column lengths, their dot product, and the determinant over time, without assuming that visible deformation must appear after a particular number of frames or minutes.
For animation whose state consists of position, angle, and scale, reconstruct the matrix from those channels:
bone.angle += delta // state is a scalar
bone.matrix = compose(bone.position,
bone.angle,
bone.scale) // rebuilt each frame
Storing position, angle, and scale reduces drift from repeated matrix multiplication, although those scalar channels still incur floating-point error. If incremental quaternions store orientation, check their unit length and renormalise as the precision requirements demand.
If the linear block is intended to remain a pure rotation, Gram–Schmidt can re-orthogonalise two nonzero, linearly independent columns. Applying it to intentional scale or shear changes the deformation. Nearly degenerate columns need separate handling.
When blending rotations, check that interpolation preserves rotation
The same principle has a second manifestation, which appears whenever two poses need blending — walk transitioning to run, or an aiming pose on the upper body over a locomotion pose on the lower.
A direct implementation adds the two matrices element-wise by weight:
M = (1 - t) * M_a + t * M_b // wrong
Element-wise blending generally does not preserve rotation. Consider the 2×2 linear blocks of two pure 2D rotations, with angle difference Δ and let t lie in [0, 1]. The blended matrix A satisfies AᵀA = [1 − 2t(1−t)(1−cosΔ)]I, and its determinant equals the bracketed quantity. At the midpoint of a 90-degree difference, the determinant is 0.5 and the length scale is 1/√2 ≈ 0.707107: area halves and lengths shrink by about 29.29%. This 2D example gives uniform shrinkage without shear; a more general affine blend can also shear. At a 180-degree difference, the midpoint is the zero matrix.
For poses expressed as TRS channels, interpolate the channels and recompose: linearly interpolate translation and scale, and choose the shortest angular path. Positive scales also permit logarithmic interpolation. Negative scales, reflections, an exact 180-degree tie, and intentional shear each need an explicit blending convention.
pos = lerp(a.pos, b.pos, t)
scale = lerp(a.scale, b.scale, t)
angle = a.angle + shortest_delta(a.angle, b.angle) * t
M = compose(pos, angle, scale)
TRS channels make this kind of pose blending easier to control. If the pose includes shear or other affine deformation, choose a decomposition and interpolation method that preserves the intended components; an arbitrary matrix need not represent a TRS-only pose.
Adjust draw order for occlusion when the character turns
In a 2D renderer that composites layers in draw order, a turn may require changing z-order for the affected parts while preserving the bone hierarchy. The artwork and occlusion design determine which parts change order; the whole list need not reverse.
For example, an asset may place the right arm in front of the torso when facing right, with the left arm behind, and swap those roles when facing left. Only z-order changes in this example; the left arm remains a child of the torso. Other artwork can use a different arrangement.
A horizontal mirror exchanges screen positions but does not change the draw list. If the mirrored occlusion matches the art direction, the order can stay unchanged. Intermediate turning poses or different upper- and lower-body headings may require independent draw-order control.
For artwork with only left- and right-facing states, two order values per part are one option:
{
"id": "left-arm",
"parent": "torso",
"z_facing_right": 10,
"z_facing_left": 90
}
The renderer selects and sorts by the current facing. Facing can come from the sign of the root’s horizontal scale or an explicit facing field. More complex turns may need draw order that varies with the pose.
Submit facing, pose, and draw order together so one render uses a consistent snapshot. Mixing versions can produce a single-frame flicker in which an arm has moved but the occlusion order still belongs to the old pose.
8. Next: solve joint angles from an end target
Forward kinematics answers, “Given these angles, where is the hand?” The next article, Two-Bone Inverse Kinematics, answers the reverse question. If the artwork still lacks stable pivots, start with the Character Image to SVG Animation Asset Pipeline.