Corregido un bug con las colisiones entre un punto y una linea diagonal

This commit is contained in:
2022-09-14 21:18:50 +02:00
parent 2ff12f7db1
commit d5e5d142a4
10 changed files with 97 additions and 25 deletions

View File

@@ -9,14 +9,14 @@
26,0,0,0,0,0,86,0,0,0,0,0,0,0,0,0,0,183,0,0,0,0,0,0,0,0,0,0,0,0,0,26,
26,0,0,0,86,86,86,0,0,0,0,0,0,0,0,0,0,0,183,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,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,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,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,0,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,0,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,0,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,0,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,0,0,0,0,0,0,0,0,
26,0,0,0,86,0,86,0,0,0,0,0,0,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,0,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,0,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,0,0,0,0,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,0,0,0,0,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,163,26,26,26,26,26,26,26,26,183,0,0,0,0,0,0,22,22,0,0,0,26,
26,0,0,0,0,0,0,0,0,163,0,0,0,0,0,0,0,0,0,0,183,0,0,0,0,0,0,0,0,0,0,0,
26,0,0,0,0,0,0,0,163,0,0,0,0,0,0,0,0,0,0,0,0,183,0,0,0,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
</data>

View File

@@ -34,7 +34,7 @@ width=16
height=16
x=19
y=7
vx=0.6
vx=0
vy=0.3
x1=19
y1=3

View File

@@ -10,13 +10,13 @@
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,113,113,113,183,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,113,113,113,113,0,183,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,113,113,113,113,0,0,183,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,0,0,163,113,113,113,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,0,163,0,113,113,113,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,163,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
113,0,0,163,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,113,113,113,182,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,113,113,113,113,0,182,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,113,0,0,0,0,0,182,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,0,0,162,113,113,113,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,0,162,0,113,113,113,113,113,0,0,0,0,113,113,0,0,0,0,113,113,0,0,0,0,113,113,113,113,
113,0,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
113,0,0,162,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
113,113,113,113,101,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,103,113,113,113,113,113,
113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113
</data>

View File

@@ -45,6 +45,13 @@ void Debug::render()
x += w * text->getCharacterSize() + 2;
}
}
y = 0;
for (auto l : log)
{
text->writeColored(x, y, l, {255, 255, 255});
y += text->getCharacterSize() + 1;
}
}
// Establece la posición donde se colocará la información de debug
@@ -66,6 +73,18 @@ void Debug::clear()
slot.clear();
}
// Añade un texto para mostrar en el apartado log
void Debug::addToLog(std::string text)
{
log.push_back(text);
}
// Borra la información de debug del apartado log
void Debug::clearLog()
{
log.clear();
}
// Establece el valor de la variable
void Debug::setEnabled(bool value)
{

View File

@@ -21,6 +21,7 @@ private:
Asset *asset; // Objeto con los ficheros de recursos
Text *text; // Objeto encargado de escribir texto en pantalla
std::vector<std::string> slot; // Vector con los textos a escribir
std::vector<std::string> log; // Vector con los textos a escribir
int x; // Posicion donde escribir el texto de debug
int y; // Posición donde escribir el texto de debug
bool enabled; // Indica si esta activo el modo debug
@@ -47,6 +48,12 @@ public:
// Borra la información de debug
void clear();
// Añade un texto para mostrar en el apartado log
void addToLog(std::string text);
// Borra la información de debug del apartado log
void clearLog();
// Establece el valor de la variable
void setEnabled(bool value);

View File

@@ -17,7 +17,7 @@ Director::Director(std::string path)
}
else
{
section.name = SECTION_PROG_CREDITS;
section.name = SECTION_PROG_GAME;
section.subsection = 0;
}

View File

