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

@@ -11,10 +11,10 @@
26,0,0,0,86,0,86,0,0,0,0,0,0,0,0,167,66,66,66,66,187,0,0,0,0,164,65,65,0,0,0,26, 26,0,0,0,86,0,86,0,0,0,0,0,0,0,0,167,66,66,66,66,187,0,0,0,0,164,65,65,0,0,0,26,
26,0,0,0,86,0,86,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26, 26,0,0,0,86,0,86,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,
26,0,0,0,0,0,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26, 26,0,0,0,0,0,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26,
26,0,0,0,0,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,26, 26,0,0,0,0,0,0,0,0,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,62,62,0,0,0,0,26,
26,0,0,0,0,0,0,0,0,161,212,212,212,212,212,212,212,0,212,212,181,0,0,0,0,0,0,0,0,0,0,26, 26,0,0,0,0,0,0,0,0,161,212,212,212,212,212,212,212,0,212,212,181,0,0,0,0,62,62,0,0,0,0,26,
26,0,0,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,0,0,181,0,0,0,0,0,0,0,0,0,26, 26,0,0,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,0,0,181,0,0,0,0,0,0,0,0,0,26,
26,0,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,269,0,0,0,0,181,0,0,0,0,0,0,0,0,26, 26,0,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,269,0,0,0,0,181,0,0,0,22,22,0,0,0,26,
26,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,269,0,0,0,0,0,181,0,0,0,0,0,0,0,0, 26,0,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,269,0,0,0,0,0,181,0,0,0,0,0,0,0,0,
26,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,81,289,81,0,0,0,0,0,181,0,0,0,0,0,0,0, 26,0,0,0,0,161,0,0,0,0,0,0,0,0,0,0,81,289,81,0,0,0,0,0,181,0,0,0,0,0,0,0,
26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26, 26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,26,

View File

@@ -211,7 +211,7 @@ void Game::renderDebugInfo()
SDL_RenderFillRect(renderer, &rect); SDL_RenderFillRect(renderer, &rect);
// Pinta la rejilla // 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) for (int i = 0; i < PLAY_AREA_BOTTOM; i += 8)
{ // Lineas horizontales { // Lineas horizontales
SDL_RenderDrawLine(renderer, 0, i, PLAY_AREA_RIGHT, i); SDL_RenderDrawLine(renderer, 0, i, PLAY_AREA_RIGHT, i);

View File

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

View File

@@ -444,7 +444,7 @@ void Room::fillMapTexture()
{ {
clip.x = x * 8; clip.x = x * 8;
clip.y = y * 8; clip.y = y * 8;
SDL_SetRenderDrawColor(renderer, 48, 48, 48, 192); SDL_SetRenderDrawColor(renderer, 48, 48, 48, 224);
SDL_RenderFillRect(renderer, &clip); SDL_RenderFillRect(renderer, &clip);
} }
} }
@@ -453,41 +453,45 @@ void Room::fillMapTexture()
// **** // ****
if (debug->getEnabled()) if (debug->getEnabled())
{ {
// BottomSurfaces
if (true) if (true)
{ {
for (auto l : bottomSurfaces) 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); SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
} }
} }
// TopSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF); SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
for (auto l : topSurfaces) 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); SDL_RenderDrawLine(renderer, l.x1, l.y, l.x2, l.y);
} }
} }
// LeftSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
for (auto l : leftSurfaces) 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); SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
} }
} }
// RightSurfaces
if (true) if (true)
{ {
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
for (auto l : rightSurfaces) 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); SDL_RenderDrawLine(renderer, l.x, l.y1, l.x, l.y2);
} }
} }
@@ -864,7 +868,7 @@ void Room::setRightSurfaces()
while (i < tile.size()) while (i < tile.size())
{ {
v_line_t line; 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); line.y1 = ((tile[i] / mapWidth) * tileSize);
while (tile[i] + mapWidth == tile[i + 1]) while (tile[i] + mapWidth == tile[i + 1])
{ {
@@ -879,88 +883,69 @@ void Room::setRightSurfaces()
// Comprueba las colisiones // Comprueba las colisiones
int Room::checkRightSurfaces(SDL_Rect *rect) int Room::checkRightSurfaces(SDL_Rect *rect)
{ {
bool collision = false;
int pos = -1;
for (auto s : rightSurfaces) for (auto s : rightSurfaces)
{ {
collision = checkCollision(s, *rect); if (checkCollision(s, *rect))
if (collision)
{ {
pos = s.x; return s.x;
break;
} }
} }
return pos; return -1;
} }
// Comprueba las colisiones // Comprueba las colisiones
int Room::checkLeftSurfaces(SDL_Rect *rect) int Room::checkLeftSurfaces(SDL_Rect *rect)
{ {
bool collision = false;
int pos = -1;
for (auto s : leftSurfaces) for (auto s : leftSurfaces)
{ {
collision = checkCollision(s, *rect); if (checkCollision(s, *rect))
if (collision)
{ {
pos = s.x; return s.x;
break;
} }
} }
return pos; return -1;
} }
// Comprueba las colisiones // Comprueba las colisiones
int Room::checkTopSurfaces(SDL_Rect *rect) int Room::checkTopSurfaces(SDL_Rect *rect)
{ {
bool collision = false;
int pos = -1;
for (auto s : topSurfaces) for (auto s : topSurfaces)
{ {
collision = checkCollision(s, *rect); if (checkCollision(s, *rect))
if (collision)
{ {
pos = s.y; return s.y;
break;
} }
} }
return pos; return -1;
} }
// Comprueba las colisiones // Comprueba las colisiones
int Room::checkBottomSurfaces(SDL_Rect *rect) int Room::checkBottomSurfaces(SDL_Rect *rect)
{ {
bool collision = false;
int pos = -1;
for (auto s : bottomSurfaces) for (auto s : bottomSurfaces)
{ {
collision = checkCollision(s, *rect); if (checkCollision(s, *rect))
if (collision)
{ {
pos = s.y; return s.y;
break;
} }
} }
return pos; return -1;
} }
// Comprueba las colisiones // Comprueba las colisiones
bool Room::checkTopSurfaces(SDL_Point *p) bool Room::checkTopSurfaces(SDL_Point *p)
{ {
bool collision = false;
for (auto s : topSurfaces) 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 // Detector de colisiones entre dos rectangulos
bool checkCollision(SDL_Rect &a, SDL_Rect &b) 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 leftA = a.x;
const int rightA = a.x + a.w; const int rightA = a.x + a.w;
const int topA = a.y; const int topA = a.y;
const int bottomA = a.y + a.h; 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 leftB = b.x;
const int rightB = b.x + b.w; const int rightB = b.x + b.w;
const int topB = b.y; const int topB = b.y;
const int bottomB = b.y + b.h; 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) if (bottomA <= topB)
{ {
return false; return false;
@@ -107,36 +107,38 @@ bool checkCollision(SDL_Rect &a, SDL_Rect &b)
return false; return false;
} }
// If none of the sides from A are outside B // Si ninguna de las caras está fuera de b
return true; 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) 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) if (p.x < r.x)
{ {
return false; return false;
} }
// Comprueba si el punto está a la derecha del rectangulo
if (p.x > r.x + r.w) if (p.x > r.x + r.w)
{ {
return false; 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) if (p.y < r.y)
{ {
return false; return false;
} }
// Comprueba si el punto está por debajo del rectangulo
if (p.y > r.y + r.h) if (p.y > r.y + r.h)
{ {
return false; return false;
} }
// Si ha llegado hasta aquí, es que está dentro // Si no está fuera, es que está dentro
return true; 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 // 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; return false;
} }