This project has been created as part of the 42 curriculum by wlucke, dasal.
cub3d renders a navigable first-person 3D world from a simple 2D grid map using the DDA (Digital Differential Analysis) raycasting algorithm. Every frame, rays are cast per screen column to determine wall distances and texture offsets — producing a perspective-correct, textured 3D view without any 3D graphics API.
The project is split into two parts: a mandatory raycaster with textured walls and a bonus implementation adding doors, animated sprites, a minimap, and mouse look.
- DDA raycasting with perspective correction (no fisheye distortion)
- Four directional wall textures (North / South / East / West)
- Configurable floor and ceiling colors (RGB)
.cubmap file parser with full validation (flood-fill enclosed-map check)- WASD movement + arrow key rotation
- Collision detection
- Interactive doors — 11-frame open/close animation, toggleable with Space
- Animated sprites — collectible key with cycling animation frames
- Real-time minimap — 10×10 overhead overlay with player direction indicator
- Mouse look — free-look camera control with configurable sensitivity
- Sprite depth sorting — back-to-front ordering relative to walls
| Key | Action |
|---|---|
W A S D |
Move forward / left / backward / right |
← → |
Rotate camera |
| Mouse | Free look (horizontal) |
Space |
Interact with door (requires key) |
ESC |
Quit |
Maps are defined in .cub files:
NO ./textures/walls/brks_b.xpm
SO ./textures/walls/brks_g.xpm
WE ./textures/walls/brks_r.xpm
EA ./textures/walls/brks_y.xpm
C 226,186,255
F 178,255,186
1111111111111111
1E00000D000001
10000000000001
1D00000D000001
10010000001001
11111111111111
| Character | Meaning |
|---|---|
1 |
Wall |
0 |
Open floor |
D |
Door (interactive) |
N/S/E/W |
Player spawn + initial direction |
The parser validates that all walkable areas are fully enclosed by walls using a flood-fill algorithm.
Requirements: Linux, gcc, X11 dev libraries (libx11-dev, libxext-dev)
# Build
cd cub3d
make
# Run
./cub3d maps/config1.cubFive example maps are included (config1.cub – config5.cub).
| Constant | Value |
|---|---|
| Resolution | 1920 × 930 |
| Target FPS | 59 |
| Texture size | 128 × 128 px |
| FOV | ~66° |
| Move speed | 7 units/s |
| Max sprites | 128 |
Raycasting pipeline (per column, per frame):
- Compute ray direction from camera plane and player angle
- Calculate delta distances for DDA grid traversal
- Step through the grid until a wall is hit
- Compute perpendicular wall distance (corrects fisheye)
- Sample the appropriate texture column and rasterize to screen
Sprite rendering: sprites are transformed into camera space, depth-sorted back-to-front, projected onto the screen, and rendered column-by-column — skipping transparent pixels.
Delta time: movement and rotation speeds are frame-rate independent, scaled by elapsed time per frame.
cub3d/
├── 0_main.c Entry point, MLX window & event loop
├── 1_init*.c Initialization, texture loading
├── 2_parsing_*.c .cub file parsing & map validation
├── 3_render_*.c Raycasting, walls, doors, sprites, minimap
├── 4_controller_*.c Keyboard & mouse input, movement
├── 8_errors_0.c Error handling
├── 9_utils_*.c Utilities, memory management, timing
├── cub3d.h Master header (structs, constants, prototypes)
├── maps/ Example .cub map files
├── textures/ Wall, door, and sprite XPM assets
└── libft/ Custom C standard library (ft_printf, GNL, …)
- Lode's Raycasting Tutorial — primary reference for the DDA algorithm
- MinLibX Documentation