]> git.dkaiser.de - 42/so_long.git/commitdiff
Change collision checking and add player.size
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Wed, 15 May 2024 10:16:05 +0000 (12:16 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Wed, 15 May 2024 10:16:05 +0000 (12:16 +0200)
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
src/collision.c
src/init.c
src/player_process.c

index cddfa9e6dc478a887e46cd93aa83b03823cbc097..97ef5c72882c3023c3bb23b4078a93effe912c1c 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 14:43:41 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/15 12:06:35 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -55,6 +55,7 @@ typedef struct s_player
        t_vector        position;
        t_vector        direction;
        t_vector        velocity;
+       t_ivector       size;
        mlx_image_t     *img;
 }                              t_player;
 
@@ -91,5 +92,6 @@ 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);
+void                   move_and_slide(t_player *player, t_tilemap *map);
 
 #endif // SO_LONG_H
index a45f3392009aec595411a2e6e422b9c0690db7ba..d21e321be0afee4135d4f7fbd58db119729a480a 100644 (file)
@@ -6,14 +6,14 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/14 11:48:59 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/14 14:59:13 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/15 12:06:55 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "so_long.h"
 
 static int     check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size,
-                               t_tilemap *map, t_ivector tile);
+                                                                                  t_tilemap *map, t_ivector tile);
 
 /*
  * Checks if a and b are colliding and returns the direction relative to a.
@@ -83,3 +83,23 @@ static int   check_wall_collision_with_cell(t_vector a_pos, t_ivector a_size,
        }
        return (0);
 }
+
+void move_and_slide(t_player *player, t_tilemap *map)
+{
+       t_vector move_pos;
+
+       move_pos.x = player->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;
+}
index 5277b5cf2f92cc5185601e566c9aab6fe7d643ed..9959da60996a54348fe5b02151e78fdcf6c8c68d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 14:50:09 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/11 16:23:54 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/15 12:13:03 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -40,12 +40,14 @@ static void init_player(t_game *game)
 
        player = &game->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);
 }
index 5fcf994019eae66969b6469000d662c475e688d9..52277f87e8d57ae28d4e6d2e7371063fc50bd7c1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/14 12:40:05 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/14 15:12:28 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/15 12:03:52 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -18,8 +18,6 @@ static t_vector       get_direction_from_input(t_game *game);
 void   player_process(t_game *game)
 {
        t_player        *player;
-       t_vector collision_pos;
-    int collision;
 
        player = &game->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)