Skip to content

CUB3D is a 42 Network project that explores the fundamentals of raycasting, a rendering technique used in early 3D games. Using the MiniLibX library, this project creates a first-person perspective of a maze where the player can navigate through corridors with textured walls.

Notifications You must be signed in to change notification settings

msabr/CUB3D_1337

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

CUB3D_1337

42 School C Linux

A raycasting engine inspired by the legendary Wolfenstein 3D, the first true FPS in videogame history. This project implements raycasting techniques to render a dynamic 3D view inside a maze from a 2D map perspective.

๐Ÿ“‹ Table of Contents

๐ŸŽฎ About

CUB3D is a 42 Network project that explores the fundamentals of raycasting, a rendering technique used in early 3D games. Using the MiniLibX library, this project creates a first-person perspective of a maze where the player can navigate through corridors with textured walls.

Key Concepts:

  • Raycasting algorithm
  • DDA (Digital Differential Analysis) algorithm
  • Texture mapping
  • Field of view rendering
  • Player movement and collision detection

images

โœจ Features

Mandatory

  • โœ… Wall rendering with raycasting
  • โœ… Different textures for each cardinal direction (North, South, East, West)
  • โœ… Configurable floor and ceiling colors
  • โœ… Smooth player movement (W, A, S, D)
  • โœ… Camera rotation (Arrow keys)
  • โœ… Map parsing and validation
  • โœ… Proper error handling

Bonus

  • ๐ŸŽฏ Wall collision detection
  • ๐Ÿ—บ๏ธ Minimap display
  • ๐Ÿ–ฑ๏ธ Mouse rotation
  • ๐Ÿšช Doors (optional)
  • ๐ŸŽจ Animated sprites (optional)

๐Ÿ“ฆ Installation

  1. Clone the repository:
git clone https://github.com/msabr/CUB3D_1337.git
cd CUB3D_1337
  1. Compile the project:
# Mandatory part
make

# Bonus part
make bonus
  1. Clean build files:
make clean   # Remove object files
make fclean  # Remove object files and executable
make re      # Recompile everything

๐Ÿš€ Usage

Run the program with a map file:

./cub3D maps/mapSubject.cub 

or

 ./cub3D_bonus maps/mapSubject.cub

The program will:

  1. Parse the map file
  2. Validate the map structure
  3. Initialize the graphics window
  4. Render the 3D environment
  5. Wait for player input

๐Ÿ—บ๏ธ Map Format

Map files use the .cub extension and follow this structure:

NO textures/wall_Gry.xpm
SO textures/wall_Huf.xpm
WE textures/wall_Sly.xpm
EA textures/wall_Rev.xpm

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

        1111111111111111111111111
        1000000000110000000000001
        1011000001110000000000001
        1001000000000000000000001
111111111011000001110000000000001
100000000011000001110111111111111
11110111111111011100000010001
11110111111111011101010010001
11000000110101011100000010001
10000000000000001100000010001
10000000000000001101010010001
11000001110101011111011110N0111
11110111 1110101 101111010001
11111111 1111111 111111111111

Map Components:

Texture Paths:

  • NO - North wall texture
  • SO - South wall texture
  • WE - West wall texture
  • EA - East wall texture

Colors:

  • F - Floor color (RGB format: R,G,B)
  • C - Ceiling color (RGB format: R,G,B)

Map Characters:

  • 1 - Wall
  • 0 - Empty space (walkable floor)
  • N - Player starting position facing North
  • S - Player starting position facing South
  • E - Player starting position facing East
  • W - Player starting position facing West
  • (space) - Void (outside map boundaries)

Map Requirements:

  • Map must be surrounded by walls (1)
  • Must contain exactly one player starting position
  • Valid characters only: 0, 1, N, S, E, W, and spaces
  • Map must be closed (flood fill algorithm validation)

๐ŸŽฎ Controls

Key Action
W Move forward
S Move backward
A Strafe left
D Strafe right
โ† Rotate camera left
โ†’ Rotate camera right
ESC Exit program
Mouse Rotate view (bonus)

๐Ÿ”ฌ Technical Implementation

Raycasting Algorithm

