223 words
1 minute
cub3D — 3D Raycasting Game Engine in C
cub3D — Raycasting 3D Graphics Engine
cub3D is a 1337 / 42 graphics project inspired by the Wolfenstein 3D engine. It renders a 3D perspective viewport from a 2D tile map in C using MiniLibX.
Raycasting Geometry & DDA Algorithm
The renderer casts rays across the camera plane for each screen column:
Ray Direction Vector ^ | / (Ray) | / [Camera Plane: Vector -1 to +1] | / |/ [Player Position: (px, py)]Digital Differential Analysis (DDA)
To compute wall intersections efficiently without floating-point increments, the engine uses the DDA algorithm:
- Calculates step directions (
stepX,stepY) and initial side distances. - Traverses the grid boundary by boundary until an obstacle is reached.
- Computes the perpendicular wall distance to eliminate fisheye distortion.
while (hit == 0){ if (side_dist_x < side_dist_y) { side_dist_x += delta_dist_x; map_x += step_x; side = 0; // Hit vertical grid line (East/West) } else { side_dist_y += delta_dist_y; map_y += step_y; side = 1; // Hit horizontal grid line (North/South) } if (world_map[map_x][map_y] > 0) hit = 1;}Texture Mapping and Frame Buffering
- Wall Texturing: Derives
wallX(exact hit coordinate on the wall), identifies texture columns (texX), and maps pixels from directional XPM textures. - Double Buffering: Writes pixel data directly into MiniLibX memory image buffers before blitting to the screen window at 60 FPS.
- Player Collision Physics: Implements bounding circle and axis-aligned collision detection to allow smooth wall sliding without clipping.
Sphynx | Ismail Larhrib