\o/ The collisions are working PERFECTLY now except on the borders of the map! \o/

This commit is contained in:
mibi88 2023-07-09 13:37:49 +02:00
parent 909e0760c5
commit 6afde39af8

View file

@ -45,14 +45,19 @@ void player_move(Map *map_level, Player *player, Direction direction) {
const char dy = one_px_mov[direction*2+1]*SPEED; const char dy = one_px_mov[direction*2+1]*SPEED;
/* If the player will collide with a hard tile. */ /* If the player will collide with a hard tile. */
if(player_collision(map_level, player, direction, P_CENTER)){ 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); player_fix_position(player, dx, dy);
}else{ }else{
/* If he won't collide I just move him normally */
if(player_collision(map_level, player, direction, P_RIGHTDOWN) || if(player_collision(map_level, player, direction, P_RIGHTDOWN) ||
player_collision(map_level, player, direction, P_LEFTUP)){ 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 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); 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->x += dx;
player->y += dy; 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]; char dy = one_px_mov[direction*2+1];
if(!dx){ if(!dx){
dx += nomov_axis_check; dx += nomov_axis_check;
dx = dx*(P_WIDTH/2+(nomov_axis_check == P_CENTER));
}else if(!dy){ }else if(!dy){
dy += nomov_axis_check; 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. */ /* The tile he will go to. */
int player_tile_x = (player->x+dx)/T_WIDTH; int player_tile_x = (player->x+dx)/T_WIDTH;
int player_tile_y = (player->y+dy)/T_HEIGHT; int player_tile_y = (player->y+dy)/T_HEIGHT;