📚 Encyclopedia

Games & Graphics · updated 2026-10-13

DDA raycasting (turning a top-down game into first person)

raycastingddapseudo-3dcanvaswolfensteinjavascript

A grid raycaster (the Wolfenstein 3D technique) draws a first-person view by casting one ray per screen column through a 2D grid of walls. Because it only needs a 2D world, an existing top-down game whose physics already lives in world space (x, y, angle) can be converted by replacing only the renderer. Done for a small vanilla-JS racer ("Vanilla Velocity") in 2026-08.

Steps

  1. Rasterize the world into a wall grid. For a racetrack defined by a centerline: for every cell (e.g. 16 px), mark it wall if its center is farther than the road half-width from the nearest centerline point. Store in a Uint8Array; treat out-of-bounds as wall.
  2. Cast per column with DDA (as in Lode Vandevenne's tutorial): ray direction from the camera angle plus the column's offset across the field of view; deltaDist = |1/dir| per axis; step to whichever side is nearer (sideDistX vs sideDistY) until a wall cell is hit.
  3. Perpendicular distance (not Euclidean) avoids the fisheye effect; wall height = constant / distance. Shade by which side was hit and by distance; stripe kerbs for speed cues.
  4. Floor and ceiling: gradients are enough (sky and asphalt).
  5. Sprites (other cars): sort by distance, project to a screen x and scale by distance, and skip columns where the stored per-column wall distance (a 1D z-buffer) is nearer, so walls occlude them.

What changes for game feel

Gotchas

See also

Linked from

Sources: forge ai-activity (tldr post #1596)