summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-14 15:13:26 +0200
committerDominik Kaiser2024-05-14 15:13:26 +0200
commit5a7e68ce0331077594dc2d85c81fc087d80734c7 (patch)
tree3883a11b20f6809bd28f2f12e36b638e900fd15a
parent11c24e171b4dca0b751fc3f43b4d72121106599e (diff)
downloadso_long-5a7e68ce0331077594dc2d85c81fc087d80734c7.tar.gz
so_long-5a7e68ce0331077594dc2d85c81fc087d80734c7.zip
Implement (somewhat) functional collision
-rw-r--r--include/so_long.h9
-rw-r--r--src/collision.c80
-rw-r--r--src/map_utils.c7
-rw-r--r--src/player_process.c32
4 files changed, 101 insertions, 27 deletions
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/08 14:14:02 by dkaiser #+# #+# */
-/* Updated: 2024/05/14 13:52:05 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 14:43:41 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -86,7 +86,10 @@ int draw_map(t_game *game);
void on_key_input(mlx_key_data_t event, void *params);
t_vector grid_to_screen_pos(t_ivector grid_pos, t_ivector tile_size);
t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size);
-int check_collision(t_vector a_pos, t_vector a_size, t_vector b_pos,
- t_vector b_size);
+enum e_tile get_tile(t_tilemap *map, int x, int y);
+int check_collision(t_vector a_pos, t_ivector a_size,
+ t_vector b_pos, t_ivector b_size);
+int check_wall_collision(t_vector a_pos, t_ivector a_size,
+ t_tilemap *map);
#endif // SO_LONG_H
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);
+}
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 13:19:34 by dkaiser #+# #+# */
-/* Updated: 2024/05/14 13:50:23 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 14:44:39 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,3 +29,8 @@ t_ivector screen_to_grid_pos(t_vector screen_pos, t_ivector tile_size)
grid_pos.y = screen_pos.y / tile_size.y;
return (grid_pos);
}
+
+enum e_tile get_tile(t_tilemap *map, int x, int y)
+{
+ return map->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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 12:40:05 by dkaiser #+# #+# */
-/* Updated: 2024/05/14 12:44:17 by dkaiser ### ########.fr */
+/* Updated: 2024/05/14 15:12:28 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
+#include "ft_printf.h"
#include "so_long.h"
static t_vector get_direction_from_input(t_game *game);
void player_process(t_game *game)
{
- t_player player;
+ t_player *player;
+ t_vector collision_pos;
+ int collision;
- player = game->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)