]> git.dkaiser.de - 42/so_long.git/commitdiff
Implement (somewhat) functional collision
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 14 May 2024 13:13:26 +0000 (15:13 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 14 May 2024 13:13:26 +0000 (15:13 +0200)
include/so_long.h
src/collision.c
src/map_utils.c
src/player_process.c

index 68346200daa639a4cdeb29e2eff6b9c3c0968711..cddfa9e6dc478a887e46cd93aa83b03823cbc097 100644 (file)
@@ -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
index fee1452fd9c2bdbad53cec7677340ea336e3711c..a45f3392009aec595411a2e6e422b9c0690db7ba 100644 (file)
@@ -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);
+}
index 54577a859eaf6dd0d8949b17c3b1aba813b23f2a..82acaa9920887003a97dfe81d64a1f6bfbc6c693 100644 (file)
@@ -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];
+}
index 8cabe9ac2a2ab7f9ecb55aba6e53327c00878cd4..5fcf994019eae66969b6469000d662c475e688d9 100644 (file)
@@ -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)