From 74b7eb17ee574efd6bb67e49e70dc62ff5d9d8d5 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 14:20:10 +0200 Subject: [PATCH 1/7] map: removed unused variable j --- src/map.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/map.c b/src/map.c index 8460833..ec50804 100644 --- a/src/map.c +++ b/src/map.c @@ -220,12 +220,12 @@ int map_create(t_game **game, int argc, char **argv) void map_free(t_map *map) { int i; - int j; + //int j; i = 0; while (i < map->height) { - j = 0; + //j = 0; free(map->grid[i]); i++; } From 8cd885f6f81e8195bdff024a1a7714251e30dc0b Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 14:39:51 +0200 Subject: [PATCH 2/7] 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) From cde704228f075422e107e576d6bc2724754b6fb3 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 14:44:15 +0200 Subject: [PATCH 3/7] player, collision: split it up --- inc/collision.h | 21 +++++++++++++++++++++ inc/cub3d.h | 1 + src/collision.c | 30 ++++++++++++++++++++++++++++++ src/player.c | 19 ++----------------- 4 files changed, 54 insertions(+), 17 deletions(-) create mode 100644 inc/collision.h create mode 100644 src/collision.c 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; } From 1252edf661a76562aedc15267ab5de0de7c70936 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 14:45:09 +0200 Subject: [PATCH 4/7] player: fix includes --- src/player.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/player.c b/src/player.c index d1e2c68..a9777be 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) { From 89e0ef21c998436907395db385f150c36cc1a980 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 15:23:02 +0200 Subject: [PATCH 5/7] player: direction vector --- inc/types.h | 2 +- src/player.c | 24 +++++++++++++++++------- src/util/keyboard.c | 4 ++-- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/inc/types.h b/inc/types.h index 968b234..9d94f0d 100644 --- a/inc/types.h +++ b/inc/types.h @@ -33,8 +33,8 @@ typedef struct s_vec2 typedef struct s_player { t_vec2 pos; + t_vec2 dir; float speed; - float angle; float fov; } t_player; diff --git a/src/player.c b/src/player.c index a9777be..98c95c6 100644 --- a/src/player.c +++ b/src/player.c @@ -21,7 +21,8 @@ int player_create(t_game **game) return (FAILURE); player->pos.x = 2 * TILE_SIZE; player->pos.y = 2 * TILE_SIZE; - player->angle = 0.f; + player->dir.x = 1; + player->dir.y = 0; player->speed = 80.f; player->fov = 90.f; (*game)->player = player; @@ -34,14 +35,23 @@ 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); + 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, float rot_speed) +{ + 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) { if (get_key(game, MLX_KEY_W)) @@ -49,9 +59,9 @@ void player_update(t_game *game) else if (get_key(game, MLX_KEY_S)) move(game->map, game->player, -1, game->screen->mlx->delta_time); if (get_key(game, MLX_KEY_LEFT)) - game->player->angle -= .1f; + rotate(game->player, .1f); else if (get_key(game, MLX_KEY_RIGHT)) - game->player->angle += .1f; + rotate(game->player, -.1f); } void player_render(t_screen *screen, t_player *player) @@ -61,7 +71,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++; From 5bac9125d6d9e689c809cfdc5326759f3e8a7800 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 15:26:01 +0200 Subject: [PATCH 6/7] refactor: delta time as an argument --- inc/player.h | 4 ++-- src/game.c | 6 ++++-- src/map.c | 1 - src/player.c | 6 +++--- 4 files changed, 9 insertions(+), 8 deletions(-) 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/src/game.c b/src/game.c index d74a9ea..1e2c2c1 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/17 20:01:19 by qmennen ### ########.fr */ +/* Updated: 2025/04/22 15:24:24 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,10 +31,12 @@ int game_create(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 87ec3c1..c3bca31 100644 --- a/src/map.c +++ b/src/map.c @@ -238,6 +238,5 @@ 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 98c95c6..cafb1ef 100644 --- a/src/player.c +++ b/src/player.c @@ -52,12 +52,12 @@ static void rotate(t_player *player, float 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->map, 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->map, 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, .1f); else if (get_key(game, MLX_KEY_RIGHT)) From 840accea527737c92448e1b4b32900cbecb06263 Mon Sep 17 00:00:00 2001 From: Quinten Date: Tue, 22 Apr 2025 15:28:07 +0200 Subject: [PATCH 7/7] feat: setup camera plane --- inc/types.h | 1 + src/player.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/inc/types.h b/inc/types.h index 9d94f0d..f854ac7 100644 --- a/inc/types.h +++ b/inc/types.h @@ -34,6 +34,7 @@ typedef struct s_player { t_vec2 pos; t_vec2 dir; + t_vec2 camera; float speed; float fov; } t_player; diff --git a/src/player.c b/src/player.c index cafb1ef..2649f91 100644 --- a/src/player.c +++ b/src/player.c @@ -23,6 +23,8 @@ int player_create(t_game **game) 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;