diff options
| author | Dominik Kaiser | 2024-05-14 15:13:26 +0200 |
|---|---|---|
| committer | Dominik Kaiser | 2024-05-14 15:13:26 +0200 |
| commit | 5a7e68ce0331077594dc2d85c81fc087d80734c7 (patch) | |
| tree | 3883a11b20f6809bd28f2f12e36b638e900fd15a /src/collision.c | |
| parent | 11c24e171b4dca0b751fc3f43b4d72121106599e (diff) | |
| download | so_long-5a7e68ce0331077594dc2d85c81fc087d80734c7.tar.gz so_long-5a7e68ce0331077594dc2d85c81fc087d80734c7.zip | |
Implement (somewhat) functional collision
Diffstat (limited to 'src/collision.c')
| -rw-r--r-- | src/collision.c | 80 |
1 files changed, 65 insertions, 15 deletions
diff --git a/src/collision.c b/src/collision.c index fee1452..a45f339 100644 --- a/src/collision.c +++ b/src/collision.c @@ -6,30 +6,80 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/14 11:48:59 by dkaiser #+# #+# */ -/* Updated: 2024/05/14 12:26:02 by dkaiser ### ########.fr */ +/* Updated: 2024/05/14 14:59:13 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "so_long.h" -int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos, - t_vector b_size) +static int check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size, + t_tilemap *map, t_ivector tile); + +/* + * Checks if a and b are colliding and returns the direction relative to a. + * Example: The top left edge of a and the bottom right of b overlap. + * In this case, check_collision() will return (UP | LEFT). + */ +int check_collision(t_vector a_pos, t_ivector a_size, t_vector b_pos, + t_ivector b_size) { - int result; + int result; - result = 0; + result = 0; if (a_pos.x < b_size.x + b_pos.x && a_pos.x + a_size.x > 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); +} |
