Z-up in a Y-down World
The next post in the math sub-series, promised at the end of Wrapping Wire Round a Branch. Same audience: you know cos and sin, and I’ll explain the rest, including the one idea from linear algebra the whole post hangs on. The Tree Was Upside Down and So Was I told how I got the prototype’s camera upright by trial and error. This one works out what the signs I landed on actually do.
Two signs that look wrong
The GameMaker prototype builds its viewer camera in obj_viewer_3d/Draw_72.gml with two engine calls:
var _view = matrix_build_lookat(_cx, _cy, _cz, cam_target.x, cam_target.y, cam_target.z, 0, 0, -1);
var _proj = matrix_build_projection_perspective_fov(60, -_aspect, 0.01, 20);
The game is z-up: trees grow in z. The last three numbers of the look-at are the camera’s “up” arrow, and it points down. The aspect ratio, width over height, is negative. The prototype’s architecture notes say the first is there because GameMaker’s screen y grows downward and the second “to match handedness”, and its guide says not to “fix” either. Neither says which handedness comes out the other end. That is the question here.
A mirror is a matrix with determinant -1
A set of axes is right-handed if x × y = z, with the cross product written the way the viewer’s vec3_cross writes it. Point your right hand’s first finger along x and your second along y, and your thumb gives z. A mirror turns a right hand into a left hand.
Rotations, scalings and flips are all linear maps, and each can be written as a 3×3 matrix. A matrix’s determinant is the factor by which it scales volume, with a sign. A rotation keeps volume and keeps a right hand right: determinant 1. Flipping one axis, the matrix diag(1, -1, 1), keeps volume but turns the hand over: determinant -1. Determinants multiply when you chain maps, so a chain is a mirror exactly when it holds an odd number of flips. Two flips cancel into a rotation: diag(-1, -1, 1) is a half turn about z.
You can test for a mirror without writing out the whole matrix. If a matrix’s rows are three arrows a, b, c, its determinant is the triple product (a × b) · c. So take the world arrows that point screen-right and screen-up. If right × up points out of the screen toward you, the picture shows the world as it is. If it points into the screen, the picture is a mirror.
What each sign does
The aspect ratio. A perspective matrix uses the aspect in exactly one place, the x scale. In Three.js, which I can read (PerspectiveCamera.updateProjectionMatrix and Matrix4.makePerspective), a 60 degree view gives
clip x = (1.732 / aspect) × view x
clip y = 1.732 × view y
where 1.732 is 1 / tan 30°. Negate the aspect and you negate the x scale, nothing else. That is one flip, left to right across the screen. It also means the aspect can never turn a picture upside down, which explains something in the upside-down post: my first fix took the minus off the aspect and the tree stayed upside down. No change to x can mend y.
The up arrow. A look-at builds the camera’s right arrow as a cross product with the up arrow you give it, then builds the camera’s true up as a cross product with that right arrow. Three.js’s Matrix4.lookAt does exactly that. Negate the up you hand in and the right arrow changes sign; the second cross product then changes sign too. Both screen axes flip. That is two flips, determinant +1, a half turn about the line of sight. So (0, 0, -1) is a rotation. It is what an upside-down tree needs, and it cannot change handedness.
The tally is one flip from the aspect and none, net, from the up arrow. Whatever GameMaker does inside, those two signs leave the picture one mirror away from what it would be without them.
Is the prototype’s picture a mirror?
GameMaker’s own convention isn’t in the repo, so I’ll settle it from the code instead of the engine. Two facts are pinned. The bend buttons’ _trunk_bend_world_angle sends “right” to world angle -90 - yaw, the arrow (-sin yaw, -cos yaw, 0). World +z is up on screen. The orbit camera sits at (cos yaw, -sin yaw, …) from the tree, so at pitch 0 it looks along (-cos yaw, sin yaw, 0). Then
right × up = (-sin yaw, -cos yaw, 0) × (0, 0, 1) = (-cos yaw, sin yaw, 0)
That is the direction the camera looks: into the screen. Determinant -1. The prototype’s viewer shows every tree as its own mirror image. At the default yaw of 45, screen-right is world (-1, -1, 0).
Twelve minutes of swapped buttons
On 8 May at 19
, commit711967f replaced the trunk-wiring compass with a persistent Up / Left / Down / Right selector. Its comment derived screen-right as “world (sin yaw, cos yaw)”, so Right meant angle 90 - yaw. That is the honest right-handed answer: (sin yaw, cos yaw, 0) × (0, 0, 1) = (cos yaw, -sin yaw, 0), toward the viewer. I derived it as if the screen showed the world, and it shows the mirror. Picking Left bent the trunk right. At 20, fe40610, “Fix swapped trunk-bend Left/Right”, traded the two cases. Its message blames “the combo of z-up world, (0,0,-1) up vector in matrix_build_lookat, and negated aspect ratio” and says “Up/Down sit on the camera-radial axis and aren’t affected.”
That last line is the linear algebra. A reflection leaves every arrow in its mirror plane alone and reverses the one arrow perpendicular to it. Here the mirror swaps screen-left and screen-right, so its plane holds the line of sight and the vertical. Up bends the trunk away from the camera (180 - yaw) and Down toward it (-yaw): both in the plane, both untouched. My derivation was right for two buttons and backwards for exactly the other two.
The branch buttons, which landed the next evening (5a3a494), don’t derive anything. _branch_bend_sign bends a copy of the branch by 5 degrees, projects its tip through the real camera before and after, and checks the screen movement against the chosen direction with a dot product. It asks the pipeline instead of reasoning about it, so no mirror can fool it. The port keeps both, as trunkBendWorldAngle and branchBendSign in src/viewer/hotspots.ts.
Why a toy scene never notices
A mirror only shows when the scene has a handedness someone acts on. A spinning cube or a symmetric tree looks the same in the mirror. All the upside-down debugging was about up and down, which a left-right mirror can’t touch; the mirror sat in the viewer from the first day and showed only when a button said Left. I copied the negative aspect from a tutorial pattern, and I can’t vouch for what any one tutorial does. My point is narrower: a pattern checked on a scene with nothing left- or right-handed in it passes every check it gets.
Carrying the mirror into Three.js
The TypeScript port doesn’t turn the sim’s z-up into Three.js’s default y-up. It scales centimetres to display units (DISPLAY_SCALE, 4 units to the metre), sets the camera’s up to (0, 0, 1) in TreeViewer.ts, and keeps the sim’s axes. But Three.js is right-handed and its aspect is positive, so the same numbers would show the world unmirrored, and every formula ported from the prototype, trunkBendWorldAngle included, would come out swapped again.
So the port adds the mirror on purpose. toViewerSpace in src/viewer/camera.ts is one flip:
export function toViewerSpace(p: Vec3): Vec3 {
return { x: p.x, y: -p.y, z: p.z };
}
The meshes sit in a group scaled (1, -1, 1), the camera’s eye and target go through toViewerSpace in placeCamera, and so do the hotspots in projectToScreen. One flip, odd again. threeCamera.test.ts pins it: a point (-1, -1, 0) from the target lands right of centre at yaw 45, and +z lands above.
Flipping x would have worked as well, provided the camera went through the same flip. The two flips differ by a half turn about the vertical axis, which turns tree and camera together and leaves the picture alone. The mistake to avoid is mirroring the meshes and not the camera, or the other way round.
One side effect: a determinant of -1 reverses the order in which every triangle’s corners go round on screen, its winding, which is how the GPU tells a triangle’s front from its back. The viewer draws every triangle in both windings (bothWindings in src/viewer/toon.ts), so the reversal hides nothing.
What I take from it
Count the flips: an odd count is a mirror, and one triple product confirms it. And when a direction matters to a player, test it through the real projection on something lopsided.
Coming next
What scoring 100 actually means: the number on the inspector, taken apart as a weighted average of bounded criteria with a vitality multiplier on top.