A simple raycasting engine inspired by Wolfenstein 3D, developed as part of the 42 school curriculum at Hive Helsinki. This project creates a 3D graphical representation of a maze using raycasting principles.
cub3d_video.mp4
cub3D is a first-person perspective maze renderer inspired by Wolfenstein 3D. The rendering process happens in three main steps:
-
Basic Scene Creation: First, we draw the ceiling and floor until the middle of the screen using solid colors defined in the map file. This creates the basic structure of our 3D view.
-
Wall Rendering: For each vertical line on the screen, we calculate the distance from the player to the wall. Closer walls appear taller and wider, while distant walls appear shorter and thinner, creating the 3D perspective effect.
-
Texture Mapping: Finally, we apply textures to the walls based on their orientation (North, South, East, West). The textures are mapped onto the walls according to where the ray hits the wall, creating the final detailed appearance.
git clone https://github.com/pmarkaid/cub3D.git
cd cub3D
make# runt he program with the desired map
./cub3D maps/valid_subject.cubW: Move forwardS: Move backwardA: Strafe leftD: Strafe right←: Rotate camera left→: Rotate camera rightESC: Exit the game
- The map is represented with
1for walls,0for empty space and a spawing point, that can be any of the lettersN|S|W|Ethat represent the starting player view direction. - The map must be closed/surrounded by walls
- The game implements a buffer zone around the defined map
- An outer wall with collision detection surrounds this buffer zone
- While there's no collision detection with interior walls, you cannot exit the buffer zone
Example map file format:
NO textures/Ncolor.png
SO textures/Scolor.png
WE textures/Wcolor.png
EA textures/Ecolor.png
F 117,92,60
C 14,62,89
1111111111111111111111111
1000000000110000000000001
1011000001110000000000001
10010000000000000W0000001
111111111011000001110000000000001
100000000011000001110000000011111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
1100000111010101111101111000111
11110111 1110101 101111010001
11111111 1111111 111111111111
Raycasting creates a 3D perspective by sending out rays from the player's position and using the Digital Differential Analysis (DDA) algorithm to efficiently find wall intersections.
- For each vertical screen column, a ray is cast from the player's position
- The ray's direction is calculated based on the player's viewing angle and the screen column position
- When the ray hits a wall, we calculate the distance to determine the wall's height on screen
The DDA algorithm efficiently finds wall intersections by:
- Calculating the ray's direction and initial position
- Computing the distance to the next X and Y grid intersections
- Moving step by step through the grid by choosing the shortest distance
- Stopping when a wall is hit
The algorithm is particularly efficient because:
- It only checks grid cells the ray actually passes through
- It avoids floating-point arithmetic where possible
- It naturally finds the closest wall intersection
- The perpendicular distance to the wall is used to avoid fisheye effect
- Wall height is calculated as: screenHeight / distance
- Texture coordinates are determined by where exactly the ray hits the wall
- Final color is sampled from the appropriate wall texture