@@ -15,9 +15,15 @@ Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input, D
this->input = input;
this->debug = debug;
// this->debug->setEnabled(true);
// ****
this->debug->setEnabled(true);
currentRoom = "11.room";
spawnPoint = {2 * 8, 5 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// currentRoom = "06.room";
// spawnPoint = {240, 96, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// spawnPoint = {14 * 8, 9 * 8, 0, 0, 0, s_standing, SDL_FLIP_HORIZONTAL};
// ****
// Crea los objetos
scoreboard = new ScoreBoard(renderer, asset, &board);

View File

@@ -126,7 +126,6 @@ void Player::render()
// 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));
}
}
@@ -454,6 +453,8 @@ void Player::move()
// Actualiza la posición del sprite
sprite->setPosX(x);
sprite->setPosY(y);
debug->add("RECT: " + std::to_string(r.x) + "," + std::to_string(r.y) + "," + std::to_string(r.w) + "," + std::to_string(r.h));
}
// Establece la animación del jugador
@@ -506,6 +507,8 @@ void Player::playFallSound()
bool Player::isOnFloor()
{
bool onFloor = false;
bool onSlopeL = false;
bool onSlopeR = false;
updateFeet();
@@ -516,15 +519,25 @@ bool Player::isOnFloor()
}
// Comprueba las rampas
onFloor |= room->checkLeftSlopes(&underFeet[0]);
onFloor |= room->checkRightSlopes(&underFeet[1]);
onSlopeL = room->checkLeftSlopes(&underFeet[0]);
onSlopeR = room->checkRightSlopes(&underFeet[1]);
if (onFloor)
{
debug->add("ONFLOOR");
debug->add("ON_FLOOR");
}
return onFloor;
if (onSlopeL)
{
debug->add("ON_SLOPE_L: " + std::to_string(underFeet[0].x) + "," + std::to_string(underFeet[0].y));
}
if (onSlopeR)
{
debug->add("ON_SLOPE_R: " + std::to_string(underFeet[1].x) + "," + std::to_string(underFeet[1].y));
}
return onFloor || onSlopeL || onSlopeR;
}
// Comprueba si el jugador está sobre una rampa hacia abajo
@@ -545,7 +558,7 @@ bool Player::isOnDownSlope()
if (onSlope)
{
debug->add("ONSLOPE");
debug->add("ON_DOWN_SLOPE");
}
return onSlope;

View File

@@ -25,6 +25,8 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
texture = new LTexture(renderer, asset->get(tileset));
itemSound = JA_LoadSound(asset->get("item.wav").c_str());
debug->clearLog();
debug->addToLog(tileset);
// Calcula las superficies
setBottomSurfaces();
setTopSurfaces();
@@ -43,6 +45,7 @@ Room::Room(std::string file_path, SDL_Renderer *renderer, Screen *screen, Asset
// Establece el color del borde
screen->setBorderColor(borderColor);
}
// Destructor
@@ -937,6 +940,7 @@ void Room::setLeftSlopes()
line.x2 = ((lastOneFound % mapWidth) * tileSize) + tileSize - 1;
line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1;
leftSlopes.push_back(line);
debug->addToLog("LS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2));
}
}
@@ -978,6 +982,7 @@ void Room::setRightSlopes()
line.x2 = (lastOneFound % mapWidth) * tileSize;
line.y2 = ((lastOneFound / mapWidth) * tileSize) + tileSize - 1;
rightSlopes.push_back(line);
debug->addToLog("RS: " + std::to_string(line.x1) + "," + std::to_string(line.y1) + "," + std::to_string(line.x2) + "," + std::to_string(line.y2));
}
}

View File

@@ -353,8 +353,30 @@ bool checkCollision(SDL_Point &p, d_line_t &l)
return false;
}
// Comprueba si está por encima de la linea
if (p.y > l.y1 && p.y > l.y2)
{
return false;
}
// Comprueba si está por debajo de la linea
if (p.y < l.y1 && p.y < l.y2)
{
return false;
}
// En caso contrario, el punto está en la linea
return true;
/*const int m = (l.y2 - l.y1) / (l.x2 - l.x1);
const int c = 0;
// Comprueba si p cumple la ecuación de la linea
if (p.y == ((m * p.x) + c))
return true;
return false;*/
}
// Devuelve un color_t a partir de un string