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.

rodfollows the controller's position and direction
→
joints[0]hangs at the end of the handle, turns by bend
→
joints[1]hangs at the end of segment 1, turns by bend again
→
joints[2]hangs at the end of segment 2, turns by bend again

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.

Local matrix: relative to its parent (getMatrix)
Global matrix: multiplied all the way up (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 is root; the tail is its child, tail

Controls

makeFish in fishing.js

Notice: 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.js

move 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.


      
Full files: fishing.js ↗ · cg.js (matrix library) ↗ · clay.js is part of the course engine and not copied here

What you should be able to explain

  1. 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.
  2. Global = multiply all the way up. getGlobalMatrix multiplies 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.
  3. 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.
  4. Animation changes only a few numbers. Each frame the rod changes one number, bend; the fish changes heading and wag. 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 θ.