cub3D
Just pixels, and a lot of trigonometry
A first-person renderer written in C with no 3D library at all: for every column of the screen, a ray crosses a grid until it meets a wall, and that distance gives the height to draw. The scene is a text file, checked closed before any window opens. Not a triangle anywhere.
- Area
- 42
- game
- ray-casting
- Stack
- C
- Grade
- 110%
- Date
- 2023
- Links
Redrawing Wolfenstein
Wolfenstein 3D, in 1992, drew a maze in first person on a machine that could not compute a three-dimensional scene at all. There was none: every column of the screen was a plain rectangle, whose height came from a distance measured on a flat plan. Rebuilding that engine in C is fifteen hundred lines and a great deal of trigonometry.
The scene file
A .cub file holds four texture paths, one per cardinal direction, a floor and a ceiling colour, then the map: 0 for empty space, 1 for a wall, and one of N S E W for the player’s starting position and heading. No window opens until the map has been validated: exactly one spawn point, and above all a sealed maze, every walkable cell surrounded by walls or other walkable cells. The rest of the program can then take for granted that the player will never walk out of the world.

One ray per column
The principle fits in a sentence: one ray per column of pixels. Sixty degrees of field of view spread across the width of the window, one ray each, and for each the distance to the first wall it meets.
That distance becomes a height. A wall at distance d is drawn with a height proportional to 1 / d, scaled by the distance to the projection plane, which itself follows from the field of view. The column is centred on the horizon line, and that is all: there is no three-dimensional geometry anywhere in the program.

Why not a fixed step
The wall still has to be found. The naive approach advances the ray by a constant step, testing at each stop whether it is inside a wall. It has a flaw no tuning fixes: too wide a step walks over a thin wall and the ray flies off to infinity, while a step fine enough to miss nothing multiplies the tests tenfold and still guarantees nothing.

The DDA algorithm turns the problem around. In a grid, a wall always begins on a grid line, so there is no reason to look anywhere else. The ray jumps from line to line, and the length of each jump follows from its angle, computed once, instead of being picked in advance. Two families of intersections are tracked in parallel, one for the horizontal grid lines and one for the vertical, and the nearer of the two wins. No wall can slip between the tests, and their number falls to the strict minimum.
Knowing which family won does a second job, too. A horizontal intersection means the top or bottom of a cell was hit, a vertical one means a side; the sign of the direction then decides between north and south, or between east and west. The texture to map comes out of that single test.
The full calculation, the nine steps that take a ray from its starting angle to the drawn column, is written up in the repository README, under The Nine-Step Raycasting Process.

From distance to image
Measuring the distance to the wall along the ray gives a bulged picture: straight walls curve at the edges of the screen. The ray at the edge is simply longer than the one at the centre for the same wall seen head-on. Multiplying by the cosine of the angle between the ray and the direction of view brings the measurement back to the perpendicular, and the walls straighten out.
The exact point of impact gives the texture column to read, and the vertical position within the drawn column gives the row. Nothing reaches the screen meanwhile: it is all written into a buffer, pixel by pixel, straight into memory. One image goes to the window, once the frame is complete.
The bonuses
A minimap overlay at a fifth of the scale, showing the maze, the player’s position and one ray in thirty-two, to make visible what the engine is computing. The M key shows or hides it.
Then collisions. Testing only the target cell would pin the player to the wall the moment they approach it at an angle. The two components of the movement are therefore tested separately, alongside their sum: if one passes and the other does not, the player slides along the wall rather than stopping dead.
