Skip to main content

Welcome to Cub3D

Cub3D is a 3D raycasting engine written in C that recreates the classic first-person perspective of games like Wolfenstein 3D. This project demonstrates fundamental computer graphics techniques, including raycasting algorithms, texture mapping, and real-time rendering.
Cub3D is a 42 School project that explores raycasting principles without using external 3D rendering libraries.

What is Raycasting?

Raycasting is a rendering technique that creates a 3D perspective in a 2D map by casting rays from the player’s viewpoint. For each vertical line on the screen, a ray is cast into the game world to determine:
  • Distance to the nearest wall - determines the height of the wall slice
  • Wall orientation - determines which texture to apply (North, South, East, or West)
  • Texture coordinates - maps the correct portion of the texture to the wall
The algorithm uses Digital Differential Analysis (DDA) to efficiently traverse the map grid and detect wall collisions.

Key Features

Real-time 3D Rendering

Renders a 3D perspective at 1732x1000 resolution using raycasting algorithms implemented in raycasting.c

Texture Mapping

Supports XPM texture files for all four wall directions (North, South, East, West)

Smooth Movement

WASD movement and arrow key rotation with collision detection

Custom Map Format

Parse .cub map files with configurable textures, colors, and layouts

Technical Overview

Core Components

  • Raycasting Engine (raycasting.c:102-127) - Main rendering loop that casts rays across the screen width
  • DDA Algorithm (raycasting.c:15-66) - Digital Differential Analysis for efficient grid traversal
  • Texture Mapping (drawing.c) - Maps wall textures based on ray intersection points
  • Map Parser - Validates and loads .cub map files with texture paths and RGB color definitions

Window Resolution

The engine renders at a fixed resolution defined in cub3d.h:30-31:
#define WIDTH 1732
#define HEIGHT 1000

Field of View

The camera’s field of view is controlled by the plane vector perpendicular to the direction vector:
#define POV 1.17  // Approximately 66 degrees FOV

Map File Format

Cub3D uses .cub files to define game worlds. Here’s an example from maps/COME.cub:
NO ./pizza.xpm
SO ./doom2.xpm      
WE ./doom3.xpm
EA ./eagle.xpm

F 220,100,0
C 225,30,0

111
1N1
111
Map Elements:
  • NO/SO/WE/EA - Texture paths for North, South, West, East walls
  • F - Floor color in RGB format (0-255)
  • C - Ceiling color in RGB format (0-255)
  • 1 - Wall
  • 0 - Empty space
  • N/S/E/W - Player starting position and direction

Architecture

The project is structured around a central t_map structure (cub3d.h:79-162) that contains:
  • Map data and dimensions
  • Player position and direction vectors
  • Texture information for all four walls
  • MLX window and image pointers
  • Raycasting calculation variables

Next Steps

Installation

Set up dependencies and build the project

Quick Start

Run your first 3D scene in minutes