Chapter 05: First-Person Controls & Level Geometry

Lock the mouse cursor for true FPS mouselook, add gravity and jumping, resolve smooth sliding wall collisions, and walk through a multi-room textured dungeon.

Chapter:05    Duration: 1 hr 30
Files:src/world.h, src/world.c, src/main.c, Makefile
Concepts:Mouselook pitch clamp, normalized diagonal movement, axis-separated AABB sliding collision, hidden-face culling on grid cells
On this page:FPS Movement & Mouselook · Sliding Wall Collision · World Module · Connecting main.c · Exercises · Recap

1. Mouselook & Normalized Strafing

In main.c, call plat_set_mouse_grab(1) right after plat_init so SDL hides the pointer and streams relative mouse deltas into input.mouse_dx and input.mouse_dy. Two classic FPS details live in player_update:

2. Axis-Separated Sliding Collision

Instead of testing the player’s new (nx, nz) position in one shot—which makes you stick like glue whenever you brush a wall at an angle—we test X first, then Z independently. If X hits a wall, Z still advances, giving you smooth Quake-style wall sliding in just two lines of code.

3. Writing src/world.h & src/world.c

world_render walks the 12x12 map grid and emits floor, ceiling, and only those wall quads facing open space (hidden interior faces between two solid blocks are never sent to the rasterizer). Every quad splits cleanly into two outward-wound triangles passed to rast_tri_tex.

src/world.h (c)
#ifndef     WORLD_H
#define     WORLD_H

#include    "types.h"
#include    "math3d.h"
#include    "raster.h"
#include    "tex.h"

#define     MAP_W       12
#define     MAP_H       12
#define     CELL_SIZE   2.0f
#define     WALL_H      2.4f

typedef struct {

    Vec3    pos;
    Vec3    vel;
    float   yaw;
    float   pitch;
    int     on_ground;

} Player;

void    player_init(Player *p, float x, float z);
void    player_update(Player *p, const InputState *in, float dt);
int     world_is_solid(int gx, int gz);
void    world_render(Framebuffer *fb,
                     ZBuffer *zb,
                     const Texture *wall_tex,
                     const Texture *floor_tex,
                     Mat4 view,
                     Mat4 proj,
                     u32 fog_col);

#endif
src/world.c (c)
#include    "world.h"
#include    <math.h>

