added utils.cpp
This commit is contained in:
112
source/utils.cpp
Normal file
112
source/utils.cpp
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
|
double distanceSquared(int x1, int y1, int x2, int y2)
|
||||||
|
{
|
||||||
|
int deltaX = x2 - x1;
|
||||||
|
int deltaY = y2 - y1;
|
||||||
|
return deltaX * deltaX + deltaY * deltaY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos circulos
|
||||||
|
bool checkCollision(Circle &a, Circle &b)
|
||||||
|
{
|
||||||
|
// Calcula el radio total al cuadrado
|
||||||
|
int totalRadiusSquared = a.r + b.r;
|
||||||
|
totalRadiusSquared = totalRadiusSquared * totalRadiusSquared;
|
||||||
|
|
||||||
|
// Si la distancia entre el centro de los circulos es inferior a la suma de sus radios
|
||||||
|
if (distanceSquared(a.x, a.y, b.x, b.y) < (totalRadiusSquared))
|
||||||
|
{
|
||||||
|
// Los circulos han colisionado
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// En caso contrario
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detector de colisiones entre un circulo y un rectangulo
|
||||||
|
bool checkCollision(Circle &a, SDL_Rect &b)
|
||||||
|
{
|
||||||
|
//Closest point on collision box
|
||||||
|
int cX, cY;
|
||||||
|
|
||||||
|
//Find closest x offset
|
||||||
|
if (a.x < b.x)
|
||||||
|
{
|
||||||
|
cX = b.x;
|
||||||
|
}
|
||||||
|
else if (a.x > b.x + b.w)
|
||||||
|
{
|
||||||
|
cX = b.x + b.w;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cX = a.x;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Find closest y offset
|
||||||
|
if (a.y < b.y)
|
||||||
|
{
|
||||||
|
cY = b.y;
|
||||||
|
}
|
||||||
|
else if (a.y > b.y + b.h)
|
||||||
|
{
|
||||||
|
cY = b.y + b.h;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
cY = a.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
//If the closest point is inside the circle
|
||||||
|
if (distanceSquared(a.x, a.y, cX, cY) < 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 un dos rectangulos
|
||||||
|
bool checkCollision(SDL_Rect &a, SDL_Rect &b)
|
||||||
|
{
|
||||||
|
//Calculate the sides of rect A
|
||||||
|
int leftA = a.x;
|
||||||
|
int rightA = a.x + a.w;
|
||||||
|
int topA = a.y;
|
||||||
|
int bottomA = a.y + a.h;
|
||||||
|
|
||||||
|
//Calculate the sides of rect B
|
||||||
|
int leftB = b.x;
|
||||||
|
int rightB = b.x + b.w;
|
||||||
|
int topB = b.y;
|
||||||
|
int bottomB = b.y + b.h;
|
||||||
|
|
||||||
|
//If any of the sides from A are outside of B
|
||||||
|
if (bottomA <= topB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (topA >= bottomB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rightA <= leftB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (leftA >= rightB)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
//If none of the sides from A are outside B
|
||||||
|
return true;
|
||||||
|
}
|
||||||
54
source/utils.h
Normal file
54
source/utils.h
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "ifdefs.h"
|
||||||
|
|
||||||
|
#ifndef UTILS_H
|
||||||
|
#define UTILS_H
|
||||||
|
|
||||||
|
// Estructura para definir un circulo
|
||||||
|
struct Circle
|
||||||
|
{
|
||||||
|
Uint16 x;
|
||||||
|
Uint16 y;
|
||||||
|
Uint8 r;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para definir todas las entradas que aceptará el programa
|
||||||
|
struct Input
|
||||||
|
{
|
||||||
|
Uint8 up;
|
||||||
|
Uint8 down;
|
||||||
|
Uint8 left;
|
||||||
|
Uint8 right;
|
||||||
|
Uint8 escape;
|
||||||
|
Uint8 pause;
|
||||||
|
Uint8 fire;
|
||||||
|
Uint8 fireLeft;
|
||||||
|
Uint8 fireRight;
|
||||||
|
Uint8 accept;
|
||||||
|
Uint8 cancel;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Estructura para mapear el teclado usado en la demo
|
||||||
|
struct DemoKeys
|
||||||
|
{
|
||||||
|
Uint8 left;
|
||||||
|
Uint8 right;
|
||||||
|
Uint8 noInput;
|
||||||
|
Uint8 fire;
|
||||||
|
Uint8 fireLeft;
|
||||||
|
Uint8 fireRight;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Calcula el cuadrado de la distancia entre dos puntos
|
||||||
|
double distanceSquared(int x1, int y1, int x2, int y2);
|
||||||
|
|
||||||
|
// Detector de colisiones entre dos circulos
|
||||||
|
bool checkCollision(Circle &a, Circle &b);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un circulo y un rectangulo
|
||||||
|
bool checkCollision(Circle &a, SDL_Rect &b);
|
||||||
|
|
||||||
|
// Detector de colisiones entre un dos rectangulos
|
||||||
|
bool checkCollision(SDL_Rect a, SDL_Rect b);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user