Compare commits

...

6 commits

Author SHA1 Message Date
Theo Champion
a9b5fbd370 chore(map): changed spawn angle to north for easier debugging of angles: 2025-07-29 20:15:20 +02:00
Theo Champion
788f248c5e fix: norme compliance 2025-07-29 20:13:43 +02:00
Theo Champion
b6eeea5e9f feat(player): reimplemented angles 2025-07-29 19:50:23 +02:00
Theo Champion
e46f199606 fix: forgot to add header file 2025-07-29 15:03:10 +02:00
Theo Champion
2e1838e0bd removal(player): removed old angle code as it's incompatible with new methods 2025-07-29 15:02:40 +02:00
Theo Champion
faf5127829 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
2025-07-29 15:01:31 +02:00
15 changed files with 215 additions and 72 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

@ -25,7 +25,7 @@ C 225,30,0
1000000000000000000000000000001
1000000000000000000000000000001
1000000000000000000000000000001
10000000000000E0000000000000001
10000000000000N0000000000000001
1000000000000000000000000000001
1000000000000000000000000000001
1000000000000000000000000000001

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 */
/* */
/* ************************************************************************** */

View file

@ -6,42 +6,50 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/24 14:41:35 by tchampio #+# #+# */
/* Updated: 2025/07/24 14:48:04 by tchampio ### ########.fr */
/* Updated: 2025/07/29 20:09:55 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include <X11/keysym.h>
#include <X11/X.h>
#include "../cub3d_data.h"
#include "../consts.h"
#include <math.h>
#include <X11/keysym.h>
#include <X11/X.h>
#include "player.h"
/* key parameter should be X's value such as XK_a */
void apply_angle(int key, t_cub3d_data *data)
void turn_right_angle(t_cub3d_data *data)
{
double cos_angle;
double sin_angle;
t_player *p;
double prev_dir_x;
double prev_plane_x;
cos_angle = cos(data->player.yaw);
sin_angle = sin(data->player.yaw);
if (key == XK_a)
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_left_angle(t_cub3d_data *data)
{
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;
}
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. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "../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

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

@ -0,0 +1,91 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:58:00 by tchampio #+# #+# */
/* Updated: 2025/07/29 19:47:38 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "angle.h"
#include "../consts.h"
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);
if (data->keypresses.is_left_pressed)
turn_left_angle(data);
if (data->keypresses.is_right_pressed)
turn_right_angle(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 20:13:08 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 20:13:01 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,6 +32,7 @@ void init_lon(t_player *player, char dir)
player->plane_y = 0;
}
}
// north south
void init_lat(t_player *player, char dir)
{
@ -58,6 +59,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);

26
src/utils/keypresses.h Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* keypresses.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/29 13:42:39 by tchampio #+# #+# */
/* Updated: 2025/07/29 20:09:30 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef KEYPRESSES_H
# define KEYPRESSES_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;
#endif // KEYPRESSES_H