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
This commit is contained in:
Theo Champion 2025-07-29 14:58:30 +02:00
parent ac94e9e937
commit faf5127829
11 changed files with 143 additions and 40 deletions

View file

@ -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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
{

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 ");

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

87
src/player/move.c Normal file
View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:58:00 by tchampio #+# #+# */
/* Updated: 2025/07/29 14:52:54 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include <stdio.h> // 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);
}

20
src/player/move.h Normal file
View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:58:49 by tchampio #+# #+# */
/* Updated: 2025/07/29 13:59:29 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MOVE_H
# define MOVE_H
#include "../cub3d_data.h"
void move_player(t_cub3d_data *data);
#endif

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 11:42:44 by tchampio #+# #+# */
/* Updated: 2025/07/29 12:04:52 by tchampio ### ########.fr */
/* Updated: 2025/07/29 14:44:44 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -58,6 +58,7 @@ void init_player(t_player *player, t_mapdata *map)
dir = map->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')

View file

@ -6,11 +6,12 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <X11/keysym.h>

View file

@ -6,7 +6,7 @@
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);