Workshop › Wolfenstein-style 3D Maze › Step 10 of 18
Now the ray walks: each time it crosses whichever grid line is nearer, it enters a new tile. As soon as that tile is not
floor, we found the wall. This method is called DDA. castRay returns how far the wall is, which kind of line it
crossed, the wall's kind and the exact hit point.
```js
// Walk the grid line by line (DDA) until the ray enters a wall tile.
while (true) {
let dist
let side
if (nextX < nextY) {
dist = nextX
nextX += deltaX
col += stepX
side = 'x'
} else {
dist = nextY
nextY += deltaY
row += stepY
side = 'y'
}
const tile = MAP[row][col]
if (tile !== '.') return { dist, side, tile, x: player.x + dx * dist, y: player.y + dy * dist }
}
```dist, the next one is deltaX further, the
ray is now one column over.side remembers which: 'x' means the wall is hit on its left or right face, 'y' on its top or bottom face.while (true) always finds one and returns.castRay, write the comment.return line with the while loop.Skills: Velocity and gravity, Loops
← A ray: the grid lines The 3D view →