eliminat sistema de colisió antic

This commit is contained in:
2026-04-06 23:05:22 +02:00
parent 6be93da929
commit 5414b0dfd4
9 changed files with 155 additions and 1157 deletions

View File

@@ -132,156 +132,6 @@ auto checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) -> bool {
return true;
}
// Detector de colisiones entre una linea horizontal y un rectangulo
auto checkCollision(const LineHorizontal& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por encima del rectangulo
if (l.y < r.y) {
return false;
}
// Comprueba si la linea esta por debajo del rectangulo
if (l.y >= r.y + r.h) {
return false;
}
// Comprueba si el inicio de la linea esta a la derecha del rectangulo
if (l.x1 >= r.x + r.w) {
return false;
}
// Comprueba si el final de la linea esta a la izquierda del rectangulo
if (l.x2 < r.x) {
return false;
}
// Si ha llegado hasta aquí, hay colisión
return true;
}
// Detector de colisiones entre una linea vertical y un rectangulo
auto checkCollision(const LineVertical& l, const SDL_FRect& rect) -> bool {
SDL_Rect r = toSDLRect(rect);
// Comprueba si la linea esta por la izquierda del rectangulo
if (l.x < r.x) {
return false;
}
// Comprueba si la linea esta por la derecha del rectangulo
if (l.x >= r.x + r.w) {
return false;
}
// Comprueba si el inicio de la linea esta debajo del rectangulo
if (l.y1 >= r.y + r.h) {
return false;
}
// Comprueba si el final de la linea esta encima del rectangulo
if (l.y2 < r.y) {
return false;
}
// Si ha llegado hasta aquí, hay colisión
return true;
}
// Detector de colisiones entre una linea horizontal y un punto
auto checkCollision(const LineHorizontal& l, const SDL_FPoint& point) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto esta sobre la linea
if (p.y > l.y) {
return false;
}
// Comprueba si el punto esta bajo la linea
if (p.y < l.y) {
return false;
}
// Comprueba si el punto esta a la izquierda de la linea
if (p.x < l.x1) {
return false;
}
// Comprueba si el punto esta a la derecha de la linea
if (p.x > l.x2) {
return false;
}
// Si ha llegado aquí, hay colisión
return true;
}
// Detector de colisiones entre dos lineas
auto checkCollision(const Line& l1, const Line& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
const float Y2 = l1.y2;
const float X3 = l2.x1;
const float Y3 = l2.y1;
const float X4 = l2.x2;
const float Y4 = l2.y2;
// calculate the direction of the lines
float u_a = (((X4 - X3) * (Y1 - Y3)) - ((Y4 - Y3) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1)));
float u_b = (((X2 - X1) * (Y1 - Y3)) - ((Y2 - Y1) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1)));
// if uA and uB are between 0-1, lines are colliding
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) {
// Calcula la intersección
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {.x = static_cast<int>(std::round(X)), .y = static_cast<int>(std::round(Y))};
}
return {.x = -1, .y = -1};
}
// Detector de colisiones entre dos lineas
auto checkCollision(const LineDiagonal& l1, const LineVertical& l2) -> SDL_Point {
const float X1 = l1.x1;
const float Y1 = l1.y1;
const float X2 = l1.x2;
const float Y2 = l1.y2;
const float X3 = l2.x;
const float Y3 = l2.y1;
const float X4 = l2.x;
const float Y4 = l2.y2;
// calculate the direction of the lines
float u_a = (((X4 - X3) * (Y1 - Y3)) - ((Y4 - Y3) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1)));
float u_b = (((X2 - X1) * (Y1 - Y3)) - ((Y2 - Y1) * (X1 - X3))) / (((Y4 - Y3) * (X2 - X1)) - ((X4 - X3) * (Y2 - Y1)));
// if uA and uB are between 0-1, lines are colliding
if (u_a >= 0 && u_a <= 1 && u_b >= 0 && u_b <= 1) {
// Calcula la intersección
const float X = X1 + (u_a * (X2 - X1));
const float Y = Y1 + (u_a * (Y2 - Y1));
return {.x = static_cast<int>(std::round(X)), .y = static_cast<int>(std::round(Y))};
}
return {.x = -1, .y = -1};
}
// Normaliza una linea diagonal
void normalizeLine(LineDiagonal& l) {
// Las lineas diagonales van de izquierda a derecha
// x2 mayor que x1
if (l.x2 < l.x1) {
const int X = l.x1;
const int Y = l.y1;
l.x1 = l.x2;
l.y1 = l.y2;
l.x2 = X;
l.y2 = Y;
}
}
// Convierte SDL_FRect a SDL_Rect
auto toSDLRect(const SDL_FRect& frect) -> SDL_Rect {
SDL_Rect rect = {
@@ -300,39 +150,6 @@ auto toSDLPoint(const SDL_FPoint& fpoint) -> SDL_Point {
return point;
}
// Detector de colisiones entre un punto y una linea diagonal
auto checkCollision(const SDL_FPoint& point, const LineDiagonal& l) -> bool {
SDL_Point p = toSDLPoint(point);
// Comprueba si el punto está en alineado con la linea
if (abs(p.x - l.x1) != abs(p.y - l.y1)) {
return false;
}
// Comprueba si está a la derecha de la linea
if (p.x > l.x1 && p.x > l.x2) {
return false;
}
// Comprueba si está a la izquierda de la linea
if (p.x < l.x1 && p.x < l.x2) {
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;
}
// Convierte una cadena a un entero de forma segura
auto safeStoi(const std::string& value, int default_value) -> int {
try {