ARM LAB / STUDY / LEVEL 7 OF 8
Driving
it.
Because the arm's state is six numbers, driving it means writing into those numbers, and every input is just a different way to do that. Keys 1–6 and the beam choose which number; keys J/K and either thumbstick add or subtract a little every frame. The stick's direction is made to match what you see: the scene nudges the motor in forward kinematics, looks where the gripper would go, and picks the sign so that pushing right moves it to your right and pushing forward moves it up or away. The beam needs one more idea, the hit test: which joint ring is it pointing at? And the target ball is dragged along the beam at a fixed distance, the "hand" grab from beamSphere's lesson. This page runs copies of the scene's ray, nearest and input code, seen from above.
From above: the beam and the rings
move the mouse to aim · click = trigger · hold on the ball to drag itThe controller sits where the player's hand would be. Rings are the small circles on the motors; the one the beam passes within 3 cm of (6 cm in the doubled scene) lights up. Looking straight down, this page ignores heights; the scene tests in 3D. Dashed: the beam's full line, including the part behind the controller that never counts.
Inputs
What happened
Controller buttons, as the framework numbers them
| # | right | left | used for |
|---|---|---|---|
| 0 | trigger | trigger | press / drag / release / click events |
| 1 | grip | grip | free |
| 3 | stick press | stick press | the framework's "alt press" |
| 4 | A | X | free (the framework's room alignment needs both hands) |
| 5 | B | Y | free |
The beam and the hit test
armLab.jsray reads the beam: origin from column 3 of the controller's beam matrix, direction along its −z, both converted into scene coordinates. nearest is beamSphere.js's projection done for a list of points: the one closest to the beam, in front of the controller, within a radius. It returns which point and how far along the beam, which the drag needs.
Press, drag, sticks
armLab.jsThe trigger press decides once: a ring selects a joint, the ball starts a drag that remembers its distance along the beam. Every frame: a held ball rides at that distance, the sticks (with a dead zone) add to a number, and clampAngle keeps it legal. Keys do the same through held.
What you should be able to explain
- Choose once, move every frame. Selecting a joint or grabbing the ball happens on the press event. Turning and dragging happen every frame while the stick or trigger is held. Nothing re-decides what is held.
- Dead zone and dt. A released stick does not read exactly 0, so values under 0.15 count as 0 or the arm would creep. Multiplying by
dtmakes the speed the same at any frame rate: 1.2 rad/s at full deflection. - A stick should feel like the gripper, not like the motor. Each motor's positive direction is whatever the designers chose; the shoulder pan's +z, seen from the player, swings the gripper away.
stickSignshides that: it nudges the angle, measures where the tool centre point moves, and flips the sign so the stick and the gripper agree. The wrist roll and the gripper do not move the TCP, so for them right or forward is simply positive. - Fixed distance along the beam. The grab remembers
alongand places the ball ato + dir · alongeach frame. Swing fast and it still follows, unlike re-testing the hit every frame. - Swapping an input. To open and close the gripper with A and B instead of a stick, only the line that computes the input changes:
(buttonState.right[4].pressed ? 1 : 0) − (buttonState.right[5].pressed ? 1 : 0). The line that applies it stays.
Exercises: ① Aim past a ring from behind the controller (put the mouse on the far side): why does it not light up? ② Set the stick to 0.1 and watch: nothing moves. Why? ③ Select the shoulder pan: which stick axis moves it, and what are its two signs? ④ Grab the ball, then move the mouse quickly in a circle: what stays constant?