Corregido un bug en las colisiones. El rectangulo era un pixel demasiado bajo o estrecho

This commit is contained in:
2022-09-10 21:04:42 +02:00
parent c905c348d5
commit cc68a02111
6 changed files with 45 additions and 23 deletions

View File

@@ -50,7 +50,7 @@ Player::Player(player_t ini, std::string tileset, std::string animation, SDL_Ren
underFeet.insert(underFeet.end(), {p, p});
feet.insert(feet.end(), {p, p});
line = {0, 0, 0, 0};
r = {0, 0, 0, 0};
}
// Destructor
@@ -67,11 +67,20 @@ void Player::render()
sprite->render();
if (debug->getEnabled())
{
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
// Pinta los underfeet
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
SDL_RenderDrawPoint(renderer, underFeet[0].x, underFeet[0].y);
SDL_RenderDrawPoint(renderer, underFeet[1].x, underFeet[1].y);
SDL_RenderDrawLine(renderer, line.x1, line.y1, line.x2, line.y2);
// Pinta rectangulo del jugador
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 192);
SDL_Rect rect = getRect();
SDL_RenderFillRect(renderer, &rect);
// Pinta el rectangulo de movimiento
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &r);
debug->add("RECT: " + std::to_string(r.x) +","+ std::to_string(r.y) +","+ std::to_string(r.w) +","+ std::to_string(r.h));
}
}
@@ -244,9 +253,11 @@ void Player::move()
SDL_Rect proj;
proj.x = (int)(x + vx);
proj.y = (int)y;
proj.h = h - 1;
proj.h = h;
proj.w = ceil(abs(vx)); // Para evitar que tenga un ancho de 0 pixels
r = proj;
// Comprueba la colisión con las superficies
const int pos = room->checkRightSurfaces(&proj);
@@ -268,7 +279,7 @@ void Player::move()
if (ly > -1)
{
y = ly - h;
setState(s_standing);
// setState(s_standing);
}
}
@@ -286,9 +297,11 @@ void Player::move()
SDL_Rect proj;
proj.x = (int)x + w;
proj.y = (int)y;
proj.h = h - 1;
proj.h = h;
proj.w = ceil(vx); // Para evitar que tenga un ancho de 0 pixels
r = proj;
// Comprueba la colisión
const int pos = room->checkLeftSurfaces(&proj);
@@ -310,7 +323,7 @@ void Player::move()
if (ry > -1)
{
y = ry - h;
setState(s_standing);
// setState(s_standing);
}
}
@@ -330,12 +343,14 @@ void Player::move()
// Se mueve hacia arriba
if (vy < 0.0f)
{
// Crea el rectangulo de proyección en el eje X para ver si colisiona
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)(y + vy);
proj.h = ceil(abs(vy)); // Para evitar que tenga una altura de 0 pixels
proj.w = w - 1;
proj.w = w;
r = proj;
// Comprueba la colisión
const int pos = room->checkBottomSurfaces(&proj);
@@ -355,12 +370,14 @@ void Player::move()
// Se mueve hacia abajo
else if (vy > 0.0f)
{
// Crea el rectangulo de proyección en el eje X para ver si colisiona
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y + h;
proj.h = ceil(vy); // Para evitar que tenga una altura de 0 pixels
proj.w = w - 1;
proj.w = w;
r = proj;
// Comprueba la colisión con los muros
const int pos = room->checkTopSurfaces(&proj);