fix: collisions on corners
This commit is contained in:
parent
f0680246a6
commit
e7fc155581
3 changed files with 94 additions and 37 deletions
40
src/move.c
40
src/move.c
|
@ -3,14 +3,15 @@
|
|||
/* ::: :::::::: */
|
||||
/* move.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: Zy <frzysk@proton.me> +#+ +:+ +#+ */
|
||||
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/11/13 18:39:22 by Zy ### ########.fr */
|
||||
/* Updated: 2024/11/13 21:42:40 by mc ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "move.h"
|
||||
#include "move_utils.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "map.h"
|
||||
|
@ -18,41 +19,6 @@
|
|||
#include "const.h"
|
||||
#include "input.h"
|
||||
|
||||
static void push_from_wall(t_point_int c)
|
||||
{
|
||||
if (map_get_case(&g_map, c.x, c.y)->wall == EMPTY)
|
||||
return ;
|
||||
if (g_map.player.pos.x < c.x + 1 + HITBOX && g_map.player.pos.x > c.x + 1)
|
||||
g_map.player.pos.x = c.x + 1 + HITBOX;
|
||||
if (g_map.player.pos.x > c.x - HITBOX && g_map.player.pos.x < c.x)
|
||||
g_map.player.pos.x = c.x - HITBOX;
|
||||
if (g_map.player.pos.y < c.y + 1 + HITBOX && g_map.player.pos.y > c.y + 1)
|
||||
g_map.player.pos.y = c.y + 1 + HITBOX;
|
||||
if (g_map.player.pos.y > c.y - HITBOX && g_map.player.pos.y < c.y)
|
||||
g_map.player.pos.y = c.y - HITBOX;
|
||||
}
|
||||
|
||||
static void push_from_walls(void)
|
||||
{
|
||||
t_point_int c;
|
||||
t_point_int p;
|
||||
|
||||
p.x = g_map.player.pos.x;
|
||||
p.y = g_map.player.pos.y;
|
||||
c.x = p.x - 1;
|
||||
c.y = p.y;
|
||||
push_from_wall(c);
|
||||
c.x = p.x + 1;
|
||||
c.y = p.y;
|
||||
push_from_wall(c);
|
||||
c.x = p.x;
|
||||
c.y = p.y - 1;
|
||||
push_from_wall(c);
|
||||
c.x = p.x;
|
||||
c.y = p.y + 1;
|
||||
push_from_wall(c);
|
||||
}
|
||||
|
||||
static int move_forward(int factor)
|
||||
{
|
||||
t_point_double dir;
|
||||
|
|
73
src/move_utils.c
Normal file
73
src/move_utils.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/13 21:42:57 by mc #+# #+# */
|
||||
/* Updated: 2024/11/13 21:46:49 by mc ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "move_utils.h"
|
||||
|
||||
#include "utils.h"
|
||||
#include "map.h"
|
||||
#include "global.h"
|
||||
#include "const.h"
|
||||
|
||||
static void move_in_direction(t_point_double *pos, t_direction dir,
|
||||
double value)
|
||||
{
|
||||
if (dir == NORTH)
|
||||
pos->y -= value;
|
||||
else if (dir == SOUTH)
|
||||
pos->y += value;
|
||||
else if (dir == EAST)
|
||||
pos->x -= value;
|
||||
else if (dir == WEST)
|
||||
pos->x += value;
|
||||
}
|
||||
|
||||
static void push_from_wall(int cx, int cy)
|
||||
{
|
||||
const t_point_double p = g_map.player.pos;
|
||||
double distance_with_side[4];
|
||||
t_direction d;
|
||||
t_direction d_min;
|
||||
const t_point_double c_d = {cx, cy};
|
||||
|
||||
if (map_get_case(&g_map, cx, cy)->wall == EMPTY)
|
||||
return ;
|
||||
if (p.x > c_d.x - HITBOX && p.x < c_d.x + 1 + HITBOX
|
||||
&& p.y > c_d.y - HITBOX && p.y < c_d.y + 1 + HITBOX)
|
||||
{
|
||||
distance_with_side[NORTH] = p.y - (c_d.y - HITBOX);
|
||||
distance_with_side[SOUTH] = (c_d.y + 1 + HITBOX) - p.y;
|
||||
distance_with_side[EAST] = p.x - (c_d.x - HITBOX);
|
||||
distance_with_side[WEST] = (c_d.x + 1 + HITBOX) - p.x;
|
||||
d_min = 0;
|
||||
d = 0;
|
||||
while (++d < 4)
|
||||
if (distance_with_side[d] < distance_with_side[d_min])
|
||||
d_min = d;
|
||||
move_in_direction(&g_map.player.pos, d_min, distance_with_side[d_min]);
|
||||
}
|
||||
}
|
||||
|
||||
void push_from_walls(void)
|
||||
{
|
||||
t_point_int p;
|
||||
|
||||
p.x = (int)g_map.player.pos.x;
|
||||
p.y = (int)g_map.player.pos.y;
|
||||
push_from_wall(p.x - 1, p.y);
|
||||
push_from_wall(p.x + 1, p.y);
|
||||
push_from_wall(p.x, p.y - 1);
|
||||
push_from_wall(p.x, p.y + 1);
|
||||
push_from_wall(p.x - 1, p.y - 1);
|
||||
push_from_wall(p.x - 1, p.y + 1);
|
||||
push_from_wall(p.x + 1, p.y - 1);
|
||||
push_from_wall(p.x + 1, p.y + 1);
|
||||
}
|
18
src/move_utils.h
Normal file
18
src/move_utils.h
Normal file
|
@ -0,0 +1,18 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* move_utils.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/13 21:43:25 by mc #+# #+# */
|
||||
/* Updated: 2024/11/13 21:43:51 by mc ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MOVE_UTILS_H
|
||||
# define MOVE_UTILS_H
|
||||
|
||||
void push_from_walls(void);
|
||||
|
||||
#endif
|
Loading…
Add table
Reference in a new issue