POCKET CITY / STUDY / LEVEL 2 OF 5

Picking a cell
with a beam.

In VR you build by pointing the controller beam at a cell and pulling the trigger. But a lot is a crowd of cubes: pointing at the side of a stack must pick that stack, not the table behind it, and must pick the nearest cube when several are in line. So Pocket City does not ask "which point is the beam closest to" (the Arm Lab test); it asks "which box does the beam enter first". That is a ray–box intersection, rayBoxDistance, and this page runs the scene's own version, seen from the side.

the beamorigin and direction, from the controller
→
every boxeach cube of the example, then the nine floor tiles
→
rayBoxDistancehow far along the beam the box is entered, or null
→
the smallest distance winsthat cube's column is the cell to build on

The gold lot from the side

move the mouse to aim · drag the controller to move it

One row of the lot (z = 1), three columns. The first box the beam enters is outlined; the beam is dashed behind the controller.

The numbers

Each box along the beam

The ray–box test

pocketCityRules.js

For each axis, the beam enters the box's slab at one distance and leaves it at another. The beam is inside the box only while it is inside all three slabs at once: the latest entry must come before the earliest exit. near is where it enters; a box behind the controller (far ≤ 0) is ignored. pickExampleCell runs this for every cube and every floor tile and keeps the smallest distance.


      

Where the scene uses it

pocketCity.js

pick tests the cells and the buttons with the same function and keeps whichever is nearer. onPress then either chooses a material or button, stacks a cube on the cell, or reports that nothing was aimed at.


      
Full files: pocketCityRules.js ↗ · pocketCity.js ↗ · compare the Arm Lab's nearest ↗

What you should be able to explain

  1. Slabs. A box is where three slabs overlap: x between two planes, y between two, z between two. Entering the box means having entered all three; leaving means having left any one.
  2. Why not "closest to the beam". That test, used for the arm's joint rings, would happily pick a cube that the beam passes near but behind another cube. Entering first is what a person means by "pointing at".
  3. Cubes before tiles. Cubes and floor tiles are all boxes in one list, so a beam that hits a stack picks the stack; a beam that reaches the floor between stacks picks the empty cell.
  4. Buttons use the same test. The swatches and GROW CITY are thin boxes; the nearest hit of any kind wins, so a button in front of the lot is never mistaken for a cell.

Exercises: ① Aim at the side of the tall stack: which box is entered first, and at what distance? ② Tick the comparison: find an aim where the two tests disagree. ③ Move the controller below the table and aim up through it: why is the floor tile not picked?