From 8cd885f6f81e8195bdff024a1a7714251e30dc0b Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 14:39:51 +0200 Subject: [PATCH] player: sliding collisions --- inc/map.h | 1 + src/map.c | 9 +++++++++ src/player.c | 40 +++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/inc/map.h b/inc/map.h index 15466f9..663e20d 100644 --- a/inc/map.h +++ b/inc/map.h @@ -18,5 +18,6 @@ int map_create(t_game **game, int argc, char **argv); void map_free(t_map *map); void print_map(t_map *map); +t_tile get_tile(t_map * map, int x, int y); #endif diff --git a/src/map.c b/src/map.c index ec50804..87ec3c1 100644 --- a/src/map.c +++ b/src/map.c @@ -3,6 +3,7 @@ #include #include "cub3d.h" #include "libft.h" +#include "types.h" #define FAILURE 0 #define SUCCESS 1 @@ -232,3 +233,11 @@ void map_free(t_map *map) free(map->grid); free(map); } + +t_tile get_tile(t_map * map, int x, int y) +{ + if (x < 0 || y < 0 || x >= map->width || y >= map->height) + return (TILE_WALL); + printf("tile at %i %i: %i\n", x, y, map->grid[y][x]); + return (map->grid[y][x]); +} diff --git a/src/player.c b/src/player.c index 746c70e..7fbd4da 100644 --- a/src/player.c +++ b/src/player.c @@ -21,8 +21,8 @@ int player_create(t_game **game) player = malloc(sizeof(t_player)); if (!player) return (FAILURE); - player->pos.x = 20.f; - player->pos.y = 20.f; + player->pos.x = 2 * TILE_SIZE; + player->pos.y = 2 * TILE_SIZE; player->angle = 0.f; player->speed = 80.f; player->fov = 90.f; @@ -30,27 +30,45 @@ int player_create(t_game **game) return (SUCCESS); } -static void move(t_player *player, int dir, float delta) +static int check_horizontal(t_map *map, t_player *player, float xa) { - player->pos.x += dir * (sin(player->angle) * player->speed * delta); - player->pos.y += dir * -1 * (cos(player->angle) * player->speed * delta); + 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 void rotate(t_player *player, int dir) +static int check_vertical(t_map *map, t_player *player, float ya) { - player->angle += dir * .1f; + 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) +{ + float xa; + float ya; + + 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)) + player->pos.x += xa; + if ( ya != 0 && check_vertical(map, player, ya)) + player->pos.y += ya; } void player_update(t_game *game) { if (get_key(game, MLX_KEY_W)) - move(game->player, 1, game->screen->mlx->delta_time); + move(game->map, game->player, 1, game->screen->mlx->delta_time); else if (get_key(game, MLX_KEY_S)) - move(game->player, -1, game->screen->mlx->delta_time); + move(game->map, game->player, -1, game->screen->mlx->delta_time); if (get_key(game, MLX_KEY_LEFT)) - rotate(game->player, -1); + game->player->angle -= .1f; else if (get_key(game, MLX_KEY_RIGHT)) - rotate(game->player, 1); + game->player->angle += .1f; } void player_render(t_screen *screen, t_player *player)