SDL Raytracer
2023-06-26
Source: raycaster-sdl
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. It casts one ray per screen column into a 2D grid map and draws 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 shootRay right was most of the project. It walks a ray outward along vertical and horizontal grid-line crossings separately, picks whichever hits a wall first, and corrects 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 where one was going wrong.
I also built a separate little map editor (maker.c) so I could paint 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, and enemies and weapons were the natural next step.