GitHub - yeuci/raycaster: raycast engine in c
raycast engine in c. Contribute to yeuci/raycaster development by creating an account on GitHub.
raycasting
rendering pseudo-3d gameplay with simple raycasts

a wolfenstein-style raycast engine in c with sdl2. the world is represented as a 2d grid. you cast one ray per screen column and draw a vertical strip whose height depends on how far the ray travelled. everything is drawn into a pixel buffer and blitted to the window once per frame.

textured walls, a couple of sprites, and the minimap in the corner showing every ray.
textured walls, a couple of sprites, and the minimap in the corner showing every ray.

the map

the map is an array of ints. zero is empty floor, anything else is a wall. each cell is $64$ units across, so the player's position is in world units and the cell it's in is just $\lfloor x / 64 \rfloor$. the player keeps a position, an angle, a walk speed and a turn speed, and movement is

$$x' = x + \cos\theta \cdot v \cdot \Delta t, \qquad y' = y + \sin\theta \cdot v \cdot \Delta t$$

with a wall check on the new position before it's accepted.

casting rays

there is one ray per column so at $1280$ pixels wide that's $1280$ rays per frame. the field of view is $60^\circ$, and the distance from the player to the projection plane falls out of that:

$$d = \frac{\text{width} / 2}{\tan(\text{fov} / 2)}$$

the rays are not spaced evenly in angle. instead each column's angle comes from where that column sits on the projection plane,

$$\theta_{col} = \theta_{player} + \arctan\frac{col - \text{width}/2}{d}$$

each ray is traced twice: once stepping along horizontal grid lines and once along vertical ones. for the horizontal pass you find the first horizontal line the ray crosses, then step one full tile in $y$ and $64 / \tan\theta$ in $x$ until you land in a wall cell. the vertical pass is the same with $x$ and $y$ swapped. whichever hit is closer wins, and the ray remembers whether it hit a vertical or horizontal face, because that decides both the texture coordinate and the shading.

projecting walls

the raw distance from the two-pass trace is along the ray, and using it directly gives a fisheye lens. multiplying by the cosine of the angle between the ray and the player's facing direction turns it into the perpendicular distance to the wall, and then the strip height is a similar-triangles ratio:

$$h = \frac{64}{\text{dist} \cdot \cos(\theta_{col} - \theta_{player})} \cdot d$$

float p_distance = rays[x].distance * cos(rays[x].rayAngle - player.rotationAngle);
float projected_wall_height = (TILE_SIZE / p_distance) * DIST_PROJ_PLANE;

int wall_top_pixel    = (WINDOW_HEIGHT / 2) - (wall_strip_height / 2);
int wall_bottom_pixel = (WINDOW_HEIGHT / 2) + (wall_strip_height / 2);

the strip is centered vertically and everything above it is painted ceiling and everything below is floor. for the texture, the column of the texture to sample is the hit point modulo the tile size, the $y$ of the hit for vertical faces and the $x$ for horizontal ones, and each pixel of the strip maps to a row of the texture by the ratio of texture height to strip height. textures are pngs decoded with upng into plain uint32_t buffers, so sampling is the same buffer[width * y + x] as the screen. walls hit on a vertical face get their colour scaled by $0.7$, which is enough fake lighting to make corners read.

sprites

sprites are billboards. for each one i take the angle from the player to the sprite, and if it's inside half the fov (plus a bit of slack so sprites don't pop at the screen edge) it's visible. visible sprites are sorted far to near and drawn in that order so the close ones paint over the far ones. the size uses the same projection formula as walls, the screen $x$ is $\tan(\text{angle}) \cdot d$ from the centre, and each column of the sprite is only drawn if the sprite is closer than the wall that column's ray hit, which is a per-column depth test against the ray distances. magenta is the transparency key.