← Back to projects

cub3D

A raycasting 3D engine written in C, in the spirit of Wolfenstein 3D.

Period
15/06/2023
Category
42
Area
Systems
Grade
110
Stack
  • C
Links

A raycasting engine in C, in the spirit of Wolfenstein 3D (1992) — the game that invented the first-person shooter.

What it does

The program reads a scene file and renders a maze in first person, in real time, in a MiniLibX window. Walls are textured per cardinal direction, floor and ceiling take their colours from the file. Arrow keys turn the camera, W A S D move, M toggles the minimap, ESC quits.

The scene file

A .cub file holds four texture paths (NO, SO, WE, EA), a floor and a ceiling colour as RGB triplets, and the map itself: 0 for empty space, 1 for a wall, and one of N S E W for the player’s position and initial heading. The parser works in two phases — the six configuration elements first, then the map, collected in a linked list since its size is not known in advance — and validates everything before a window is ever opened: the maze must be closed, and it must hold exactly one spawn point.

Raycasting

For each column of the window, a ray leaves the camera and steps through the grid until it hits a wall. The distance to that wall, corrected for the fisheye effect, gives the height of the column to draw; the side that was hit gives the texture, and the exact point of impact gives the texture column.

Bonus

Wall collision detection, and a minimap overlay showing the player’s position in the maze.