From faf5127829d1053cd1a75f5a061fed78e7eb3344 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Tue, 29 Jul 2025 14:58:30 +0200 Subject: [PATCH 1/6] feat(player): redid all the movement code more details in desc - had to tamper at init with the map pointer to allow player to move - tweaked movement speed - allowed for multiple keys to be pressed at the same time - added collisions - had to modify minimap code because it segfaulted due to old code --- Makefile | 2 + src/consts.h | 4 +- src/cub3d_data.h | 4 +- src/draw/draw_map.c | 4 +- src/main.c | 42 +++++++++++---------- src/map/map_checker.c | 2 +- src/player/move.c | 87 +++++++++++++++++++++++++++++++++++++++++++ src/player/move.h | 20 ++++++++++ src/player/player.c | 3 +- src/utils/hooks.c | 3 +- src/utils/hooks.h | 12 +----- 11 files changed, 143 insertions(+), 40 deletions(-) create mode 100644 src/player/move.c create mode 100644 src/player/move.h diff --git a/Makefile b/Makefile index 726bec9..5280891 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,8 @@ SOURCEFILES = \ src/utils/hooks.c \ src/map/map_checker.c \ src/player/angle.c \ + src/player/player.c \ + src/player/move.c \ OBJECTS = $(SOURCEFILES:.c=.o) NAME = cub3d diff --git a/src/consts.h b/src/consts.h index 59fd961..d9b1792 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/07/29 13:29:11 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 14:28:45 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ extern const float g_southwest; extern const float g_southeast; # define RESSOURCE_DIR "ressources" -# define MOVEMENT_SPEED 6.4 +# define MOVEMENT_SPEED 0.1 # define ROTATION_SPEED 0.1 # define PLANE_VALUE 0.66 # ifdef BONUS diff --git a/src/cub3d_data.h b/src/cub3d_data.h index e1ad270..28c3859 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:26:32 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:43:40 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ # include "map/mapdata.h" # include "draw/img_data.h" # include "player/player.h" -#include "utils/hooks.h" +# include "utils/keypresses.h" typedef struct s_cub3d_data { diff --git a/src/draw/draw_map.c b/src/draw/draw_map.c index b174e80..ca0740f 100644 --- a/src/draw/draw_map.c +++ b/src/draw/draw_map.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:20:00 by kcolin #+# #+# */ -/* Updated: 2025/07/29 11:58:34 by kcolin ### ########.fr */ +/* Updated: 2025/07/29 14:16:07 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,5 +59,5 @@ void draw_map(t_mapdata *map, t_player *player, t_img_data *data) } i++; } - draw_2d_wall(0x00FF0000, data, MAP_SIZE * ((player->x - (float)SIZE / 2)) / SIZE, MAP_SIZE * ((player->y - (float)SIZE / 2) / SIZE)); + draw_2d_wall(0x00FF0000, data, MAP_SIZE * player->x, MAP_SIZE * player->y); } diff --git a/src/main.c b/src/main.c index 79eb323..77d1693 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/29 13:28:35 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 14:00:36 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include "consts.h" #include "cub3d_data.h" #include "draw/drawutils.h" +#include "player/move.h" #include "map/map_checker.h" #include "draw/draw_map.h" #include "map/mapdata.h" @@ -57,21 +58,21 @@ bool touch(float px, float py, t_mapdata *map) // } // } -t_cardinal_dir angle_cardinal_dir(float angle) -{ - if (angle >= g_southwest || (angle > g_south && angle < g_southeast)) - return (SOUTH); - if (angle >= g_southeast && angle < g_northeast) - return (EAST); - if (angle >= g_northeast && angle < g_northwest) - return (NORTH); - return (WEST); -} - -t_cardinal_dir player_cardinal_dir(t_player player) -{ - return (angle_cardinal_dir(player.yaw)); -} +// t_cardinal_dir angle_cardinal_dir(float angle) +// { +// if (angle >= g_southwest || (angle > g_south && angle < g_southeast)) +// return (SOUTH); +// if (angle >= g_southeast && angle < g_northeast) +// return (EAST); +// if (angle >= g_northeast && angle < g_northwest) +// return (NORTH); +// return (WEST); +// } +// +// t_cardinal_dir player_cardinal_dir(t_player player) +// { +// return (angle_cardinal_dir(player.yaw)); +// } t_intercardinal_dir angle_intercardinal_dir(float angle) { @@ -84,10 +85,10 @@ t_intercardinal_dir angle_intercardinal_dir(float angle) return (SOUTHWEST); } -t_intercardinal_dir player_intercardinal_dir(t_player player) -{ - return (angle_intercardinal_dir(player.yaw)); -} +// t_intercardinal_dir player_intercardinal_dir(t_player player) +// { +// return (angle_intercardinal_dir(player.yaw)); +// } /* ** Converts a coordinate from global map space to local tile space. @@ -104,6 +105,7 @@ int game_loop(t_cub3d_data *data) { mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + move_player(data); // if (player_intercardinal_dir(data->player) == SOUTHEAST) // ft_printf("southeast "); diff --git a/src/map/map_checker.c b/src/map/map_checker.c index 65205f5..e4abafd 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */ -/* Updated: 2025/07/23 13:33:05 by kcolin ### ########.fr */ +/* Updated: 2025/07/29 14:43:32 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/player/move.c b/src/player/move.c new file mode 100644 index 0000000..e1cadd6 --- /dev/null +++ b/src/player/move.c @@ -0,0 +1,87 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio // bad + +void move_player_forward(t_cub3d_data *data) +{ + char **map; + double next_x; + double next_y; + + map = data->map->map; + next_x = data->player.x + data->player.dir_x * MOVEMENT_SPEED; + next_y = data->player.y + data->player.dir_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x += data->player.dir_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y += data->player.dir_y * MOVEMENT_SPEED; +} + +void move_player_backward(t_cub3d_data *data) +{ + char **map; + double next_x; + double next_y; + + map = data->map->map; + next_x = data->player.x - data->player.dir_x * MOVEMENT_SPEED; + next_y = data->player.y - data->player.dir_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x -= data->player.dir_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y -= data->player.dir_y * MOVEMENT_SPEED; +} + +void move_player_strafe_left(t_cub3d_data *data) +{ + char **map; + double next_x; + double next_y; + + map = data->map->map; + next_x = data->player.x - data->player.plane_x * MOVEMENT_SPEED; + next_y = data->player.y - data->player.plane_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x -= data->player.plane_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y -= data->player.plane_y * MOVEMENT_SPEED; +} + +void move_player_strafe_right(t_cub3d_data *data) +{ + char **map; + double next_x; + double next_y; + + map = data->map->map; + next_x = data->player.x + data->player.plane_x * MOVEMENT_SPEED; + next_y = data->player.y + data->player.plane_y * MOVEMENT_SPEED; + if (map[(int)data->player.y][(int)next_x] == '0') + data->player.x += data->player.plane_x * MOVEMENT_SPEED; + if (map[(int)next_y][(int)data->player.x] == '0') + data->player.y += data->player.plane_y * MOVEMENT_SPEED; +} + +void move_player(t_cub3d_data *data) +{ + if (data->keypresses.is_w_pressed) + move_player_forward(data); + if (data->keypresses.is_s_pressed) + move_player_backward(data); + if (data->keypresses.is_a_pressed) + move_player_strafe_left(data); + if (data->keypresses.is_d_pressed) + move_player_strafe_right(data); +} diff --git a/src/player/move.h b/src/player/move.h new file mode 100644 index 0000000..14e9490 --- /dev/null +++ b/src/player/move.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* move.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio map[map->starty][map->startx]; player->x = map->startx + 0.5; player->y = map->starty + 0.5; + map->map[map->starty][map->startx] = '0'; player->health = 100; player->points = 500; if (dir == 'N' || dir == 'S') diff --git a/src/utils/hooks.c b/src/utils/hooks.c index dd940a2..9ec9356 100644 --- a/src/utils/hooks.c +++ b/src/utils/hooks.c @@ -6,11 +6,12 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:22:57 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:28:54 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:35:29 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "../consts.h" +#include "../cub3d_data.h" #include "../player/angle.h" #include "frees.h" #include diff --git a/src/utils/hooks.h b/src/utils/hooks.h index fee72ef..c6b78af 100644 --- a/src/utils/hooks.h +++ b/src/utils/hooks.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:23:06 by kcolin #+# #+# */ -/* Updated: 2025/07/29 12:26:04 by tchampio ### ########.fr */ +/* Updated: 2025/07/29 13:42:22 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,6 @@ # include "../cub3d_data.h" -typedef struct s_keypresses -{ - bool is_w_pressed; - bool is_a_pressed; - bool is_s_pressed; - bool is_d_pressed; - bool is_left_pressed; - bool is_right_pressed; -} t_keypresses; - int keypress_handler(int keycode, t_cub3d_data *data); int keyrelease_handler(int keycode, t_cub3d_data *data); From 2e1838e0bdaadee77064e1f93df7b39bc0470d84 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Tue, 29 Jul 2025 15:02:40 +0200 Subject: [PATCH 2/6] removal(player): removed old angle code as it's incompatible with new methods --- src/player/angle.c | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/src/player/angle.c b/src/player/angle.c index 847496b..a5d9837 100644 --- a/src/player/angle.c +++ b/src/player/angle.c @@ -6,42 +6,11 @@ /* By: tchampio #include #include -/* 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; - } -} From e46f199606607f932598dfd0d645e9c865d6846a Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Tue, 29 Jul 2025 15:03:10 +0200 Subject: [PATCH 3/6] fix: forgot to add header file --- src/utils/keypresses.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/utils/keypresses.h diff --git a/src/utils/keypresses.h b/src/utils/keypresses.h new file mode 100644 index 0000000..2f3da60 --- /dev/null +++ b/src/utils/keypresses.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* keypresses.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio Date: Tue, 29 Jul 2025 19:50:23 +0200 Subject: [PATCH 4/6] feat(player): reimplemented angles --- src/player/angle.c | 34 +++++++++++++++++++++++++++++++++- src/player/angle.h | 5 +++-- src/player/move.c | 8 ++++++-- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/player/angle.c b/src/player/angle.c index a5d9837..eeacb76 100644 --- a/src/player/angle.c +++ b/src/player/angle.c @@ -6,11 +6,43 @@ /* By: tchampio #include +#include "../cub3d_data.h" +#include "../consts.h" +#include +#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); +} diff --git a/src/player/angle.h b/src/player/angle.h index a87c59b..bf4807a 100644 --- a/src/player/angle.h +++ b/src/player/angle.h @@ -6,7 +6,7 @@ /* By: tchampio // bad void move_player_forward(t_cub3d_data *data) { @@ -84,4 +84,8 @@ void move_player(t_cub3d_data *data) move_player_strafe_left(data); if (data->keypresses.is_d_pressed) 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); } From 788f248c5e1a17d76f79b51a7030250cf32a12ee Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Tue, 29 Jul 2025 20:13:43 +0200 Subject: [PATCH 5/6] fix: norme compliance --- src/player/angle.c | 47 ++++++++++++++++++++++++------------------ src/player/move.h | 4 ++-- src/player/player.c | 3 ++- src/utils/keypresses.h | 4 ++-- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/player/angle.c b/src/player/angle.c index eeacb76..b2b3cba 100644 --- a/src/player/angle.c +++ b/src/player/angle.c @@ -6,7 +6,7 @@ /* By: tchampio #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; @@ -41,8 +26,30 @@ void turn_right_angle(t_cub3d_data *data) 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); + 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_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); } diff --git a/src/player/move.h b/src/player/move.h index 14e9490..66ea016 100644 --- a/src/player/move.h +++ b/src/player/move.h @@ -6,14 +6,14 @@ /* By: tchampio plane_y = 0; } } + // north south void init_lat(t_player *player, char dir) { diff --git a/src/utils/keypresses.h b/src/utils/keypresses.h index 2f3da60..7d9d426 100644 --- a/src/utils/keypresses.h +++ b/src/utils/keypresses.h @@ -6,14 +6,14 @@ /* By: tchampio Date: Tue, 29 Jul 2025 20:15:20 +0200 Subject: [PATCH 6/6] chore(map): changed spawn angle to north for easier debugging of angles: --- ressources/good_maps/big_square.cub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ressources/good_maps/big_square.cub b/ressources/good_maps/big_square.cub index d0021e4..030724d 100644 --- a/ressources/good_maps/big_square.cub +++ b/ressources/good_maps/big_square.cub @@ -25,7 +25,7 @@ C 225,30,0 1000000000000000000000000000001 1000000000000000000000000000001 1000000000000000000000000000001 -10000000000000E0000000000000001 +10000000000000N0000000000000001 1000000000000000000000000000001 1000000000000000000000000000001 1000000000000000000000000000001