move_left - 1Fr3aK2/Cub3d GitHub Wiki

๐Ÿ“ move_left

Moves the player to the left relative to the direction they are currently facing, based on their directional vector and movement speed.

โš™๏ธ Parameters

Parameter Type Description
data t_data* Pointer to the main game data structure containing the player and map information.

๐Ÿ” Returns

Return value Description
void This function does not return a value.

๐Ÿ“– Description

The move_left function updates the player's position to move them sideways to the left relative to the direction they are facing. It calculates new potential coordinates based on the player's directional vector (dir_x, dir_y) and a constant movement speed (MOVE_SPEED).

Before updating the player's position, the function checks for collisions using the is_wall function to ensure that the new position does not intersect a wall.
If there is no wall in the intended direction of movement, the playerโ€™s x and y coordinates are updated accordingly.

  • The function first ensures that the data pointer is valid.
  • The new potential position is calculated as:
    • new_x = player.x + player.dir_y * MOVE_SPEED
    • new_y = player.y - player.dir_x * MOVE_SPEED
  • Each axis (x and y) is checked independently to allow smooth sliding along walls.

๐Ÿ’ก Example Usage

t_data data; // Game data structure containing the map and player info
// Assume data.player.dir_x and data.player.dir_y are set to the direction the player is facing

move_left(&data); // Moves the player to the left if no wall is blocking