Cub3D is a project from Ecole 42 that introduces basic 3D graphics programming using raycasting techniques. Inspired by classic games like Wolfenstein 3D, the project aims to create a simple first-person 3D game where the player navigates a maze and views the environment from a first-person perspective.
The Cub3D project is focused on building a 3D game engine using a 2D grid map. The core objective is to render 3D walls from a first-person view using raycasting. This project serves as an introduction to fundamental graphics programming, dealing with rendering, handling textures, and managing user inputs for movement and interaction.
We utilized the minilibx (mlx) graphics library to handle window management, rendering, and input handling. Raycasting, the core technique for rendering 3D perspectives, was implemented based on foundational mathematical concepts.
- Raycasting tutorial: lodev.org Raycasting Tutorial
- MiniLibX tutorial: 42docs MiniLibX Getting Started
- Implement a basic 3D rendering engine using raycasting.
- Handle player movement and interaction with the environment.
- Render textures and handle screen drawing efficiently using MiniLibX.
- Parse and load a game map from a file.
- Detect and handle collisions with walls, obstacles, and doors.
- Manage user input for navigation (move forward, backward, rotate view).
- First-Person 3D Rendering: Simulate a 3D environment from a first-person view using raycasting.
- Player Movement: The player can move forward, backward, and rotate the view left or right.
- Textured Walls: Display different textures for walls to enhance the visual experience.
- Map Parsing: Read and parse a game map from a file.
- Collision Detection: Prevent the player from walking through walls or doors.
The game map is defined in a .cub
file. This file represents the layout of walls (1
), doors (D
), and empty spaces (0
) in a grid format. Additionally, textures for walls in different directions (north, south, east, west) and the RGB colors for the floor and ceiling are also defined in this file.
- Texture Files: Paths to texture files for different wall directions:
NO
: Texture for north-facing wallsSO
: Texture for south-facing wallsWE
: Texture for west-facing wallsEA
: Texture for east-facing walls
- Colors: RGB values for floor (
F
) and ceiling (C
):F
: RGB color for the floorC
: RGB color for the ceiling
- Symbols:
1
: Wall0
: Empty spaceD
: DoorN / S / W / E
: Player's starting position and direction
NO ./textures/n_wall.xpm
SO ./textures/s_wall.xpm
WE ./textures/w_wall.xpm
EA ./textures/e_wall.xpm
F 111,78,55
C 173,216,230
111111111111111111111111111
1000000000110000000000001
1011000001110000000000001
1001000000D00000000000001
1111111110110000011100000000000N1
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001 1
10000000000000001101010010001 101
110000011101010111110111100011 1
11110111 1110101 101111010001
11111111 1111111 111111111111
- W: Move forward
- S: Move backward
- A: Strafe left
- D: Strafe right
- Left Arrow: Rotate view to the left
- Right Arrow: Rotate view to the right
- E: Interact (open doors)
- ESC: Exit the game
Raycasting is a mathematical technique used to simulate a 3D perspective by projecting rays from the player's viewpoint to detect walls (and doors) in the environment. Each ray is traced across the 2D map to find the distance to the nearest wall or door. This distance is then used to calculate how tall the wall should appear in the 3D rendering.
For a deeper understanding of raycasting and the mathematical calculations involved, refer to the excellent tutorial by Lode Vandevenne: Raycasting Tutorial: https://lodev.org/cgtutor/raycasting.html
We used the MiniLibX (mlx) library to handle graphics and input. It is a minimal graphics library provided by Ecole 42, which offers basic functionality for creating windows, drawing pixels, and managing events.
For more information on setting up and using MiniLibX, check out the following guide: MiniLibX Tutorial: https://harm-smits.github.io/42docs/libs/minilibx/getting_started.html
To run the game, follow these steps:
-
Compile the project:
make
-
Execute the game with a valid map file:
./cub3D maps/your_map.cub
The maps/your_map.cub file should follow the map structure defined in the project. Example map files are provided in the maps/ directory. You can also create your own map using the .cub format described in the documentation.