All 42 projects

42 Project brief

cub3D

A first-person 3D maze in C, in the spirit of Wolfenstein 3D, built with a teammate. I wrote the raycasting engine: one ray per screen column, wall hits found on the grid, and textured walls drawn pixel by pixel.

My role
Raycasting & rendering
Team
2 developers
Validated
Oct 2022
Result
Passed with bonus · 280 h
The bonus build running, with the minimap in the top left. Recorded on Linux with smaller movement steps for a smooth capture.

The project

cub3D is the 42 graphics project: render a maze in first person from a 2D map file, in C, with nothing but the MiniLibX graphics library. There is no 3D engine and no GPU shader; every frame is computed on the CPU.

The program reads a .cub file with the four wall textures, the floor and ceiling colors and the map, checks that the map is closed, then lets you walk around with the keyboard. The bonus adds a minimap.

The team’s stack

  • C
  • MiniLibX
  • Raycasting
  • Trigonometry
  • XPM textures
  • Make

My part

The 42 file headers show the split: I wrote the ray casting, texturing and frame rendering; my teammate wrote the map parser, the projection math and the minimap drawing.

01

Casting the rays

One ray for every column of the screen.

  • 1,080 rays per frame, spread across a 60° field of view around the player’s direction
  • Angles kept within one turn, and each ray’s direction (up or down, left or right) decides how it walks through the grid
02

Grid intersections

Finding the first wall a ray hits without testing every pixel.

  • Horizontal intersections: the first grid line the ray crosses, then jumps of one 64-unit tile at a time
  • Combined with the vertical intersections, the nearest hit gives the wall distance and the side that was hit
03

Textured walls

Four textures, chosen by which face of the wall the ray hits.

  • One 64×64 XPM texture per orientation, picked from the hit side and the ray’s direction
  • Texture coordinates from the exact hit point and the position inside the wall slice, so textures stretch correctly with distance
  • Ceiling and floor filled with the colors from the map file
04

Frames & input

The loop that turns key presses into frames.

  • The player’s position is updated with wall checks before every frame
  • Each frame is drawn pixel by pixel into an off-screen image, then shown in a single call to avoid flicker
  • The MiniLibX window and its keyboard hooks for moving, turning and closing

How a frame is drawn

  1. 1

    Input

    A key press moves or turns the player, unless a wall is in the way.

  2. 2

    Cast

    1,080 rays leave the player across a 60° field of view.

  3. 3

    Hit

    Each ray steps across the grid lines until it reaches a wall.

  4. 4

    Project

    The distance, corrected for the fisheye effect, sets the height of that column’s wall slice.

  5. 5

    Texture

    The slice is filled from the right texture, with the ceiling and floor colors above and below.

  6. 6

    Show

    The finished image is pushed to the window in one call.

Code

The code is in a private repository. I’m happy to walk through it in an interview.