Workshop › Wolfenstein-style 3D Maze › Step 8 of 18

A ray: where it starts

What we are doing

Here is the big idea of raycasting: for every column of the screen we send a ray from the player's eye and find the first wall it hits. The nearer the wall, the taller we draw that column. This step starts the ray: its direction, the tile it starts on, and which way it moves through the grid. For now castRay returns these so we can check them.

```js
function castRay(angle) {
  const dx = Math.cos(angle)
  const dy = Math.sin(angle)
  let col = Math.floor(player.x)
  let row = Math.floor(player.y)
  const stepX = dx > 0 ? 1 : -1
  const stepY = dy > 0 ? 1 : -1
  return { col, row, stepX, stepY }
}
```

What it means

Your turn

Above the key listeners, write castRay.

Skills: Velocity and gravity, Functions

All steps

  1. The map
  2. The player
  3. Ceiling, floor and a loop
  4. Turn around
  5. Walk
  6. Is it a wall?
  7. Stop at walls, slide along them
  8. A ray: where it starts
  9. A ray: the grid lines
  10. A ray: walk to the wall
  11. The 3D view
  12. No fish-eye
  13. Light and shade
  14. A timer
  15. Escape!
  16. Best time
  17. Touch controls
  18. Build it yourself: run