ECHO GARDEN / STUDY / LEVEL 2 OF 7
Pointing
with a beam.
The panel is 1.5 m away, out of reach, so you point at it. A controller beam is a straight line: a starting point o and a direction d. Two questions are asked of that line all the time: where does it hit the panel? (to plant a note) and which note does it pass closest to? (to grab one). Both are a few lines of vector arithmetic, the same as in the course's beamSphere.js. This page draws the garden from the side and runs those lines with the game's cg.js.
The garden from the side
move the mouse to aim · drag the controller to walkThe panel is on the left; you stand on the right. Dashed lines are each note's shortest distance to the beam. A circle around a note is its 8.5 cm grab radius.
The numbers
aiming- origin o (y, z)
- direction d (y, z)
- t to the panel
- hit point (y)
- closest note
- its distance
Try this: drag the controller to the left, past a note that sticks out of the panel, then aim away from the panel. Untick the box and see which note the beam "grabs".
The beam and the panel
gameKit.jsray reads the beam from the controller: its origin is column 3 of the beam matrix and its direction is minus column 2, both turned into the garden's coordinates by the inverse of worldCoords. plane walks along the beam until z reaches the panel. pointsAt asks whether the beam passes within a radius of a point.
Where the scene uses them
echoGarden.jspointOnPanel plants only where the beam meets the panel. nearestSeed is beamSphere.js's projectOntoBeam, done for every note, keeping the closest within 8.5 cm, and skipping notes behind the controller.
What you should be able to explain
- A beam is a line. Every point on it is
o + d·tfor some distancet. Withdof length 1,tis in metres. - Hitting the panel. The panel is the flat surface where z equals −1.455. Solving
o.z + d.z·t = zgivest = (z − o.z) / d.z. A negativetmeans the panel is behind you, and a tinyd.zmeans the beam runs parallel to it: both give no hit. - The closest point to a note.
along = dot(note − o, d)is how far along the beam the note's "shadow" falls. The note's distance from the beam is the length of(note − o) − d·along. This is the projection frombeamSphere.js. - Why skip
along ≤ 0? The projection works on the whole infinite line, including the part behind the controller. Without the check you could grab a note behind your hand by pointing away from it.
Exercises: ① Aim exactly level (d.y = 0): what is t, and why? ② Aim upward, away from the panel: why does the hit point disappear? ③ Change 8.5 cm to 3 cm in your head: what gets harder for a player whose hand shakes?