feat(player): reimplemented angles

This commit is contained in:
Theo Champion 2025-07-29 19:50:23 +02:00
parent e46f199606
commit b6eeea5e9f
3 changed files with 42 additions and 5 deletions

View file

@ -6,11 +6,43 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/24 14:41:35 by tchampio #+# #+# */ /* Created: 2025/07/24 14:41:35 by tchampio #+# #+# */
/* Updated: 2025/07/29 12:21:27 by tchampio ### ########.fr */ /* Updated: 2025/07/29 19:47:18 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/X.h> #include <X11/X.h>
#include "../cub3d_data.h"
#include "../consts.h"
#include <math.h>
#include "player.h"
void turn_left_angle(t_cub3d_data *data)
{
t_player *p;
double prev_dir_x;
double prev_plane_x;
p = &data->player;
prev_dir_x = p->dir_x;
prev_plane_x = p->plane_x;
p->dir_x = p->dir_x * cos(ROTATION_SPEED) - p->dir_y * sin(ROTATION_SPEED);
p->dir_y = prev_dir_x * sin(ROTATION_SPEED) + p->dir_y * cos(ROTATION_SPEED);
p->plane_x = p->plane_x * cos(ROTATION_SPEED) - p->plane_y * sin(ROTATION_SPEED);
p->plane_y = prev_plane_x * sin(ROTATION_SPEED) + p->plane_y * cos(ROTATION_SPEED);
}
void turn_right_angle(t_cub3d_data *data)
{
t_player *p;
double prev_dir_x;
double prev_plane_x;
p = &data->player;
prev_dir_x = p->dir_x;
prev_plane_x = p->plane_x;
p->dir_x = p->dir_x * cos(-ROTATION_SPEED) - p->dir_y * sin(-ROTATION_SPEED);
p->dir_y = prev_dir_x * sin(-ROTATION_SPEED) + p->dir_y * cos(-ROTATION_SPEED);
p->plane_x = p->plane_x * cos(-ROTATION_SPEED) - p->plane_y * sin(-ROTATION_SPEED);
p->plane_y = prev_plane_x * sin(-ROTATION_SPEED) + p->plane_y * cos(-ROTATION_SPEED);
}

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/24 14:46:11 by tchampio #+# #+# */ /* Created: 2025/07/24 14:46:11 by tchampio #+# #+# */
/* Updated: 2025/07/24 14:48:52 by tchampio ### ########.fr */ /* Updated: 2025/07/29 19:34:34 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@
# include "../cub3d_data.h" # include "../cub3d_data.h"
# include "../consts.h" # include "../consts.h"
void apply_angle(int key, t_cub3d_data *data); void turn_left_angle(t_cub3d_data *data);
void turn_right_angle(t_cub3d_data *data);
#endif // ANGLE_H #endif // ANGLE_H

View file

@ -6,13 +6,13 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */ /* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:58:00 by tchampio #+# #+# */ /* Created: 2025/07/29 13:58:00 by tchampio #+# #+# */
/* Updated: 2025/07/29 14:52:54 by tchampio ### ########.fr */ /* Updated: 2025/07/29 19:47:38 by tchampio ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../cub3d_data.h" #include "../cub3d_data.h"
#include "angle.h"
#include "../consts.h" #include "../consts.h"
#include <stdio.h> // bad
void move_player_forward(t_cub3d_data *data) void move_player_forward(t_cub3d_data *data)
{ {
@ -84,4 +84,8 @@ void move_player(t_cub3d_data *data)
move_player_strafe_left(data); move_player_strafe_left(data);
if (data->keypresses.is_d_pressed) if (data->keypresses.is_d_pressed)
move_player_strafe_right(data); move_player_strafe_right(data);
if (data->keypresses.is_left_pressed)
turn_left_angle(data);
if (data->keypresses.is_right_pressed)
turn_right_angle(data);
} }