06 - A ray that bounces
Drag the ring to reposition the source. The cross marks the aim and follows the pointer.
cast(originX, originY, directionX, directionY, maxDistance, options)
Cast, reflect the direction about normalX and normalY, cast again from where the ray stopped, and take what it traveled out of the budget.
The loop ends when the budget runs out or the reflections do.
This is the first page that uses a normal rather than drawing it. Reflecting is one line: subtract twice the part of the direction that points into the surface.
- A reflected ray starts exactly on the surface it just met, and in floating point it may meet it again at distance zero and bounce there forever.
- Pushing the new origin a hundredth of a unit along the normal removes the whole class of problem.
outOfBounds: 1walls the map in, so the ray also bounces off its edges.