Skip to content

Player Initialization

Player initialization converts the spawn symbol from the map (N/S/E/W) into a world position and a raycasting-ready camera basis. This state is required before the first rendered frame.

Camera basis setup

From spawn tile to raycasting vectors

The map stores only `N/S/E/W`. Startup converts that symbol into a centered world position, a direction vector, and a perpendicular camera plane.

01
find_player(app, lines, map_start)

Runs during validation setup and locates the single spawn tile.

// tile found at (col, row)
player.x = col + 0.5
player.y = row + 0.5
player.orientation = tile_char

`+0.5` centers the player in the cell and avoids wall-boundary ambiguity.

02
init_player_vectors(app)

Converts orientation into `dir` and `plane`, which must stay perpendicular.

North N
dir
(0, -1)
plane
(+FOV_FACTOR, 0)

looks upward in map rows

South S
dir
(0, +1)
plane
(-FOV_FACTOR, 0)

looks downward in map rows

West W
dir
(-1, 0)
plane
(0, -FOV_FACTOR)

looks toward lower x

East E
dir
(+1, 0)
plane
(0, +FOV_FACTOR)

looks toward higher x

03 camera basis visualization

North — dir(0, -1) plane(+FOV_FACTOR, 0)

render loop can start

`player.x/y` are centered, `dir ⟂ plane`, and camera vectors are non-zero.

The current startup sequence is:

  1. parse_file(...)
  2. validation locates the single spawn tile
  3. init_player_vectors(app)
  4. init_mlx(app)
  5. frame loop starts

Validation guarantees that there is exactly one valid spawn before vector setup runs.

The spawn is found during validation setup with find_player(...).

When the spawn tile is found at (col, row), the player position is centered:

player.x = col + 0.5;
player.y = row + 0.5;
player.orientation = tile_char;

The +0.5 offset matters because movement and raycasting operate in continuous world coordinates, not tile indexes. Starting in the center avoids immediate boundary ambiguity.

init_player_vectors(app) converts orientation into:

  • direction vector: dir_x, dir_y
  • camera plane vector: plane_x, plane_y

The direction vector points where the player looks. The camera plane is perpendicular to it and controls the horizontal field of view used by raycasting.

SpawnDirectionCamera plane
N(0, -1)(+FOV_FACTOR, 0)
S(0, +1)(-FOV_FACTOR, 0)
W(-1, 0)(0, -FOV_FACTOR)
E(+1, 0)(0, +FOV_FACTOR)

After player initialization:

  • player position is centered in the spawn tile
  • orientation is one of N/S/E/W
  • direction vector is non-zero
  • camera plane is perpendicular to direction
  • rendering can safely cast rays from the player position
  • srcs/validation/validate_bfs.c (find_player)
  • srcs/validation/validate_map_player.c
  • srcs/core/init_app.c (init_player_vectors)
  • srcs/core/main.c