A first-person 3D maze game built from scratch in C using ray-casting β inspired by the original Wolfenstein 3D engine.
"This project taught me how the first 3D games were actually rendered β using ray-casting instead of a real 3D engine, projecting a 2D map into a 3D perspective in real time. I learned the math behind field of view, angular projection, texture mapping, and game loop design. This is the foundation of every graphics engine, from game development to augmented reality."
Ray-casting is the original rendering optimization that made real-time 3D games possible in the early 90s. Understanding it gives you deep intuition for how modern rendering pipelines β including GPU shaders and AR engines β work.
A first-person perspective rendered from a .cub map file:
111111
100001
1N0001
100001
111111
1= wall0= open spaceN/S/E/W= player spawn + initial facing direction
- Ray-caster β casts rays from the player's POV to calculate wall distances and heights
- Texture rendering β loads
.xpmtextures and maps them onto walls based on ray hit position - Map validator β verifies the map is fully enclosed, has exactly one player, and only valid characters
- Movement system β WASD movement + left/right rotation with collision detection
- Minimap (bonus) β real-time overhead view of the maze
- Color rendering β configurable floor and ceiling colors from the
.cubfile
The project is organized into distinct subsystems β raycast/, validate_map/, interpretate_map/, texture/, color/, movements/, minimap/ β each in its own directory with focused responsibilities. This kind of domain-driven directory structure scales well and makes the codebase navigable, a pattern seen in production game engines and large C projects.
# On Ubuntu/Debian
sudo apt-get install libxext-dev libx11-dev libmlx-devgit clone https://github.com/gustavofsousa/cub3D.git
cd cub3D
make./cub3D maps/map1.cubNO ./textures/north.xpm
SO ./textures/south.xpm
WE ./textures/west.xpm
EA ./textures/east.xpm
F 220,100,0
C 225,30,0
111111
100001
1N0001
100001
111111
| Key | Action |
|---|---|
W A S D |
Move forward / strafe left / backward / strafe right |
β β |
Rotate camera |
ESC |
Quit |
cub3D/
βββ source/
β βββ main.c
β βββ setup.c
β βββ render_game.c
β βββ raycast/ # Ray-casting algorithm
β βββ texture/ # XPM texture loading and mapping
β βββ color/ # Floor/ceiling color rendering
β βββ validate_map/ # Map validation logic
β βββ interpretate_map/ # Map file parsing
β βββ movements/ # Player movement and collision
β βββ minimap/ # Bonus: overhead minimap
β βββ hooks_keyboard.c # Input handling
βββ include/
β βββ cub3d.h
β βββ structs.h
β βββ constants.h
βββ maps/ # Sample .cub map files
βββ textures/ # XPM texture files
βββ libft/
- Ray-casting rendering algorithm (DDA β Digital Differential Analysis)
- XPM texture loading and pixel-level mapping
- Real-time game loop with keyboard event handling
- Complex map validation (flood fill, boundary checking)
- Modular architecture in a large multi-file C project
- Trigonometry applied to camera projection (FOV, angle calculations)
This project was developed as part of the 42 School curriculum.
Made with β at 42 Rio de Janeiro