HAPTIC FISHING / STUDY / LEVEL 5 OF 7
The rod as
a hierarchy.
The rod is not one object. It is a chain of nodes hung on each other: the handle hangs on the rod, the first segment hangs on the end of the handle, the second on the end of the first… Each node only says "relative to the node above me, move this far and turn this much". When a parent moves, everything below it follows. This is what class called hierarchical shapes, or a scene graph.
Side view: one rod
blue arrow = each node's own "forward" (its local Z axis)Controls
In the game, bend = 0.03 + 0.2 × tension: almost straight with no fish, about 0.23 radians per joint at full tension.
getMatrix)getGlobalMatrix)Read a 4×4 matrix by columns: the third column (green) is where this node's "forward" points, the fourth column (orange) is its position. Only the y and z rows matter here (x stays 0 in a side view).
Top view: one fish
the body isroot; the tail is its child, tailControls
makeFish in fishing.jsNotice: you changed only the body's heading, and the tail turned with it. You changed only the tail's wag, and the body did not move. A child follows its parent; a parent is not affected by its child.
The game's code
fishing.js① Building the rod (runs once, at the start): each segment is hung under the previous one with parent.add(). ② Every frame: rod is set to the controller's position and direction, and each joint gets identity().move(...).turnX(bend).
The engine's code
clay.jsmove and turnX each multiply by one matrix; getGlobalMatrix starts from the node itself and multiplies by the matrix of every node above it. This page uses the same matrix library, cg.js, and these same lines to compute both pictures above.
What you should be able to explain
- Each node only knows its parent. Segment 2 only knows "from the end of segment 1, go forward 0.45 m, then turn by bend". It does not need to know where it is in the world. That is its local transform.
- Global = multiply all the way up.
getGlobalMatrixmultiplies the node's matrix by the matrix of every ancestor. Since every joint turns by bend, by segment 3 the total turn is 3 × bend, and the rod bends into an arc on its own. - Order matters.
move(...).turnX(...)means "go to the end, then turn there".turnX(...).move(...)means "turn first, then go along the turned direction". The results differ; tick the second option to see it. - Animation changes only a few numbers. Each frame the rod changes one number,
bend; the fish changesheadingandwag. The shapes are never rebuilt.
Exercises: ① Tick "bend only the first joint": the rod becomes a straight, tilted stick. Why? ② Tick "remove the hierarchy": the segments come apart. Why? To place them correctly now, how would you have to compute each position? ③ The top right says "tip: both methods agree". The two lines in the game that compute the tip (search fishing.js for const angle = bend*(i+1)) do not use the hierarchy; they use cos and sin to draw the fishing line. Why do they give the same answer? Hint: turnX(θ) turns "forward" Z into Z × cos θ − Y × sin θ.