diff --git a/inc/collision.h b/inc/collision.h new file mode 100644 index 0000000..c7a6ab0 --- /dev/null +++ b/inc/collision.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/22 14:40:47 by qmennen #+# #+# */ +/* Updated: 2025/04/22 14:42:35 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef COLLISION_H +# define COLLISION_H + +# include "cub3d.h" + +int collision_horizontal(t_map *map, t_player *player, float xa); +int collision_vertical(t_map *map, t_player *player, float ya); + +#endif diff --git a/inc/cub3d.h b/inc/cub3d.h index 4fb3335..4c19362 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -45,6 +45,7 @@ # include "hooks.h" # include "render.h" # include "player.h" +# include "collision.h" # include "parser.h" #endif diff --git a/src/collision.c b/src/collision.c new file mode 100644 index 0000000..d4350f2 --- /dev/null +++ b/src/collision.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/04/22 14:40:59 by qmennen #+# #+# */ +/* Updated: 2025/04/22 14:42:09 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "collision.h" + + +int collision_horizontal(t_map *map, t_player *player, float xa) +{ + t_tile tile; + + tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE)); + return (tile != TILE_WALL && tile != TILE_VOID); +} + +int collision_vertical(t_map *map, t_player *player, float ya) +{ + t_tile tile; + + tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE)); + return (tile != TILE_WALL && tile != TILE_VOID); +} diff --git a/src/player.c b/src/player.c index 7fbd4da..d1e2c68 100644 --- a/src/player.c +++ b/src/player.c @@ -30,21 +30,6 @@ int player_create(t_game **game) return (SUCCESS); } -static int check_horizontal(t_map *map, t_player *player, float xa) -{ - t_tile tile; - - tile = get_tile(map, (int) ((player->pos.x + xa) / TILE_SIZE), (int) ((player->pos.y) / TILE_SIZE)); - return (tile != TILE_WALL && tile != TILE_VOID); -} - -static int check_vertical(t_map *map, t_player *player, float ya) -{ - t_tile tile; - - tile = get_tile(map, (int) ((player->pos.x) / TILE_SIZE), (int) ((player->pos.y + ya) / TILE_SIZE)); - return (tile != TILE_WALL && tile != TILE_VOID); -} static void move(t_map *map, t_player *player, int dir, float delta) { @@ -53,9 +38,9 @@ static void move(t_map *map, t_player *player, int dir, float delta) xa = dir * (sin(player->angle) * player->speed * delta); ya = dir * -1 * (cos(player->angle) * player->speed * delta); - if ( xa != 0 && check_horizontal(map, player, xa)) + if ( xa != 0 && collision_horizontal(map, player, xa)) player->pos.x += xa; - if ( ya != 0 && check_vertical(map, player, ya)) + if ( ya != 0 && collision_vertical(map, player, ya)) player->pos.y += ya; }