Cub3D
42 Abu Dhabi

Cub3D

3D Raycasting Game Engine

6 weeks
Team Project
CRaycastingMiniLibX3D Graphics

A 3D raycasting game engine inspired by Wolfenstein 3D, implementing graphics rendering, player movement, and collision detection in a pseudo-3D environment.

Key Features

Raycasting Engine

Implemented raycasting algorithm to create 3D perspective from 2D map data with wall rendering and texture mapping.

Player Movement

Smooth player movement and rotation with collision detection and boundary checking in the 3D environment.

Texture Mapping

Applied textures to walls with proper scaling and orientation based on viewing angle and distance.

Map Parsing

Dynamic map loading with configurable textures, colors, and spawn positions from scene description files.

Development Journey

Phase 1

Map Parsing & Validation

Implemented scene file parser to read map configurations, textures, and validate input format. Created data structures to store map information and player spawn position.

Phase 2

Graphics Initialization

Set up MiniLibX graphics library, window management, and event handling. Implemented basic drawing functions and coordinate system for 3D rendering.

Phase 3

Raycasting Implementation

Developed core raycasting algorithm with DDA (Digital Differential Analyzer) for wall detection. Implemented distance calculations and wall height rendering.

Phase 4

Player Controls & Optimization

Added player movement, rotation, and collision detection. Optimized rendering performance and added texture mapping for realistic wall appearances.

Challenges & Solutions

3D Mathematics

Problem:

Understanding and implementing raycasting algorithms and 3D coordinate transformations for realistic rendering.

Solution:

Studied raycasting theory and implemented step-by-step ray intersection calculations with proper trigonometry.

Performance Optimization

Problem:

Achieving smooth frame rates while rendering complex 3D scenes in real-time without graphics acceleration.

Solution:

Optimized rendering loops and implemented efficient distance calculations with minimal floating-point operations.

Texture Management

Problem:

Properly mapping textures to walls with correct scaling and orientation based on viewing angle.

Solution:

Implemented texture coordinate calculation system with proper UV mapping for wall surfaces.

raycasting.c
c
void	perform_dda(t_game *game, t_ray *ray)
{
    int	hit = 0;
    
    while (hit == 0)
    {
        if (ray->side_dist_x < ray->side_dist_y)
        {
            ray->side_dist_x += ray->delta_dist_x;
            ray->map_x += ray->step_x;
            ray->side = 0;
        }
        else
        {
            ray->side_dist_y += ray->delta_dist_y;
            ray->map_y += ray->step_y;
            ray->side = 1;
        }
        if (game->map[ray->map_x][ray->map_y] == '1')
            hit = 1;
    }
}

void	calculate_wall_distance(t_game *game, t_ray *ray)
{
    if (ray->side == 0)
        ray->perp_wall_dist = (ray->map_x - game->player.x + 
                              (1 - ray->step_x) / 2) / ray->ray_dir_x;
    else
        ray->perp_wall_dist = (ray->map_y - game->player.y + 
                              (1 - ray->step_y) / 2) / ray->ray_dir_y;
}