325 lines
8.7 KiB
C++
325 lines
8.7 KiB
C++
#include "utils/utils.h"
|
|
|
|
#include <algorithm> // for transform
|
|
#include <cctype> // for tolower
|
|
#include <cmath> // for round, abs
|
|
#include <cstdlib> // for abs
|
|
|
|
// Calcula el cuadrado de la distancia entre dos puntos
|
|
auto distanceSquared(int x1, int y1, int x2, int y2) -> double {
|
|
const int DELTA_X = x2 - x1;
|
|
const int DELTA_Y = y2 - y1;
|
|
return (DELTA_X * DELTA_X) + (DELTA_Y * DELTA_Y);
|
|
}
|
|
|
|
// Detector de colisiones entre dos circulos
|
|
auto checkCollision(const Circle &a, const Circle &b) -> bool {
|
|
// Calcula el radio total al cuadrado
|
|
int total_radius_squared = a.r + b.r;
|
|
total_radius_squared = total_radius_squared * total_radius_squared;
|
|
|
|
// Han colisionat si la distancia entre centres és inferior a la suma de radis
|
|
return distanceSquared(a.x, a.y, b.x, b.y) < total_radius_squared;
|
|
}
|
|
|
|
// Detector de colisiones entre un circulo y un rectangulo
|
|
auto checkCollision(const Circle &a, const SDL_Rect &b) -> bool {
|
|
// Closest point on collision box
|
|
int c_x;
|
|
int c_y;
|
|
|
|
// Find closest x offset
|
|
if (a.x < b.x) {
|
|
c_x = b.x;
|
|
} else if (a.x > b.x + b.w) {
|
|
c_x = b.x + b.w;
|
|
} else {
|
|
c_x = a.x;
|
|
}
|
|
|
|
// Find closest y offset
|
|
if (a.y < b.y) {
|
|
c_y = b.y;
|
|
} else if (a.y > b.y + b.h) {
|
|
c_y = b.y + b.h;
|
|
} else {
|
|
c_y = a.y;
|
|
}
|
|
|
|
// If the closest point is inside the Circle
|
|
if (distanceSquared(a.x, a.y, c_x, c_y) < a.r * a.r) {
|
|
// This box and the Circle have collided
|
|
return true;
|
|
}
|
|
|
|
// If the shapes have not collided
|
|
return false;
|
|
}
|
|
|
|
// Detector de colisiones entre dos rectangulos
|
|
auto checkCollision(const SDL_Rect &a, const SDL_Rect &b) -> bool {
|
|
// Calcula las caras del rectangulo a
|
|
const int LEFT_A = a.x;
|
|
const int RIGHT_A = a.x + a.w;
|
|
const int TOP_A = a.y;
|
|
const int BOTTOM_A = a.y + a.h;
|
|
|
|
// Calcula las caras del rectangulo b
|
|
const int LEFT_B = b.x;
|
|
const int RIGHT_B = b.x + b.w;
|
|
const int TOP_B = b.y;
|
|
const int BOTTOM_B = b.y + b.h;
|
|
|
|
// Si cualquiera de las caras de a está fuera de b
|
|
if (BOTTOM_A <= TOP_B) {
|
|
return false;
|
|
}
|
|
|
|
if (TOP_A >= BOTTOM_B) {
|
|
return false;
|
|
}
|
|
|
|
if (RIGHT_A <= LEFT_B) {
|
|
return false;
|
|
}
|
|
|
|
if (LEFT_A >= RIGHT_B) {
|
|
return false;
|
|
}
|
|
|
|
// Si ninguna de las caras está fuera de b
|
|
return true;
|
|
}
|
|
|
|
// Detector de colisiones entre un punto y un rectangulo
|
|
auto checkCollision(const SDL_Point &p, const SDL_Rect &r) -> bool {
|
|
// 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á 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 no está fuera, es que está dentro
|
|
return true;
|
|
}
|
|
|
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
|
auto checkCollision(const HorizontalLine &l, const SDL_Rect &r) -> bool {
|
|
// 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 VerticalLine &l, const SDL_Rect &r) -> bool {
|
|
// 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 HorizontalLine &l, const SDL_Point &p) -> bool {
|
|
// 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 {(int)std::round(X), (int)std::round(Y)};
|
|
}
|
|
return {-1, -1};
|
|
}
|
|
|
|
// Detector de colisiones entre dos lineas
|
|
auto checkCollision(const DiagonalLine &l1, const VerticalLine &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 {(int)X, (int)Y};
|
|
}
|
|
return {-1, -1};
|
|
}
|
|
|
|
// Normaliza una linea diagonal
|
|
void normalizeLine(DiagonalLine &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;
|
|
}
|
|
}
|
|
|
|
// Detector de colisiones entre un punto y una linea diagonal
|
|
auto checkCollision(const SDL_Point &p, const DiagonalLine &l) -> bool {
|
|
// 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;
|
|
|
|
/*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;*/
|
|
}
|
|
|
|
// Convierte una cadena en un valor booleano
|
|
auto stringToBool(const std::string &str) -> bool {
|
|
return str == "true";
|
|
}
|
|
|
|
// Convierte un valor booleano en una cadena
|
|
auto boolToString(bool value) -> std::string {
|
|
if (value) {
|
|
return "true";
|
|
}
|
|
return "false";
|
|
}
|
|
|
|
// Convierte una cadena a minusculas
|
|
auto toLower(const std::string &str) -> std::string {
|
|
std::string result;
|
|
result.reserve(str.size());
|
|
std::ranges::transform(str, std::back_inserter(result), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
|
|
return result;
|
|
} |