Screencast.from.10-05-2024.06.56.41.PM.webm
Cub3D is a raycasting-based 3D game engine developed as part of the 42 School curriculum. The goal is to simulate a 3D environment in a 2D grid world by calculating the intersections of rays with walls and projecting them to create a first-person view. This project focuses on concepts like raycasting, texture mapping, collision detection, and efficient rendering using the MiniLibX library.
- Raycasting Engine: Renders a 3D perspective from a 2D map using raycasting.
- Texture Mapping: Textures are applied to walls to enhance visual realism.
- Dynamic Player Movement: The player can move in all directions using the keyboard, and rotate the the point of view with arrows/mouse.
- Collision Detection: Ensures that the player cannot walk through walls.
- Doors: Doors behave like walls but use a different texture and are rendered conditionally.
- Gun animation: The player holds a gun that can shoot the shooting boards.
- Minimap: Yes, a minimap
To compile and run Cub3D, you will need:
- Linux machine (cuz MacOs is bad)**
- MiniLibX (included with the project for 42's student)
sudo apt-get update && sudo apt-get install xorg libxext-dev zlib1g-dev libbsd-dev
-
Clone the repository:
git clone https://github.com/SOHAIBALOUSSI/Cub3D.git cub3d cd cub3d
-
Compile the project:
make bonus
-
Run the game and Enjoy:
./cub3d_bonus maps/shooting_range.cub
- W / A / S / D: Move the player (forward, left, backward, right).
- Left / Right Arrow: Rotate the camera view left and right.
- E: Open/Close Doors.
- Mouse: Control the camera view freely in any direction.
- Left mouse click: Shoot 🔫.
- LCRTL: Show/Hide the mouse cursor.
- ESC: Exit the game.
-
Raycasting Algorithm:
The engine uses a modified version of the Digital Differential Analysis (DDA) algorithm for raycasting. It casts rays from the player’s position and calculates intersections with grid lines, determining the distance to walls and projecting them on the screen. -
Projection and Wall Rendering:
Wall height is calculated based on the distance from the player, and the appropriate texture is scaled and drawn vertically for each ray. This creates the illusion of depth in a 3D environment. -
Player Movement and Collision Detection:
The player’s position is updated based on key hooks, with collision detection ensuring that the player cannot pass through walls. Movements are based on a grid defined in the.cub
map file. -
Textures:
Textures are stored as images and mapped onto walls using the pixel's horizontal and vertical position. The MiniLibX library handles loading.xpm
texture files and drawing them to the screen. -
Doors:
Doors (represented by 'D' in the map file) behave like walls but with diffrent textures and can be opened with 'E'.
- Cute Sound Library: for managing sound effects in the project. Thanks to @Randy.