The project implements raycasting using the DDA algorithm:

  1. Ray Generation: For each vertical strip of the screen, cast a ray from the player's position
  2. DDA Algorithm: Calculate ray intersection with the map grid
  3. Wall Distance: Compute perpendicular distance to avoid fisheye effect
  4. Wall Height: Calculate projected wall height based on distance
  5. Texture Mapping: Apply appropriate texture based on wall orientation
  6. Rendering: Draw vertical strips to create the 3D illusion

Key Formulas:

Fisheye Correction:

correct_distance = ray_distance * cos(ray_angle - player_angle)

Wall Height Projection:

wall_strip_height = (WALL_HEIGHT / correct_distance) * DISTANCE_TO_PROJECTION_PLANE
wall_top_pixel = (WINDOW_HEIGHT / 2) - (wall_strip_height / 2)
wall_bottom_pixel = (WINDOW_HEIGHT / 2) + (wall_strip_height / 2)

Field of View:

  • Standard FOV: 60 degrees
  • Window divided by width to determine ray angles
  • Each ray corresponds to one vertical pixel column

๐Ÿ“ Project Structure

cub3D
โ”œโ”€โ”€ Libft
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ”œโ”€โ”€ ft_atoi.c
โ”‚   โ”œโ”€โ”€ ft_bzero.c
โ”‚   โ”œโ”€โ”€ ft_calloc.c
โ”‚   โ”œโ”€โ”€ ft_is_number.c
โ”‚   โ”œโ”€โ”€ ft_isalnum.c
โ”‚   โ”œโ”€โ”€ ft_isalpha.c
โ”‚   โ”œโ”€โ”€ ft_isascii.c
โ”‚   โ”œโ”€โ”€ ft_isdigit.c
โ”‚   โ”œโ”€โ”€ ft_isprint.c
โ”‚   โ”œโ”€โ”€ ft_isspace.c
โ”‚   โ”œโ”€โ”€ ft_itoa.c
โ”‚   โ”œโ”€โ”€ ft_malloc.c
โ”‚   โ”œโ”€โ”€ ft_memchr.c
โ”‚   โ”œโ”€โ”€ ft_memcmp.c
โ”‚   โ”œโ”€โ”€ ft_memcpy.c
โ”‚   โ”œโ”€โ”€ ft_memmove.c
โ”‚   โ”œโ”€โ”€ ft_memset.c
โ”‚   โ”œโ”€โ”€ ft_putchar_fd.c
โ”‚   โ”œโ”€โ”€ ft_putendl_fd.c
โ”‚   โ”œโ”€โ”€ ft_putnbr_fd.c
โ”‚   โ”œโ”€โ”€ ft_putstr_fd.c
โ”‚   โ”œโ”€โ”€ ft_split.c
โ”‚   โ”œโ”€โ”€ ft_strcat.c
โ”‚   โ”œโ”€โ”€ ft_strchr.c
โ”‚   โ”œโ”€โ”€ ft_strcmp.c
โ”‚   โ”œโ”€โ”€ ft_strcpy.c
โ”‚   โ”œโ”€โ”€ ft_strdup.c
โ”‚   โ”œโ”€โ”€ ft_striteri.c
โ”‚   โ”œโ”€โ”€ ft_strjoin.c
โ”‚   โ”œโ”€โ”€ ft_strlcat.c
โ”‚   โ”œโ”€โ”€ ft_strlcpy.c
โ”‚   โ”œโ”€โ”€ ft_strlen.c
โ”‚   โ”œโ”€โ”€ ft_strmapi.c
โ”‚   โ”œโ”€โ”€ ft_strncmp.c
โ”‚   โ”œโ”€โ”€ ft_strndup.c
โ”‚   โ”œโ”€โ”€ ft_strnstr.c
โ”‚   โ”œโ”€โ”€ ft_strrchr.c
โ”‚   โ”œโ”€โ”€ ft_strrev.c
โ”‚   โ”œโ”€โ”€ ft_strstr.c
โ”‚   โ”œโ”€โ”€ ft_strtrim.c
โ”‚   โ”œโ”€โ”€ ft_substr.c
โ”‚   โ”œโ”€โ”€ ft_tolower.c
โ”‚   โ”œโ”€โ”€ ft_toupper.c
โ”‚   โ”œโ”€โ”€ get_next_line.c
โ”‚   โ””โ”€โ”€ libft.h
โ”œโ”€โ”€ Makefile
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ bonus
โ”‚   โ”œโ”€โ”€ cub3d_bonus.h
โ”‚   โ”œโ”€โ”€ main_bonus.c
โ”‚   โ”œโ”€โ”€ parsing_bonus
โ”‚   โ”‚   โ”œโ”€โ”€ parse_color_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_fill_map_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_line_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_map_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_memory_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_player_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_small_map_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_textures_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_utils_bonus.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_validate_map_bonus.c
โ”‚   โ”‚   โ””โ”€โ”€ parsing_bonus.h
โ”‚   โ””โ”€โ”€ rendring_bonus
โ”‚       โ”œโ”€โ”€ exit_game_bonus.c
โ”‚       โ”œโ”€โ”€ game_loop_bonus.c
โ”‚       โ”œโ”€โ”€ ray_cast_bonus.c
โ”‚       โ”œโ”€โ”€ ray_cast_dda_algo_bonus.c
โ”‚       โ”œโ”€โ”€ ray_cast_utils_bonus.c
โ”‚       โ”œโ”€โ”€ rendring_bonus.h
โ”‚       โ”œโ”€โ”€ start_game_bonus.c
โ”‚       โ”œโ”€โ”€ texturing_bonus
โ”‚       โ”‚   โ”œโ”€โ”€ texture_draw_bonus.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_loading_bonus.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_rendering_bonus.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_utils_bonus.c
โ”‚       โ”‚   โ””โ”€โ”€ texturing_bonus.h
โ”‚       โ”œโ”€โ”€ utils_bonus.c
โ”‚       โ””โ”€โ”€ wand_animation_bonus.c
โ”œโ”€โ”€ file.md
โ”œโ”€โ”€ images
โ”‚   โ”œโ”€โ”€ Gryffindor.png
โ”‚   โ”œโ”€โ”€ Hufflepuff.png
โ”‚   โ”œโ”€โ”€ Ravenclaw.png
โ”‚   โ”œโ”€โ”€ Slytherin.png
โ”‚   โ””โ”€โ”€ cub3d.png
โ”œโ”€โ”€ mandatory
โ”‚   โ”œโ”€โ”€ cub3d.h
โ”‚   โ”œโ”€โ”€ main.c
โ”‚   โ”œโ”€โ”€ parsing
โ”‚   โ”‚   โ”œโ”€โ”€ parse_color.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_fill_map.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_line.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_map.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_memory.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_player.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_small_map.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_textures.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_utils.c
โ”‚   โ”‚   โ”œโ”€โ”€ parse_validate_map.c
โ”‚   โ”‚   โ””โ”€โ”€ parsing_cub3d.h
โ”‚   โ””โ”€โ”€ rendring
โ”‚       โ”œโ”€โ”€ exit_game.c
โ”‚       โ”œโ”€โ”€ game_loop.c
โ”‚       โ”œโ”€โ”€ ray_cast.c
โ”‚       โ”œโ”€โ”€ ray_cast_dda_algo.c
โ”‚       โ”œโ”€โ”€ ray_cast_utils.c
โ”‚       โ”œโ”€โ”€ rendring_cub3d.h
โ”‚       โ”œโ”€โ”€ start_game.c
โ”‚       โ”œโ”€โ”€ texturing
โ”‚       โ”‚   โ”œโ”€โ”€ texture_draw.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_loading.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_rendering.c
โ”‚       โ”‚   โ”œโ”€โ”€ texture_utils.c
โ”‚       โ”‚   โ””โ”€โ”€ texturing_cub3d.h
โ”‚       โ””โ”€โ”€ utils.c
โ”œโ”€โ”€ maps
โ”‚   โ”œโ”€โ”€ Gryffindor_map.cub
โ”‚   โ”œโ”€โ”€ Hufflepuff_map.cub
โ”‚   โ”œโ”€โ”€ Ravenclaw_map.cub
โ”‚   โ”œโ”€โ”€ Slytherin_map.cub
โ”‚   โ”œโ”€โ”€ empty_map.cub
โ”‚   โ”œโ”€โ”€ mapSubject.cub
โ”‚   โ””โ”€โ”€ map_subject.cub
โ”œโ”€โ”€ minilibx_opengl
โ”‚   โ”œโ”€โ”€ Makefile
โ”‚   โ”œโ”€โ”€ font.c
โ”‚   โ”œโ”€โ”€ font.xcf
โ”‚   โ”œโ”€โ”€ mlx.h
โ”‚   โ”œโ”€โ”€ mlx_init_loop.m
โ”‚   โ”œโ”€โ”€ mlx_int.h
โ”‚   โ”œโ”€โ”€ mlx_int_str_to_wordtab.c
โ”‚   โ”œโ”€โ”€ mlx_mouse.m
โ”‚   โ”œโ”€โ”€ mlx_new_image.m
โ”‚   โ”œโ”€โ”€ mlx_new_window.h
โ”‚   โ”œโ”€โ”€ mlx_new_window.m
โ”‚   โ”œโ”€โ”€ mlx_opengl.h
โ”‚   โ”œโ”€โ”€ mlx_opengl.m
โ”‚   โ”œโ”€โ”€ mlx_png.c
โ”‚   โ”œโ”€โ”€ mlx_png.h
โ”‚   โ”œโ”€โ”€ mlx_rgb.c
โ”‚   โ”œโ”€โ”€ mlx_shaders.c
โ”‚   โ””โ”€โ”€ mlx_xpm.c
โ”œโ”€โ”€ structures_headers.tldr
โ””โ”€โ”€ textures
    โ”œโ”€โ”€ DOOR.xpm
    โ”œโ”€โ”€ wall_Gry.xpm
    โ”œโ”€โ”€ wall_Huf.xpm
    โ”œโ”€โ”€ wall_Rev.xpm
    โ”œโ”€โ”€ wall_Sly.xpm
    โ”œโ”€โ”€ wall_simple.xpm
    โ”œโ”€โ”€ wall_void.xpm
    โ””โ”€โ”€ wand_magic
        โ”œโ”€โ”€ wand_magic_1.xpm
        โ”œโ”€โ”€ wand_magic_2.xpm
        โ”œโ”€โ”€ wand_magic_3.xpm
        โ”œโ”€โ”€ wand_magic_4.xpm
        โ”œโ”€โ”€ wand_magic_5.xpm
        โ””โ”€โ”€ wand_magic_6.xpm

