Skip to content
42-Hitchhikers-CollectivePublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

113 Commits

Folders and files

Repository files navigation

cub3d

This project has been created as part of the 42 curriculum by wlucke, dasal.

Language Graphics Platform


Description

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.


Features

Mandatory

  • DDA raycasting with perspective correction (no fisheye distortion)
  • Four directional wall textures (North / South / East / West)
  • Configurable floor and ceiling colors (RGB)
  • .cub map file parser with full validation (flood-fill enclosed-map check)
  • WASD movement + arrow key rotation
  • Collision detection

Bonus

  • 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

Controls

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

Map Format

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.


Build & Run

Requirements: Linux, gcc, X11 dev libraries (libx11-dev, libxext-dev)

# Build
cd cub3d
make

# Run
./cub3d maps/config1.cub

Five example maps are included (config1.cub – config5.cub).


Technical Details

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):

  1. Compute ray direction from camera plane and player angle
  2. Calculate delta distances for DDA grid traversal
  3. Step through the grid until a wall is hit
  4. Compute perpendicular wall distance (corrects fisheye)
  5. 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.


Project Structure

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, …)

Resources

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages