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/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/inc/player.h b/inc/player.h index 880235a..49b4687 100644 --- a/inc/player.h +++ b/inc/player.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */ -/* Updated: 2025/04/17 19:49:36 by qmennen ### ########.fr */ +/* Updated: 2025/04/22 15:23:45 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,6 @@ int player_create(t_game **game); void player_render(t_screen *screen, t_player *player); -void player_update(t_game *game); +void player_update(t_game *game, float delta_time); #endif diff --git a/inc/types.h b/inc/types.h index 5be3803..44829d0 100644 --- a/inc/types.h +++ b/inc/types.h @@ -33,8 +33,9 @@ typedef struct s_vec2 typedef struct s_player { t_vec2 pos; + t_vec2 dir; + t_vec2 camera; float speed; - float angle; float fov; } t_player; 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/game.c b/src/game.c index 81039f6..d52ad3d 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ -/* Updated: 2025/04/22 12:58:50 by whaffman ######## odam.nl */ +/* Updated: 2025/04/22 15:42:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,10 +45,12 @@ void free_game(t_game **game) void game_loop(void *param) { t_game *game; + float delta_time; game = (t_game *)param; + delta_time = game->screen->mlx->delta_time; render_clear(game->screen); - player_update(game); + player_update(game, delta_time); render_entities(game); render_map(game->screen, game->map); keyboard_update(game); // Goes last diff --git a/src/map.c b/src/map.c index cf79e22..ba56915 100644 --- a/src/map.c +++ b/src/map.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:20:51 by whaffman #+# #+# */ -/* Updated: 2025/04/22 13:20:52 by whaffman ######## odam.nl */ +/* Updated: 2025/04/22 15:43:35 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include #include "cub3d.h" #include "libft.h" +#include "types.h" #define FAILURE 0 #define SUCCESS 1 @@ -247,3 +248,10 @@ void map_free(t_map *map) free(map->EA_texture); 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); + return (map->grid[y][x]); +} diff --git a/src/player.c b/src/player.c index 746c70e..2649f91 100644 --- a/src/player.c +++ b/src/player.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "player.h" -#include "render.h" -#include "types.h" +#include "cub3d.h" int player_create(t_game **game) { @@ -21,36 +19,51 @@ 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->angle = 0.f; + player->pos.x = 2 * TILE_SIZE; + player->pos.y = 2 * TILE_SIZE; + player->dir.x = 1; + player->dir.y = 0; + player->camera.x = 0; + player->camera.y = 0.66f; player->speed = 80.f; player->fov = 90.f; (*game)->player = player; return (SUCCESS); } -static void move(t_player *player, int dir, float delta) + +static void move(t_map *map, t_player *player, int dir, float delta) { - player->pos.x += dir * (sin(player->angle) * player->speed * delta); - player->pos.y += dir * -1 * (cos(player->angle) * player->speed * delta); + float xa; + float ya; + + xa = dir * player->dir.x * player->speed * delta; + ya = dir * -1 * player->dir.y * player->speed * delta; + if ( xa != 0 && collision_horizontal(map, player, xa)) + player->pos.x += xa; + if ( ya != 0 && collision_vertical(map, player, ya)) + player->pos.y += ya; } -static void rotate(t_player *player, int dir) +static void rotate(t_player *player, float rot_speed) { - player->angle += dir * .1f; + double old_x; + + old_x = player->dir.x; + player->dir.x = player->dir.x * cos(rot_speed) - player->dir.y * sin(rot_speed); + player->dir.y = old_x * sin(rot_speed) + player->dir.y * cos(rot_speed); } -void player_update(t_game *game) +void player_update(t_game *game, float delta_time) { if (get_key(game, MLX_KEY_W)) - move(game->player, 1, game->screen->mlx->delta_time); + move(game->map, game->player, 1, 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, delta_time); if (get_key(game, MLX_KEY_LEFT)) - rotate(game->player, -1); + rotate(game->player, .1f); else if (get_key(game, MLX_KEY_RIGHT)) - rotate(game->player, 1); + rotate(game->player, -.1f); } void player_render(t_screen *screen, t_player *player) @@ -60,7 +73,7 @@ void player_render(t_screen *screen, t_player *player) if (player->pos.x < 0 || player->pos.x >= screen->width || player->pos.y < 0 || player->pos.y >= screen->height) return ; render_circle(screen, player->pos, 4, 0x3294a8ff); - direction.x = player->pos.x + sin(player->angle) * 30; - direction.y = player->pos.y - cos(player->angle) * 30; + direction.x = player->pos.x + player->dir.x * 30; + direction.y = player->pos.y - player->dir.y * 30; render_line(screen, player->pos, direction, 0xa83232ff); } diff --git a/src/util/keyboard.c b/src/util/keyboard.c index 9abdf1a..d82a32f 100644 --- a/src/util/keyboard.c +++ b/src/util/keyboard.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */ -/* Updated: 2025/04/17 19:59:53 by qmennen ### ########.fr */ +/* Updated: 2025/04/22 14:46:18 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ int keyboard_create(t_game **game) return (FAILURE); (*game)->keyboard = keyboard; i = 0; - while (i < NUM_KEYS) + while (i < NUM_KEYS) // TODO: Better { (*game)->keyboard->keys[i] = 0; i++;