15 directories, 146 files

๐Ÿ‘ฅ Authors

  • msabr
  • aylamiri
	โ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—   โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—
	โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•”โ•โ•โ•โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•  โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•— โ•šโ•โ•โ–ˆโ–ˆโ•”โ•โ•โ• โ–ˆโ–ˆโ•”โ•โ•โ•โ•โ•
	โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•‘ โ–ˆโ•— โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•    โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—
	โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘   โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘โ–ˆโ–ˆโ–ˆโ•—โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—    โ–ˆโ–ˆโ•‘    โ•šโ•โ•โ•โ•โ–ˆโ–ˆโ•‘
	โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘ โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ• โ•šโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ• โ•šโ–ˆโ–ˆโ–ˆโ•”โ–ˆโ–ˆโ–ˆโ•”โ• โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘ โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ•‘    โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•‘
	โ•šโ•โ•  โ•šโ•โ•  โ•šโ•โ•โ•โ•โ•โ•   โ•šโ•โ•โ•โ•โ•โ•   โ•šโ•โ•โ•โ•šโ•โ•โ•  โ•šโ•โ•  โ•šโ•โ• โ•šโ•โ•  โ•šโ•โ•    โ•šโ•โ•    โ•šโ•โ•โ•โ•โ•โ•โ•
	                             โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•—  โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•— 
	                             โ•šโ•โ•โ•โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•”โ•โ•โ–ˆโ–ˆโ•—
	                             โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ• โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘
	                             โ•šโ•โ•โ•โ–ˆโ–ˆโ•— โ–ˆโ–ˆโ•‘  โ–ˆโ–ˆโ•‘
	                             โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ• โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ•”โ•
	                             โ•šโ•โ•โ•โ•โ•  โ•šโ•โ•โ•โ•โ•โ•
	                   ๐Ÿฐ Welcome to  Hogwarts  3D Game! ๐Ÿฐ$
	                        โœจAfter All This Time โœจ

About

CUB3D is a 42 Network project that explores the fundamentals of raycasting, a rendering technique used in early 3D games. Using the MiniLibX library, this project creates a first-person perspective of a maze where the player can navigate through corridors with textured walls.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •