← Back to ratslair.com
aboutsummaryrefslogtreecommitdiff

SDL Raycaster

A Wolfenstein 3D-style raycasting renderer in C, chasing the same trick old engines like Wolfenstein 3D and early Doom builds used to fake 3D on hardware that couldn't really do 3D: cast one ray per screen column into a 2D grid map, and draw a vertical wall strip whose height depends on how far that ray traveled before hitting something. No GPU pipeline, no triangles, just trigonometry and a grid.

Getting the ray-casting step right was most of the project. It walks a ray outward along vertical and horizontal grid-line crossings separately, picking whichever hits a wall first, and has to correct the returned distance for the fisheye effect you'd otherwise get from measuring along the ray angle instead of straight ahead. A 2D minimap drawn alongside the 3D view, showing the map grid with every cast ray overlaid on it, made that math a lot easier to debug. You could actually watch the rays fan out and see exactly where one was going wrong.

There's also a separate little map editor (maker.c) for painting out wall layouts by hand instead of hardcoding a test map into the renderer: click tiles, pick a wall type with the number keys, save it out as a plaintext .map file.

Movement, collision, and a working minimap all came together; enemies and weapons were the natural next step.

Controls

Renderer (doom):

  • W / S: move forward/back
  • A / D: strafe
  • Q / E: turn
  • Mouse: look

Map maker (maker):

  • Number keys: pick a wall type
  • Click: paint the hovered tile
  • Saves the layout to a .map file for the renderer to load

Building

Needs SDL2, SDL2_ttf, and libm.

make        # builds the renderer -> ./doom
make maker  # builds the map editor -> ./maker

License

GPLv3, see LICENSE.