fix: created angle.c and moved part of the movement to comply with norme

This commit is contained in:
Theo Champion 2025-07-24 14:49:41 +02:00
parent bec6b27879
commit 37e143d32d
3 changed files with 73 additions and 26 deletions

47
src/player/angle.c Normal file
View file

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* angle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/24 14:41:35 by tchampio #+# #+# */
/* Updated: 2025/07/24 14:48:04 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include <math.h>
#include <X11/keysym.h>
#include <X11/X.h>
/* key parameter should be X's value such as XK_a */
void apply_angle(int key, t_cub3d_data *data)
{
double cos_angle;
double sin_angle;
cos_angle = cos(data->player.yaw);
sin_angle = sin(data->player.yaw);
if (key == XK_a)
{
data->player.x += cos_angle * MOVEMENT_SPEED;
data->player.y -= sin_angle * MOVEMENT_SPEED;
}
if (key == XK_d)
{
data->player.x -= cos_angle * MOVEMENT_SPEED;
data->player.y += sin_angle * MOVEMENT_SPEED;
}
if (key == XK_w)
{
data->player.x += sin_angle * MOVEMENT_SPEED;
data->player.y += cos_angle * MOVEMENT_SPEED;
}
if (key == XK_s)
{
data->player.x -= sin_angle * MOVEMENT_SPEED;
data->player.y -= cos_angle * MOVEMENT_SPEED;
}
}

21
src/player/angle.h Normal file
View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* angle.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/24 14:46:11 by tchampio #+# #+# */
/* Updated: 2025/07/24 14:48:52 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef ANGLE_H
# define ANGLE_H
# include "../cub3d_data.h"
# include "../consts.h"
void apply_angle(int key, t_cub3d_data *data);
#endif // ANGLE_H

View file

@ -6,11 +6,12 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */
/* Updated: 2025/07/24 14:20:15 by tchampio ### ########.fr */
/* Updated: 2025/07/24 14:47:14 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../consts.h"
#include "../player/angle.h"
#include "frees.h"
#include <X11/keysym.h>
#include <X11/X.h>
@ -19,11 +20,6 @@
int keypress_handler(int keycode, t_cub3d_data *data)
{
double cos_angle;
double sin_angle;
cos_angle = cos(data->player.yaw);
sin_angle = sin(data->player.yaw);
if (keycode == XK_Escape)
destroy(data);
if (keycode == XK_Left)
@ -34,26 +30,9 @@ int keypress_handler(int keycode, t_cub3d_data *data)
data->player.yaw -= 2 * M_PI;
if (data->player.yaw < 0)
data->player.yaw += 2 * M_PI;
if (keycode == XK_a)
{
data->player.x += cos_angle * MOVEMENT_SPEED;
data->player.y -= sin_angle * MOVEMENT_SPEED;
}
if (keycode == XK_d)
{
data->player.x -= cos_angle * MOVEMENT_SPEED;
data->player.y += sin_angle * MOVEMENT_SPEED;
}
if (keycode == XK_w)
{
data->player.x += sin_angle * MOVEMENT_SPEED;
data->player.y += cos_angle * MOVEMENT_SPEED;
}
if (keycode == XK_s)
{
data->player.x -= sin_angle * MOVEMENT_SPEED;
data->player.y -= cos_angle * MOVEMENT_SPEED;
}
if (keycode == XK_a || keycode == XK_d
|| keycode == XK_w || keycode == XK_s)
apply_angle(keycode, data);
return (0);
}