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
- 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. - 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 (sideDistXvssideDistY) until a wall cell is hit. - 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.
- Floor and ceiling: gradients are enough (sky and asphalt).
- 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
- A minimap becomes essential once you cannot see the whole track.
- Speed lines at high speed, camera bob and tilt tied to speed and steering, a vignette and red flash on impact.
- Top-down effects (skid marks, in-world confetti) do not translate; move celebrations to screen space.
- Keep the reduced-motion toggle covering bob and tilt as well as shake.
Gotchas
- A full rewrite like this is too big for a short headless AI run: several attempts at this and follow-up "make it a physics simulation" requests timed out at a 300 s CLI limit. Split big conversions into steps or raise the timeout.
See also
- Game juice checklist (feedback layers that make a game feel good)
- Canvas 2D and SVG animation techniques for web scenes