05 - Moving a rectangle
The green rectangle goes as far towards the pointer as it can. Drag anywhere on the map to pull it.
cast(originX, originY, directionX, directionY, maxDistance, options)
The only method the page uses, and the answer to a question the library does not ask: how far can a rectangle go?
A row of rays leaves the leading edge of the box in the direction of travel, and the shortest of them decides.
- The outermost two rays start half a unit inside the corners, or a box sliding along a wall would catch on it.
- The two axes are resolved one at a time, sideways first, each from the edge that leads on that axis.
- A single diagonal test cannot tell which side of a corner it belongs to, and whichever it picks is wrong half the time.
- Resolving separately is also what makes the box slide along a wall instead of stopping dead against it.
Anything narrower than the spacing between rays can pass between two of them unseen. Keep the spacing below the size of the smallest obstacle and it never comes up.