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
datapointer is valid. - The new potential position is calculated as:
new_x = player.x + player.dir_y * MOVE_SPEEDnew_y = player.y - player.dir_x * MOVE_SPEED
- Each axis (
xandy) 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