From 300128b8b0eaf4e6dd04c076887721dc645c939e Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 May 2024 12:45:49 +0200 Subject: Outsource player processing and add collision func --- src/collision.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/collision.c (limited to 'src/collision.c') diff --git a/src/collision.c b/src/collision.c new file mode 100644 index 0000000..fee1452 --- /dev/null +++ b/src/collision.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collision.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser b_pos.x + && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) + { + if (a_pos.x < b_pos.x) + result |= RIGHT; + if (a_pos.x > b_pos.x) + result |= LEFT; + if (a_pos.y < b_pos.y) + result |= DOWN; + if (a_pos.y > b_pos.y) + result |= UP; + } + return (result); +} + -- cgit v1.2.3 From 5a7e68ce0331077594dc2d85c81fc087d80734c7 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 May 2024 15:13:26 +0200 Subject: Implement (somewhat) functional collision --- include/so_long.h | 9 ++++-- src/collision.c | 80 ++++++++++++++++++++++++++++++++++++++++++---------- src/map_utils.c | 7 ++++- src/player_process.c | 32 +++++++++++++++------ 4 files changed, 101 insertions(+), 27 deletions(-) (limited to 'src/collision.c') diff --git a/include/so_long.h b/include/so_long.h index 6834620..cddfa9e 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser b_pos.x && a_pos.y < b_size.y + b_pos.y && a_pos.y + a_size.y > b_pos.y) - { - if (a_pos.x < b_pos.x) - result |= RIGHT; - if (a_pos.x > b_pos.x) - result |= LEFT; - if (a_pos.y < b_pos.y) - result |= DOWN; - if (a_pos.y > b_pos.y) - result |= UP; - } + { + if (a_pos.x < b_pos.x) + result |= RIGHT; + if (a_pos.x > b_pos.x) + result |= LEFT; + if (a_pos.y < b_pos.y) + result |= DOWN; + if (a_pos.y > b_pos.y) + result |= UP; + } return (result); } +int check_wall_collision(t_vector a_pos, t_ivector a_size, t_tilemap *map) +{ + int result; + t_ivector a_tile; + t_ivector check_tile; + int x; + int y; + + result = 0; + a_tile = screen_to_grid_pos(a_pos, map->tile_size); + x = -1; + while (x <= 1) + { + y = -1; + while (y <= 1) + { + check_tile.x = a_tile.x + x; + check_tile.y = a_tile.y + y; + result |= check_wall_collision_with_cell(a_pos, a_size, map, + check_tile); + y++; + } + x++; + } + return (result); +} + +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile) +{ + t_vector wall_pos; + t_ivector wall_size; + + if (get_tile(map, tile.x, tile.y) == WALL) + { + wall_pos = grid_to_screen_pos(tile, map->tile_size); + wall_size.x = map->tile_size.x; + wall_size.y = map->tile_size.y; + return (check_collision(a_pos, a_size, wall_pos, wall_size)); + } + return (0); +} diff --git a/src/map_utils.c b/src/map_utils.c index 54577a8..82acaa9 100644 --- a/src/map_utils.c +++ b/src/map_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser tiles[y * map->grid_size.x + x]; +} diff --git a/src/player_process.c b/src/player_process.c index 8cabe9a..5fcf994 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,26 +6,42 @@ /* By: dkaiser player; - player.direction = get_direction_from_input(game); - player.velocity.x = player.direction.x * PLAYER_MOVE_SPEED + player = &game->player; + player->direction = get_direction_from_input(game); + player->velocity.x = player->direction.x * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.velocity.y = player.direction.y * PLAYER_MOVE_SPEED + player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - player.position.x += player.velocity.x; - player.position.y += player.velocity.y; + + collision_pos.x = player->position.x + player->velocity.x; + collision_pos.y = player->position.y + player->velocity.y; + collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); + if (collision & LEFT && player->velocity.x < 0) + player->velocity.x = 0; + if (collision & RIGHT && player->velocity.x > 0) + player->velocity.x = 0; + if (collision & UP && player->velocity.y < 0) + player->velocity.y = 0; + if (collision & DOWN && player->velocity.y > 0) + player->velocity.y = 0; + + player->position.x += player->velocity.x; + player->position.y += player->velocity.y; } static t_vector get_direction_from_input(t_game *game) -- cgit v1.2.3 From ed5342399d02ba5d1032a2ef99afdc1c875ed4d8 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 May 2024 12:16:05 +0200 Subject: Change collision checking and add player.size Collision checking will now be done using the move_and_slide function. Instead of tile_size, now player.size will be used for checking collisions. --- include/so_long.h | 4 +++- src/collision.c | 24 ++++++++++++++++++++++-- src/init.c | 10 ++++++---- src/player_process.c | 19 ++----------------- 4 files changed, 33 insertions(+), 24 deletions(-) (limited to 'src/collision.c') diff --git a/include/so_long.h b/include/so_long.h index cddfa9e..97ef5c7 100644 --- a/include/so_long.h +++ b/include/so_long.h @@ -6,7 +6,7 @@ /* By: dkaiser position.x + player->velocity.x; + move_pos.y = player->position.y; + + if ((check_wall_collision(move_pos, player->size, map) & (RIGHT | LEFT)) == 0) + player->position.x = move_pos.x; + else + player->velocity.x = 0; + + move_pos.x = player->position.x; + move_pos.y = player->position.y + player->velocity.y; + if ((check_wall_collision(move_pos, player->size, map) & (UP | DOWN)) == 0) + player->position.y = move_pos.y; + else + player->velocity.y = 0; +} diff --git a/src/init.c b/src/init.c index 5277b5c..9959da6 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: dkaiser player; texture = mlx_load_png("textures/player.png"); - player->position.x = game->map.player_start_tile.x * 48; - player->position.y = game->map.player_start_tile.y * 48; + player->position.x = game->map.player_start_tile.x * game->map.grid_size.x; + player->position.y = game->map.player_start_tile.y * game->map.grid_size.y; player->velocity.x = 0; player->velocity.y = 0; + player->size.x = 44; + player->size.y = 44; player->img = mlx_texture_to_image(game->mlx, texture); - mlx_resize_image(player->img, 48, 48); + mlx_resize_image(player->img, player->size.x, player->size.y); mlx_image_to_window(game->mlx, player->img, player->position.x, player->position.y); } diff --git a/src/player_process.c b/src/player_process.c index 5fcf994..52277f8 100644 --- a/src/player_process.c +++ b/src/player_process.c @@ -6,7 +6,7 @@ /* By: dkaiser player; player->direction = get_direction_from_input(game); @@ -28,20 +26,7 @@ void player_process(t_game *game) player->velocity.y = player->direction.y * PLAYER_MOVE_SPEED * game->mlx->delta_time; - collision_pos.x = player->position.x + player->velocity.x; - collision_pos.y = player->position.y + player->velocity.y; - collision = check_wall_collision(collision_pos, game->map.tile_size, &game->map); - if (collision & LEFT && player->velocity.x < 0) - player->velocity.x = 0; - if (collision & RIGHT && player->velocity.x > 0) - player->velocity.x = 0; - if (collision & UP && player->velocity.y < 0) - player->velocity.y = 0; - if (collision & DOWN && player->velocity.y > 0) - player->velocity.y = 0; - - player->position.x += player->velocity.x; - player->position.y += player->velocity.y; + move_and_slide(player, &game->map); } static t_vector get_direction_from_input(t_game *game) -- cgit v1.2.3