Skip to content

๐ŸŽฎ๐Ÿ•น๏ธ Cub3D is a simple 3D game using raycasting, made at 42 school โ€” inspired by Wolfenstein 3D.

Notifications You must be signed in to change notification settings

samir-ouaammou/3D-Game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐ŸŽฎ cub3D: Real-Time 3D Engine with Raycasting in C

๐Ÿ“– Introduction

๐Ÿ•น๏ธ cub3D is a raycasting-based 3D maze game developed as part of the curriculum at 1337 School (42 Network). Inspired by the legendary Wolfenstein 3D โ€” the pioneer of the first-person shooter genre โ€” this project aims to replicate a basic 3D experience using only 2D data and raw math logic.

With the help of MiniLibX, a minimalistic graphical library, I built a dynamic 3D environment rendered from a 2D map using the raycasting technique. This simulation allows real-time movement, wall detection, and texture mapping โ€” all from scratch in pure C.

๐ŸŽฏ The main objective:

Take a simple .cub map file, parse it, and render a realistic 3D first-person view of a maze, complete with player movement and textured walls โ€” while respecting strict memory management and code standards.

๐Ÿ’ก Why this project is exciting: Itโ€™s where math meets graphics. No OpenGL, no engines, no shortcuts. Just lines, pixels, and your brain.


Screenshot from 2025-07-11 17-34-59
Screenshot from 2025-07-11 17-31-02

๐ŸŽฏ Project Objectives

  • โœ… Apply C programming with graphics and math
  • โœ… Learn how raycasting works to simulate 3D from 2D
  • โœ… Parse a custom map file format (.cub)
  • โœ… Render a 3D view with textures using MiniLibX
  • โœ… Implement player movement and keyboard controls
  • โœ… Ensure clean memory management and no leaks

๐Ÿง  What is RayCasting?

Raycasting is a technique used in early 3D games to simulate a 3D perspective from a 2D map.
It works by casting imaginary rays from the player's point of view and calculating where each ray hits a wall. The result is a visual illusion of depth โ€” achieved with simple math and logic.

This method was famously used in games like Wolfenstein 3D, and it's the core rendering engine behind cub3D.

๐Ÿ” How it works:

  1. For every vertical line (column) on the screen, a ray is cast into the 2D map.
  2. Using an algorithm like DDA (Digital Differential Analyzer), we detect where the ray intersects a wall.
  3. The distance to the wall is calculated.
  4. Based on that distance, we draw a vertical line on the screen โ€” the closer the wall, the taller the line.
  5. A texture is applied depending on which side of the wall was hit (North, South, East, or West).
  6. This process repeats for every column, forming a full frame โ€” creating the illusion of a 3D space.

image

โœจ Why is it useful?

Raycasting is fast, efficient, and doesnโ€™t require full 3D geometry or heavy graphics engines.
Itโ€™s perfect for simple games where performance and control matter โ€” and a fantastic way to understand the math behind 3D rendering.

With raycasting, you're not rendering a world โ€” you're calculating one.


๐Ÿ”ง In cub3D

Raycasting is used to turn a .cub map file into a full 3D environment where you can move, rotate, and interact in real time โ€” with nothing but C, math, and the MiniLibX graphics library.


โš™๏ธ Features - Mandatory

Feature Description
๐Ÿงฑ Map Parsing Parse .cub file and validate structure
๐ŸŽฎ Player View First-person camera with W/A/S/D + arrow keys
๐Ÿ–ผ๏ธ Texture Support Load and apply 4 wall textures (N/S/E/W)
๐Ÿง  Raycasting Simulate 3D walls from 2D map using DDA
๐Ÿ–Œ๏ธ Floor & Ceiling Different RGB colors for floor and ceiling
๐ŸชŸ Window Handling MiniLibX window with proper close handling
๐Ÿงผ Error Handling Descriptive errors for malformed files
๐Ÿ’พ Memory Safe No leaks, crashes, or norm errors

๐Ÿ’Ž Bonus Features

Bonus Description
๐Ÿงฑ Wall Collision Prevent walking through walls
๐Ÿ—บ๏ธ Minimap 2D top-down view of the maze with player view
๐Ÿšช Doors Openable/closable doors with keypress
๐ŸŒ€ Animated Sprites Moving enemies, items, etc.
๐Ÿ–ฑ๏ธ Mouse Look Rotate view using mouse movement
๐ŸŽฏ HUD Add health bar, weapons, shooting system

๐Ÿ—บ๏ธ The .cub File Format

NO ./textures/north.xpm
SO ./textures/south.xpm
WE ./textures/west.xpm
EA ./textures/east.xpm

F 02,0,0
C 01,0,0

1111111
1000001
111D111
10N0001
1111111

Elements:

  • NO/SO/WE/EA: Texture paths
  • F / C: Floor and ceiling colors (RGB)
  • Map symbols:
    • 1: Wall
    • 0: Floor
    • D: Door
    • N/S/E/W: Player start position + orientation

Map must be closed (no open edges).


๐Ÿ“‚ Directory Structure

cub3d/
โ”œโ”€โ”€ src/                # All .c files
โ”œโ”€โ”€ includes/           # .h headers
โ”œโ”€โ”€ textures/           # .xpm textures
โ”œโ”€โ”€ maps/               # .cub map files
โ”œโ”€โ”€ libft/              # Your own libft copy
โ”œโ”€โ”€ Makefile
โ””โ”€โ”€ README.md

๐Ÿ› ๏ธ Installation

  1. Clone the repository to your local machine:

    git clone https://github.com/samir-ouaammou/3D-Game
  2. Navigate to the project directory:

    cd 3D-Game/Project
  3. Compile the source files using make:

    make 
  4. Clean up compiled files:

    make clean
  5. To remove all object files and the executable:

    make fclean
  6. To recompile the project from scratch:

    make re
  7. Run the program:

    ./cub3D maps/map2.cub
Key Action
W Move forward
S Move backward
A Move left
D Move right
E Open Door
โ† / โ†’ Rotate view
ESC Exit
Red [X] Close window

Screenshot from 2025-07-11 19-07-47

๐Ÿ› ๏ธ Tools and Dependencies

  • ๐Ÿ–ฅ๏ธ C (C99)
  • ๐ŸŽจ MiniLibX
  • ๐Ÿงฎ <math.h>
  • ๐Ÿงฐ valgrind for memory leaks
  • ๐Ÿงช 42 Norm compliant

๐Ÿ“ˆ What I Learned

  • ๐Ÿ“Š 3D rendering techniques without OpenGL
  • ๐Ÿง  Raycasting & DDA algorithm
  • ๐Ÿงต Event-driven programming with MiniLibX
  • ๐Ÿ“‚ File parsing and validation
  • ๐Ÿ”ฅ Manual memory management in C
  • ๐Ÿšช Map design and file formats

๐Ÿ“š Recommended Resources

Want to dive deeper? Here are some great resources to understand raycasting better:


๐Ÿง  Author

๐Ÿ‘ค Samir Ouaammou
Student at [1337 School โ€“ UM6P, 42 Network]
๐Ÿ–ฅ๏ธ Passionate about low-level programming, graphics, and game dev
๐Ÿ“ซ Connect with me on LinkedIn


Thank you for checking out my cub3D project! Stay tuned for more exciting challenges. ๐Ÿ”ฅ

About

๐ŸŽฎ๐Ÿ•น๏ธ Cub3D is a simple 3D game using raycasting, made at 42 school โ€” inspired by Wolfenstein 3D.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •