From 6afde39af89417294df6215158b786f0d6fb3a6b Mon Sep 17 00:00:00 2001 From: mibi88 <76903855+mibi88@users.noreply.github.com> Date: Sun, 9 Jul 2023 13:37:49 +0200 Subject: [PATCH] \o/ The collisions are working PERFECTLY now except on the borders of the map! \o/ --- src/player.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/player.c b/src/player.c index fcaba7b..bc2efcc 100644 --- a/src/player.c +++ b/src/player.c @@ -45,14 +45,19 @@ void player_move(Map *map_level, Player *player, Direction direction) { const char dy = one_px_mov[direction*2+1]*SPEED; /* If the player will collide with a hard tile. */ if(player_collision(map_level, player, direction, P_CENTER)){ + /* If the will collide with the center of the player. */ player_fix_position(player, dx, dy); }else{ - /* If he won't collide I just move him normally */ if(player_collision(map_level, player, direction, P_RIGHTDOWN) || player_collision(map_level, player, direction, P_LEFTUP)){ + /* If the will collide with the edges of the player. */ /* I fix his position so he won't be partially in the tile. */ + /* I invert dx and dy to fix the axis where he is not moving on. */ + /* Do not replace dx==0 with !dx or dy==0 with !dy, it won't work! + */ player_fix_position(player, dx==0, dy==0); } + /* If he won't collide with the center, so I just move him normally */ player->x += dx; player->y += dy; } @@ -71,11 +76,11 @@ bool player_collision(Map *map_level, Player *player, Direction direction, char dy = one_px_mov[direction*2+1]; if(!dx){ dx += nomov_axis_check; - dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER)); }else if(!dy){ dy += nomov_axis_check; - dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER)); } + dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER)); + dy = dy*(P_HEIGHT/2+(nomov_axis_check == P_CENTER)); /* The tile he will go to. */ int player_tile_x = (player->x+dx)/T_WIDTH; int player_tile_y = (player->y+dy)/T_HEIGHT;