When the Loop Disappears
The next post in the math sub-series, promised at the end of What Scoring 100 Actually Means. This one is a pause rather than a new idea: a look back at How to Bend a Trunk and How a Branch Learns to Curve, now that the code they describe has been ported to TypeScript. You need cos, sin and a 3D vector. I reran every number below, and in two places I correct what those posts, or the prototype’s comments, said.
One question, two answers
The trunk and the branches both answer the same question: at a fraction t of the way along, where is the centre line? Everything else, the bark rings, the wire coils, the hotspots you tap, is built on that answer. Both live in src/sim/geometry.ts, ported from scr_bonsai_mesh.gml in the prototype, and they answer it in two different ways.
A walk: add up small steps
If you know where you are and which way you are facing, a small step of length ds moves you by direction × ds. Do it again from the new spot, and again. Your position is the running total of all the steps.
That is numerical integration in its plainest form. A curve’s position is the integral of its direction along its length, and a walk approximates that integral by a sum of small pieces. The trunk does exactly this in computeTrunkFrames:
pos = vec3(pos.x + T.x * ds, pos.y + T.y * ds, pos.z + T.z * ds);
The number of steps is Math.max(8, Math.floor(arcWorld * 20)), where arcWorld is the trunk’s height in display units (centimetres over 100, times 4). A 20 cm trunk gets 16 steps, so one step is 1.25 cm of trunk.
The cost of a walk is that you cannot jump to the middle. To know where the trunk is at t, you have to walk there. The code walks the whole trunk once, keeps the samples in a cache keyed on what feeds them (the tree’s revision, its height and its bend events), and trunkFrameAt then answers any t by blending the two nearest samples.
A closed form: write the answer down
A closed form is a formula you evaluate at t directly, with no history. A branch bent only left or right has one. Its direction turns at a steady rate, from angle at the base to angle + bend at the tip, and the integral of cos of a steadily turning angle is a sin. The branch post worked that out; here it is as branchPoint has it now:
const angleT = branch.angle + branch.bend * tAlong;
const scale = (lengthM / branch.bend) * (180 / Math.PI);
hx = scale * (dsin(angleT) - dsin(branch.angle));
hy = scale * (-dcos(angleT) + dcos(branch.angle));
Four lines, any t, the same few sines and cosines whether you ask for the base or the tip. The test follows a circular arc for horizontal bend takes a 10 cm branch (0.4 display units) bent 90 degrees. That is a quarter circle of radius (0.4 / 90) × (180 / π), about 0.2546, so the tip should sit exactly that far along x and along y from the base. The test checks both to 12 decimal places. A walk could never be held to that, because it carries step error. A formula only carries the computer’s rounding.
Why the trunk gets no formula
The trunk’s input is a list of bend events, as many as the player likes, at any heights. Each event rotates the trunk’s whole frame by TRUNK_BEND_PER_EVENT (20 degrees) about the axis (-sin a, cos a, 0), where a is the event’s compass angle. Different events have different axes, and rotations about different axes give different results in a different order. The code says so (“Rotations don’t commute”) and sorts the events by height first, and a test checks that two events listed in reverse order give the same trunk as the same two in height order.
So the trunk’s direction at height h is the starting direction (0, 0, 1) put through every rotation below h, one after another. There is no single expression in h that collapses that chain for any list the player might build. Some kind of loop over the events is unavoidable.
Here is the first correction. In May I wrote that for the trunk “numerical integration was the only honest answer”. A loop was the only honest answer, but the trunk’s walk is barely an approximation. Between two events its direction does not change at all, so the trunk is straight pieces joined by 20 degree corners, and stepping along a straight piece is exact. The only error is where a corner lands: an event takes effect at the first sample at or past its height, so a bend can arrive up to one step late. The test only applies an event once the arc reaches its height pins that rule on the 20 cm trunk: with an event at 10 cm, sample 7 still points straight up and sample 8, at exactly 10 cm, leans 20 degrees.
The trunk could even skip the steps: position at h is the sum of each straight piece’s direction times its length, one term per event. The fixed steps are there because the mesh wants a ring of bark every step anyway, so sampling and walking come together.
The branch has a loop too
The branch post had one bend. Later the same day, commit a9f5b6a gave branches a second, and the post never mentions it. bend turns a branch left or right about world up, and bendV turns it up or down about the level axis at right angles to its starting direction. Each wire click adds BRANCH_BEND_PER_CLICK, 15 degrees. When bendV is 0, branchPoint uses the formula above. Otherwise it walks:
const steps = 12;
const ds = (lengthM * tAlong) / steps;
for (let i = 0; i < steps; i++) {
const u = ((i + 0.5) / steps) * tAlong;
const Tv = vec3Rotate(T0, vAxis, bendV * u);
const T = vec3Rotate(Tv, zAxis, branch.bend * u);
hx += T.x * ds;
hy += T.y * ds;
hz += T.z * ds;
}
So every branch the player has bent up or down goes through a walk, and this one is a real approximation. The direction turns continuously, so each straight step cuts the corner a little. The i + 0.5 takes each step’s direction from its middle rather than its start, which is called the midpoint rule. Only the position walks: branchFrameAt gets the direction at t by applying the two rotations once, so the tangent is exact while the point is not.
How big is the error? I compared the 12-step answer with a 100,000-step one for a 10 cm branch bent 30 degrees each way, two clicks of each. The tip is off by 0.000064 display units, 0.016 mm of tree. With 6 steps the error is 0.00026, with 3 steps 0.0010, and with 24 steps 0.000016. Halve the step and the error drops to a quarter: the midpoint rule’s error goes with the square of the step size. Even at 90 degrees each way the tip is off by about 0.11 mm.
I expected a test to pin this, and none does. The test is continuous between the bendV=0 and bendV!=0 code paths gives a 12 cm branch a bendV of 0.0001 and checks it lands where the unbent one does, to 4 decimal places. But branchPoint takes the formula whenever bendV is under 0.001, and 0.0001 is under it. Both sides go through the formula, the gap is exactly 0, and the walk never runs. The test compares the formula with itself: it pins that the guard exists and says nothing about the walk’s step error. agrees between code paths at a tiny bendV does the same for the frame. Both tests are vacuous for the walk, and a backlog issue now exists to make them real. My own measurement, with the guard removed so the walk is forced, puts the largest gap at 0.0000307, at the tip, which the 4-decimal tolerance (within 0.00005) would still have room for.
Did the branch need its loop?
The prototype’s comment on branch_point, which I wrote, says that with both bends “the rotation composition no longer admits a closed form”. That is the second correction: it does.
Turning the starting direction (cos a, sin a, 0.25) up or down keeps it in its own upright plane, and the sideways turn then swings that plane round. Worked through, the direction at u is √1.0625 × (cos e cos h, cos e sin h, sin e), where the elevation is e = bendV × u + 14.04° (14.04 degrees is the angle whose tangent is 0.25, the branch’s built-in upward lean) and the heading is h = a + bend × u. Both angles are linear in u. Then cos e cos h = ½(cos(e + h) + cos(e − h)), each a cosine of a linear function, which integrates to a sine just as in the flat case. I coded it, and it matches the 100,000-step walk to about 10⁻¹² on four test branches.
So the loop is a choice, not a necessity. The formula has four terms where the flat one had two, and a division by bendV − bend or bendV + bend that needs its own guard whenever they are equal. That is not a rare case: one click up or down and one click sideways gives two bends of 15 degrees, plus or minus, so one of the two denominators is zero on about the most ordinary mixed branch there is. The walk has no special cases and is accurate to a hundredth of a millimetre. I think keeping it was fair. But the comment should have said “no simple closed form”, and the post that said the branch’s loop disappeared was out of date 74 minutes after it was committed.
The trade, side by side
| Walk | Closed form | |
|---|---|---|
| Accuracy | step error, unless the direction is piecewise constant | exact, up to rounding |
Cost for one t | every step up to t, unless cached | the same few sin and cos |
| Input it handles | anything you can step through | only the problem it was built on |
| Special cases | none | one for every zero denominator |
What I take from it
Two questions decide it. Does the direction at t depend on an ordered history? Then you need a loop, but count what it loops over: the trunk’s loop is over events, and its steps cost it almost nothing in accuracy. Is the direction a simple function of t? Then try the integral. A formula may exist even when it is ugly, and using it is a trade between special cases and step error. Either way, a test should pin what you chose. Here the formula is pinned to 12 decimals, and the walk’s step error is not pinned at all yet; a test that sets out to check it has to make sure the walk actually runs.
Coming next
What a 4×4 matrix actually does to a point: the view-projection matrix unpacked one column at a time, why moving a point needs a fourth row, and what the perspective divide is really dividing by.