static const u8 g_map[MAP_H][MAP_W] = {

    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1 },
    { 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1 },
    { 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1 },
    { 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
    { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
    { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }

};

static Vertex make_vtx(float x, float y, float z, float u, float v) {

    Vertex  r;

    r.pos = v3_make(x, y, z);
    r.u   = u;
    r.v   = v;

    return r;

}

static void draw_quad(Framebuffer *fb,
                      ZBuffer *zb,
                      const Texture *tex,
                      Mat4 view,
                      Mat4 proj,
                      Vertex a,
                      Vertex b,
                      Vertex c,
                      Vertex d,
                      u32 fog_col) {

    Mat4    ident;

    ident = m4_identity();

    rast_tri_tex(fb, zb, tex, ident, view, proj, a, b, c, fog_col);
    rast_tri_tex(fb, zb, tex, ident, view, proj, a, c, d, fog_col);

}

static int point_collides(float x, float z, float rad) {

    int     min_x;
    int     max_x;
    int     min_z;
    int     max_z;
    int     gx;
    int     gz;

    min_x = (int)floor((double)((x - rad) / CELL_SIZE));
    max_x = (int)floor((double)((x + rad) / CELL_SIZE));
    min_z = (int)floor((double)((z - rad) / CELL_SIZE));
    max_z = (int)floor((double)((z + rad) / CELL_SIZE));

    for (gz = min_z; gz <= max_z; ++gz) {

        for (gx = min_x; gx <= max_x; ++gx) {

            if (world_is_solid(gx, gz)) { return 1; }

        }

    }

    return 0;

}

int world_is_solid(int gx, int gz) {

    if (gx < 0 || gx >= MAP_W || gz < 0 || gz >= MAP_H) { return 1; }

    return g_map[gz][gx] != 0;

}

void player_init(Player *p, float x, float z) {

    p -> pos       = v3_make(x, 1.15f, z);
    p -> vel       = v3_make(0.0f, 0.0f, 0.0f);
    p -> yaw       = 0.0f;
    p -> pitch     = 0.0f;
    p -> on_ground = 1;

}

void player_update(Player *p, const InputState *in, float dt) {

    float   sens;
    float   max_pitch;
    float   speed;
    float   sin_y;
    float   cos_y;
    float   mx;
    float   mz;
    float   nx;
    float   nz;
    float   rad;

    sens      = 0.0032f;
    max_pitch = DEG2RAD(82.0f);

    p -> yaw   += (float)in -> mouse_dx * sens;
    p -> pitch -= (float)in -> mouse_dy * sens;

    if (in -> keys[KEY_LEFT])  { p -> yaw -= 2.2f * dt; }
    if (in -> keys[KEY_RIGHT]) { p -> yaw += 2.2f * dt; }

    if (p -> pitch >  max_pitch) { p -> pitch =  max_pitch; }
    if (p -> pitch < -max_pitch) { p -> pitch = -max_pitch; }

    speed = in -> keys[KEY_SHIFT] ? 7.0f : 4.5f;
    sin_y = (float)sin((double)p -> yaw);
    cos_y = (float)cos((double)p -> yaw);
    mx    = 0.0f;
    mz    = 0.0f;

    if (in -> keys[KEY_W]) { mx += sin_y; mz += cos_y; }
    if (in -> keys[KEY_S]) { mx -= sin_y; mz -= cos_y; }
    if (in -> keys[KEY_D]) { mx += cos_y; mz -= sin_y; }
    if (in -> keys[KEY_A]) { mx -= cos_y; mz += sin_y; }

    if (mx != 0.0f || mz != 0.0f) {

        float   inv = 1.0f / (float)sqrt((double)((mx * mx) + (mz * mz)));

        mx *= inv * speed * dt;
        mz *= inv * speed * dt;

    }

    rad = 0.35f;
    nx  = p -> pos.x + mx;
    nz  = p -> pos.z + mz;

    if (!point_collides(nx, p -> pos.z, rad)) { p -> pos.x = nx; }
    if (!point_collides(p -> pos.x, nz, rad)) { p -> pos.z = nz; }

    if (in -> keys[KEY_SPACE] && p -> on_ground) {

        p -> vel.y     = 4.6f;
        p -> on_ground = 0;

    }

    p -> vel.y -= 13.5f * dt;
    p -> pos.y += p -> vel.y * dt;

    if (p -> pos.y <= 1.15f) {

        p -> pos.y     = 1.15f;
        p -> vel.y     = 0.0f;
        p -> on_ground = 1;

    }

}

void world_render(Framebuffer *fb,
                  ZBuffer *zb,
                  const Texture *wall_tex,
                  const Texture *floor_tex,
                  Mat4 view,
                  Mat4 proj,
                  u32 fog_col) {

    int     gz;
    int     gx;

    for (gz = 0; gz < MAP_H; ++gz) {

        for (gx = 0; gx < MAP_W; ++gx) {

            float   x0;
            float   x1;
            float   z0;
            float   z1;

            if (world_is_solid(gx, gz)) { continue; }

            x0 = (float)gx * CELL_SIZE;
            x1 = x0 + CELL_SIZE;
            z0 = (float)gz * CELL_SIZE;
            z1 = z0 + CELL_SIZE;

            draw_quad(fb, zb, floor_tex, view, proj,
                      make_vtx(x0, 0.0f, z0, 0.0f, 0.0f),
                      make_vtx(x0, 0.0f, z1, 0.0f, 1.0f),
                      make_vtx(x1, 0.0f, z1, 1.0f, 1.0f),
                      make_vtx(x1, 0.0f, z0, 1.0f, 0.0f),
                      fog_col);

            draw_quad(fb, zb, floor_tex, view, proj,
                      make_vtx(x0, WALL_H, z0, 0.0f, 0.0f),
                      make_vtx(x1, WALL_H, z0, 1.0f, 0.0f),
                      make_vtx(x1, WALL_H, z1, 1.0f, 1.0f),
                      make_vtx(x0, WALL_H, z1, 0.0f, 1.0f),
                      fog_col);

            if (world_is_solid(gx, gz - 1)) {

                draw_quad(fb, zb, wall_tex, view, proj,
                          make_vtx(x0, WALL_H, z0, 0.0f, 0.0f),
                          make_vtx(x0, 0.0f,   z0, 0.0f, 1.0f),
                          make_vtx(x1, 0.0f,   z0, 1.0f, 1.0f),
                          make_vtx(x1, WALL_H, z0, 1.0f, 0.0f),
                          fog_col);

            }

            if (world_is_solid(gx, gz + 1)) {

                draw_quad(fb, zb, wall_tex, view, proj,
                          make_vtx(x1, WALL_H, z1, 0.0f, 0.0f),
                          make_vtx(x1, 0.0f,   z1, 0.0f, 1.0f),
                          make_vtx(x0, 0.0f,   z1, 1.0f, 1.0f),
                          make_vtx(x0, WALL_H, z1, 1.0f, 0.0f),
                          fog_col);

            }

            if (world_is_solid(gx - 1, gz)) {

                draw_quad(fb, zb, wall_tex, view, proj,
                          make_vtx(x0, WALL_H, z1, 0.0f, 0.0f),
                          make_vtx(x0, 0.0f,   z1, 0.0f, 1.0f),
                          make_vtx(x0, 0.0f,   z0, 1.0f, 1.0f),
                          make_vtx(x0, WALL_H, z0, 1.0f, 0.0f),
                          fog_col);

            }

            if (world_is_solid(gx + 1, gz)) {

                draw_quad(fb, zb, wall_tex, view, proj,
                          make_vtx(x1, WALL_H, z0, 0.0f, 0.0f),
                          make_vtx(x1, 0.0f,   z0, 0.0f, 1.0f),
                          make_vtx(x1, 0.0f,   z1, 1.0f, 1.0f),
                          make_vtx(x1, WALL_H, z1, 1.0f, 0.0f),
                          fog_col);

            }

        }

    }

}

4. Connecting Everything in main.c

Add src/world.c to SRCS in Makefile. In src/main.c:

  1. Call plat_set_mouse_grab(1) after plat_init.
  2. Build a 64x64 brick wall texture (tex_make_brick) and dark stone floor texture (tex_make_checker), and initialize Player player at open cell (3.0f, 3.0f) with player_init(&player, 3.0f, 3.0f).
  3. Each frame: call player_update(&player, &input, dt), build view = m4_view_fps(player.pos, player.yaw, player.pitch), clear both framebuffer and Z-buffer, call world_render(...), and draw a small 5-pixel crosshair at (fb.width / 2, fb.height / 2) using fb_line.
Build & Explore
Run make run. Look around with the mouse, sprint with Shift, jump with Space, and brush against brick corners to feel the axis-separated collision slide cleanly along the walls.

Exercises

Recap