Las colisiones siguen fallando

This commit is contained in:
2022-09-08 23:33:46 +02:00
parent 98916cd1be
commit 197bf71c12
5 changed files with 64 additions and 65 deletions

View File

@@ -211,7 +211,7 @@ void Game::renderDebugInfo()
SDL_RenderFillRect(renderer, &rect);
// Pinta la rejilla
/*SDL_SetRenderDrawColor(renderer, 255, 255, 255, 48);
/*SDL_SetRenderDrawColor(renderer, 255, 255, 255, 32);
for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8)
{ // Lineas horizontales
SDL_RenderDrawLine(renderer, 0, i, PLAY_AREA_RIGHT, i);

View File

@@ -240,8 +240,14 @@ void Player::move()
SDL_Rect proj;
proj.x = (int)x;
proj.y = (int)y;
proj.h = h - 1;
proj.h = h;
proj.w = (int)vx;
// **new
proj.x = (int)(x+vx);
proj.y = (int)y;
proj.h = h;
proj.w = (int)abs(vx);
// Comprueba la colisión
const int pos = room->checkRightSurfaces(&proj);
@@ -252,8 +258,8 @@ void Player::move()
x += vx;
}
else
{ // Si hay colisión lo coloca donde colisiona
x = pos;
{ // Si hay colisión lo mueve hasta donde no colisiona
x = pos + 1;
}
}
@@ -264,7 +270,7 @@ void Player::move()
SDL_Rect proj;
proj.x = (int)x + w;
proj.y = (int)y;
proj.h = h - 1;
proj.h = h;
proj.w = (int)(vx);
// Comprueba la colisión
@@ -276,7 +282,7 @@ void Player::move()
x += vx;
}
else
{ // Si hay colisión lo coloca donde colisiona
{ // Si hay colisión lo mueve hasta donde no colisiona
x = pos - w;
}
}
@@ -295,7 +301,13 @@ void Player::move()
proj.x = (int)x;
proj.y = (int)y;
proj.h = (int)vy;
proj.w = w - 1;
proj.w = w;
// **new
proj.x = (int)x;
proj.y = (int)(y+vy);
proj.h = (int)abs(vy);
proj.w = w;
// Comprueba la colisión
const int pos = room->checkBottomSurfaces(&proj);
@@ -306,8 +318,8 @@ void Player::move()
y += vy;
}
else
{ // Si hay colisión lo coloca donde colisiona y entra en caída
y = pos;
{ // Si hay colisión lo mueve hasta donde no colisiona y entra en caída
y = pos + 1;
setState(s_falling);
}
}
@@ -320,7 +332,7 @@ void Player::move()
proj.x = (int)x;
proj.y = (int)y + h;
proj.h = (int)vy;
proj.w = w - 1;
proj.w = w;
// Comprueba la colisión
const int pos = room->checkTopSurfaces(&proj);
@@ -331,7 +343,7 @@ void Player::move()
y += vy;
}
else
{ // Si hay colisión lo coloca donde colisiona y pasa a estar sobre el suelo
{ // Si hay colisión lo mueve hasta donde no colisiona y pasa a estar sobre el suelo
y = pos - h;
setState(s_standing);
}

View File

@@ -444,7 +444,7 @@ void Room::fillMapTexture()
{
clip.x = x * 8;
clip.y = y * 8;
SDL_SetRenderDrawColor(renderer, 48, 48, 48, 192);
SDL_SetRenderDrawColor(renderer, 48, 48, 48, 224);
SDL_RenderFillRect(renderer, &clip);
}
}
@@ -453,41 +453,45 @@ void Room::fillMapTexture()
// ****
if (debug->getEnabled())
{
// BottomSurfaces
if (true)
{
for (auto l : bottomSurfaces)
{
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 80, (rand() % 128) + 80, (rand() % 128) + 80, 0xFF);
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
}
}
// TopSurfaces
if (true)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
for (auto l : topSurfaces)
{
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 80, (rand() % 128) + 80, (rand() % 128) + 80, 0xFF);
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
}
}
// LeftSurfaces
if (true)
{
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
for (auto l : leftSurfaces)
{
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 80, (rand() % 128) + 80, (rand() % 128) + 80, 0xFF);
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
}
}
// RightSurfaces
if (true)
{
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (auto l : rightSurfaces)
{
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 80, (rand() % 128) + 80, (rand() % 128) + 80, 0xFF);
SDL_SetRenderDrawColor(renderer, (rand() % 128) + 96, (rand() % 128) + 96, (rand() % 128) + 96, 0xFF);
SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
}
}
@@ -864,7 +868,7 @@ void Room::setRightSurfaces()
while (i < tile.size())
{
v_line_t line;
line.x = (tile[i] % mapWidth) * tileSize + tileSize;
line.x = (tile[i] % mapWidth) * tileSize + tileSize - 1;
line.y1 = ((tile[i] / mapWidth) * tileSize);
while (tile[i] + mapWidth == tile[i + 1])
{
@@ -879,88 +883,69 @@ void Room::setRightSurfaces()
// Comprueba las colisiones
int Room::checkRightSurfaces(SDL_Rect *rect)
{
bool collision = false;
int pos = -1;
for (auto s : rightSurfaces)
{
collision = checkCollision(s, *rect);
if (collision)
if (checkCollision(s, *rect))
{
pos = s.x;
break;
return s.x;
}
}
return pos;
return -1;
}
// Comprueba las colisiones
int Room::checkLeftSurfaces(SDL_Rect *rect)
{
bool collision = false;
int pos = -1;
for (auto s : leftSurfaces)
{
collision = checkCollision(s, *rect);
if (collision)
if (checkCollision(s, *rect))
{
pos = s.x;
break;
return s.x;
}
}
return pos;
return -1;
}
// Comprueba las colisiones
int Room::checkTopSurfaces(SDL_Rect *rect)
{
bool collision = false;
int pos = -1;
for (auto s : topSurfaces)
{
collision = checkCollision(s, *rect);
if (collision)
if (checkCollision(s, *rect))
{
pos = s.y;
break;
return s.y;
}
}
return pos;
return -1;
}
// Comprueba las colisiones
int Room::checkBottomSurfaces(SDL_Rect *rect)
{
bool collision = false;
int pos = -1;
for (auto s : bottomSurfaces)
{
collision = checkCollision(s, *rect);
if (collision)
if (checkCollision(s, *rect))
{
pos = s.y;
break;
return s.y;
}
}
return pos;
return -1;
}
// Comprueba las colisiones
bool Room::checkTopSurfaces(SDL_Point *p)
{
bool collision = false;
for (auto s : topSurfaces)
{
collision |= checkCollision(s, *p);
if (checkCollision(s, *p))
{
return true;
}
}
return collision;
return false;
}

View File

@@ -74,19 +74,19 @@ bool checkCollision(circle_t &a, SDL_Rect &b)
// Detector de colisiones entre dos rectangulos
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
{
// Calculate the sides of rect A
// Calcula las caras del rectangulo a
const int leftA = a.x;
const int rightA = a.x + a.w;
const int topA = a.y;
const int bottomA = a.y + a.h;
// Calculate the sides of rect B
// Calcula las caras del rectangulo b
const int leftB = b.x;
const int rightB = b.x + b.w;
const int topB = b.y;
const int bottomB = b.y + b.h;
// If any of the sides from A are outside of B
// Si cualquiera de las caras de a está fuera de b
if (bottomA <= topB)
{
return false;
@@ -107,36 +107,38 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
return false;
}
// If none of the sides from A are outside B
// Si ninguna de las caras está fuera de b
return true;
}
// Detector de colisiones entre un punto y u rectangulo
// Detector de colisiones entre un punto y un rectangulo
bool checkCollision(SDL_Point &p, SDL_Rect &r)
{
// Comprueba si el punto está fuera del rectangulo en el eje X
// Comprueba si el punto está a la izquierda del rectangulo
if (p.x < r.x)
{
return false;
}
// Comprueba si el punto está a la derecha del rectangulo
if (p.x > r.x + r.w)
{
return false;
}
// Comprueba si el punto está fuera del rectangulo en el eje Y
// Comprueba si el punto está por encima del rectangulo
if (p.y < r.y)
{
return false;
}
// Comprueba si el punto está por debajo del rectangulo
if (p.y > r.y + r.h)
{
return false;
}
// Si ha llegado hasta aquí, es que está dentro
// Si no está fuera, es que está dentro
return true;
}
@@ -156,7 +158,7 @@ bool checkCollision(h_line_t &l, SDL_Rect &r)
}
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
if (l.x1 > r.x + r.w)
if (l.x1 >= r.x + r.w)
{
return false;
}