fix: arreglades les colisions "the static cast menner"
This commit is contained in:
@@ -252,7 +252,7 @@ void Player::move() {
|
|||||||
|
|
||||||
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
||||||
if (state_ != PlayerState::JUMPING) {
|
if (state_ != PlayerState::JUMPING) {
|
||||||
const LineVertical LEFT_SIDE = {x_, y_ + HEIGHT_ - 2, y_ + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
const LineVertical LEFT_SIDE = {static_cast<int>(x_), static_cast<int>(y_) + static_cast<int>(HEIGHT_) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT_) - 1}; // Comprueba solo los dos pixels de abajo
|
||||||
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
|
const int LY = room_->checkLeftSlopes(&LEFT_SIDE);
|
||||||
if (LY > -1) {
|
if (LY > -1) {
|
||||||
y_ = LY - HEIGHT_;
|
y_ = LY - HEIGHT_;
|
||||||
@@ -292,7 +292,7 @@ void Player::move() {
|
|||||||
|
|
||||||
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
// Si ha tocado alguna rampa mientras camina (sin saltar), asciende
|
||||||
if (state_ != PlayerState::JUMPING) {
|
if (state_ != PlayerState::JUMPING) {
|
||||||
const LineVertical RIGHT_SIDE = {x_ + WIDTH_ - 1, y_ + HEIGHT_ - 2, y_ + HEIGHT_ - 1}; // Comprueba solo los dos pixels de abajo
|
const LineVertical RIGHT_SIDE = {static_cast<int>(x_) + static_cast<int>(WIDTH_) - 1, static_cast<int>(y_) + static_cast<int>(HEIGHT_) - 2, static_cast<int>(y_) + static_cast<int>(HEIGHT_) - 1}; // Comprueba solo los dos pixels de abajo
|
||||||
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
|
const int RY = room_->checkRightSlopes(&RIGHT_SIDE);
|
||||||
if (RY > -1) {
|
if (RY > -1) {
|
||||||
y_ = RY - HEIGHT_;
|
y_ = RY - HEIGHT_;
|
||||||
@@ -350,8 +350,8 @@ void Player::move() {
|
|||||||
else if (vy_ > 0.0f) {
|
else if (vy_ > 0.0f) {
|
||||||
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
// Crea el rectangulo de proyección en el eje Y para ver si colisiona
|
||||||
SDL_FRect proj;
|
SDL_FRect proj;
|
||||||
proj.x = static_cast<int>(x_);
|
proj.x = x_;
|
||||||
proj.y = static_cast<int>(y_) + HEIGHT_;
|
proj.y = y_ + HEIGHT_;
|
||||||
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
|
proj.h = ceil(vy_); // Para evitar que tenga una altura de 0 pixels
|
||||||
proj.w = WIDTH_;
|
proj.w = WIDTH_;
|
||||||
|
|
||||||
@@ -371,8 +371,9 @@ void Player::move() {
|
|||||||
} else {
|
} else {
|
||||||
// Si no hay colisión con los muros, comprueba la colisión con las rampas
|
// Si no hay colisión con los muros, comprueba la colisión con las rampas
|
||||||
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
|
if (state_ != PlayerState::JUMPING) { // Las rampas no se miran si se está saltando
|
||||||
const LineVertical LEFT_SIDE = {proj.x, proj.y, proj.y + proj.h - 1};
|
auto rect = toSDLRect(proj);
|
||||||
const LineVertical RIGHT_SIDE = {proj.x + proj.w - 1, proj.y, proj.y + proj.h - 1};
|
const LineVertical LEFT_SIDE = {rect.x, rect.y, rect.y + rect.h - 1};
|
||||||
|
const LineVertical RIGHT_SIDE = {rect.x + rect.w - 1, rect.y, rect.y + rect.h - 1};
|
||||||
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
|
const float POINT = std::max(room_->checkRightSlopes(&RIGHT_SIDE), room_->checkLeftSlopes(&LEFT_SIDE));
|
||||||
if (POINT > -1) {
|
if (POINT > -1) {
|
||||||
// No está saltando y hay colisión con una rampa
|
// No está saltando y hay colisión con una rampa
|
||||||
|
|||||||
@@ -1043,7 +1043,7 @@ bool Room::checkAutoSurfaces(SDL_FPoint* p) {
|
|||||||
// Comprueba las colisiones
|
// Comprueba las colisiones
|
||||||
int Room::checkLeftSlopes(const LineVertical* line) {
|
int Room::checkLeftSlopes(const LineVertical* line) {
|
||||||
for (const auto& slope : left_slopes_) {
|
for (const auto& slope : left_slopes_) {
|
||||||
const SDL_FPoint p = checkCollision(slope, *line);
|
const auto p = checkCollision(slope, *line);
|
||||||
if (p.x != -1) {
|
if (p.x != -1) {
|
||||||
return p.y;
|
return p.y;
|
||||||
}
|
}
|
||||||
@@ -1066,7 +1066,7 @@ bool Room::checkLeftSlopes(SDL_FPoint* p) {
|
|||||||
// Comprueba las colisiones
|
// Comprueba las colisiones
|
||||||
int Room::checkRightSlopes(const LineVertical* line) {
|
int Room::checkRightSlopes(const LineVertical* line) {
|
||||||
for (const auto& slope : right_slopes_) {
|
for (const auto& slope : right_slopes_) {
|
||||||
const SDL_FPoint p = checkCollision(slope, *line);
|
const auto p = checkCollision(slope, *line);
|
||||||
if (p.x != -1) {
|
if (p.x != -1) {
|
||||||
return p.y;
|
return p.y;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ bool checkCollision(const Circle& a, const Circle& b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre un circulo y un rectangulo
|
// Detector de colisiones entre un circulo y un rectangulo
|
||||||
bool checkCollision(const Circle& a, const SDL_FRect& b) {
|
bool checkCollision(const Circle& a, const SDL_FRect& rect) {
|
||||||
|
SDL_Rect b = toSDLRect(rect);
|
||||||
// Closest point on collision box
|
// Closest point on collision box
|
||||||
int cX, cY;
|
int cX, cY;
|
||||||
|
|
||||||
@@ -72,7 +73,9 @@ bool checkCollision(const Circle& a, const SDL_FRect& b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre dos rectangulos
|
// Detector de colisiones entre dos rectangulos
|
||||||
bool checkCollision(const SDL_FRect& a, const SDL_FRect& b) {
|
bool checkCollision(const SDL_FRect& rect_a, const SDL_FRect& rect_b) {
|
||||||
|
SDL_Rect a = toSDLRect(rect_a);
|
||||||
|
SDL_Rect b = toSDLRect(rect_b);
|
||||||
// Calcula las caras del rectangulo 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;
|
||||||
@@ -107,7 +110,10 @@ bool checkCollision(const SDL_FRect& a, const SDL_FRect& b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre un punto y un rectangulo
|
// Detector de colisiones entre un punto y un rectangulo
|
||||||
bool checkCollision(const SDL_FPoint& p, const SDL_FRect& r) {
|
bool checkCollision(const SDL_FPoint& point, const SDL_FRect& rect) {
|
||||||
|
SDL_Rect r = toSDLRect(rect);
|
||||||
|
SDL_Point p = toSDLPoint(point);
|
||||||
|
|
||||||
// Comprueba si el punto está a la izquierda del rectangulo
|
// Comprueba si el punto está a la izquierda del rectangulo
|
||||||
if (p.x < r.x) {
|
if (p.x < r.x) {
|
||||||
return false;
|
return false;
|
||||||
@@ -133,7 +139,8 @@ bool checkCollision(const SDL_FPoint& p, const SDL_FRect& r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre una linea horizontal y un rectangulo
|
// Detector de colisiones entre una linea horizontal y un rectangulo
|
||||||
bool checkCollision(const LineHorizontal& l, const SDL_FRect& r) {
|
bool checkCollision(const LineHorizontal& l, const SDL_FRect& rect) {
|
||||||
|
SDL_Rect r = toSDLRect(rect);
|
||||||
// Comprueba si la linea esta por encima del rectangulo
|
// Comprueba si la linea esta por encima del rectangulo
|
||||||
if (l.y < r.y) {
|
if (l.y < r.y) {
|
||||||
return false;
|
return false;
|
||||||
@@ -159,7 +166,8 @@ bool checkCollision(const LineHorizontal& l, const SDL_FRect& r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre una linea vertical y un rectangulo
|
// Detector de colisiones entre una linea vertical y un rectangulo
|
||||||
bool checkCollision(const LineVertical& l, const SDL_FRect& r) {
|
bool checkCollision(const LineVertical& l, const SDL_FRect& rect) {
|
||||||
|
SDL_Rect r = toSDLRect(rect);
|
||||||
// Comprueba si la linea esta por la izquierda del rectangulo
|
// Comprueba si la linea esta por la izquierda del rectangulo
|
||||||
if (l.x < r.x) {
|
if (l.x < r.x) {
|
||||||
return false;
|
return false;
|
||||||
@@ -185,7 +193,9 @@ bool checkCollision(const LineVertical& l, const SDL_FRect& r) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre una linea horizontal y un punto
|
// Detector de colisiones entre una linea horizontal y un punto
|
||||||
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& p) {
|
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& point) {
|
||||||
|
SDL_Point p = toSDLPoint(point);
|
||||||
|
|
||||||
// Comprueba si el punto esta sobre la linea
|
// Comprueba si el punto esta sobre la linea
|
||||||
if (p.y > l.y) {
|
if (p.y > l.y) {
|
||||||
return false;
|
return false;
|
||||||
@@ -211,7 +221,7 @@ bool checkCollision(const LineHorizontal& l, const SDL_FPoint& p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre dos lineas
|
// Detector de colisiones entre dos lineas
|
||||||
SDL_FPoint checkCollision(const Line& l1, const Line& l2) {
|
SDL_Point checkCollision(const Line& l1, const Line& l2) {
|
||||||
const float x1 = l1.x1;
|
const float x1 = l1.x1;
|
||||||
const float y1 = l1.y1;
|
const float y1 = l1.y1;
|
||||||
const float x2 = l1.x2;
|
const float x2 = l1.x2;
|
||||||
@@ -232,13 +242,13 @@ SDL_FPoint checkCollision(const Line& l1, const Line& l2) {
|
|||||||
const float x = x1 + (uA * (x2 - x1));
|
const float x = x1 + (uA * (x2 - x1));
|
||||||
const float y = y1 + (uA * (y2 - y1));
|
const float y = y1 + (uA * (y2 - y1));
|
||||||
|
|
||||||
return {(float)round(x), (float)round(y)};
|
return {static_cast<int>(round(x)), static_cast<int>(round(y))};
|
||||||
}
|
}
|
||||||
return {-1, -1};
|
return {-1, -1};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre dos lineas
|
// Detector de colisiones entre dos lineas
|
||||||
SDL_FPoint checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
|
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
|
||||||
const float x1 = l1.x1;
|
const float x1 = l1.x1;
|
||||||
const float y1 = l1.y1;
|
const float y1 = l1.y1;
|
||||||
const float x2 = l1.x2;
|
const float x2 = l1.x2;
|
||||||
@@ -259,7 +269,7 @@ SDL_FPoint checkCollision(const LineDiagonal& l1, const LineVertical& l2) {
|
|||||||
const float x = x1 + (uA * (x2 - x1));
|
const float x = x1 + (uA * (x2 - x1));
|
||||||
const float y = y1 + (uA * (y2 - y1));
|
const float y = y1 + (uA * (y2 - y1));
|
||||||
|
|
||||||
return {(float)x, (float)y};
|
return {static_cast<int>(x), static_cast<int>(y)};
|
||||||
}
|
}
|
||||||
return {-1, -1};
|
return {-1, -1};
|
||||||
}
|
}
|
||||||
@@ -279,7 +289,9 @@ void normalizeLine(LineDiagonal& l) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Detector de colisiones entre un punto y una linea diagonal
|
// Detector de colisiones entre un punto y una linea diagonal
|
||||||
bool checkCollision(const SDL_FPoint& p, const LineDiagonal& l) {
|
bool checkCollision(const SDL_FPoint& point, const LineDiagonal& l) {
|
||||||
|
SDL_Point p = toSDLPoint(point);
|
||||||
|
|
||||||
// Comprueba si el punto está en alineado con la linea
|
// Comprueba si el punto está en alineado con la linea
|
||||||
if (abs(p.x - l.x1) != abs(p.y - l.y1)) {
|
if (abs(p.x - l.x1) != abs(p.y - l.y1)) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -42,22 +42,22 @@ struct Circle {
|
|||||||
|
|
||||||
// Estructura para definir una linea horizontal
|
// Estructura para definir una linea horizontal
|
||||||
struct LineHorizontal {
|
struct LineHorizontal {
|
||||||
float x1, x2, y;
|
int x1, x2, y;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para definir una linea vertical
|
// Estructura para definir una linea vertical
|
||||||
struct LineVertical {
|
struct LineVertical {
|
||||||
float x, y1, y2;
|
int x, y1, y2;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para definir una linea diagonal
|
// Estructura para definir una linea diagonal
|
||||||
struct LineDiagonal {
|
struct LineDiagonal {
|
||||||
float x1, y1, x2, y2;
|
int x1, y1, x2, y2;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para definir una linea
|
// Estructura para definir una linea
|
||||||
struct Line {
|
struct Line {
|
||||||
float x1, y1, x2, y2;
|
int x1, y1, x2, y2;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Estructura para definir un color
|
// Estructura para definir un color
|
||||||
@@ -104,10 +104,10 @@ bool checkCollision(const LineVertical& l, const SDL_FRect& r);
|
|||||||
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& p);
|
bool checkCollision(const LineHorizontal& l, const SDL_FPoint& p);
|
||||||
|
|
||||||
// Detector de colisiones entre dos lineas
|
// Detector de colisiones entre dos lineas
|
||||||
SDL_FPoint checkCollision(const Line& l1, const Line& l2);
|
SDL_Point checkCollision(const Line& l1, const Line& l2);
|
||||||
|
|
||||||
// Detector de colisiones entre dos lineas
|
// Detector de colisiones entre dos lineas
|
||||||
SDL_FPoint checkCollision(const LineDiagonal& l1, const LineVertical& l2);
|
SDL_Point checkCollision(const LineDiagonal& l1, const LineVertical& l2);
|
||||||
|
|
||||||
// Detector de colisiones entre un punto y una linea diagonal
|
// Detector de colisiones entre un punto y una linea diagonal
|
||||||
bool checkCollision(const SDL_FPoint& p, const LineDiagonal& l);
|
bool checkCollision(const SDL_FPoint& p, const LineDiagonal& l);
|
||||||
@@ -152,4 +152,20 @@ bool stringInVector(const std::vector<std::string>& vec, const std::string& str)
|
|||||||
void playMusic(const std::string& music_path);
|
void playMusic(const std::string& music_path);
|
||||||
|
|
||||||
// Rellena una textura de un color
|
// Rellena una textura de un color
|
||||||
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
|
void fillTextureWithColor(SDL_Renderer* renderer, SDL_Texture* texture, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
|
||||||
|
|
||||||
|
inline SDL_Rect toSDLRect(const SDL_FRect& frect) {
|
||||||
|
SDL_Rect rect;
|
||||||
|
rect.x = static_cast<int>(frect.x);
|
||||||
|
rect.y = static_cast<int>(frect.y);
|
||||||
|
rect.w = static_cast<int>(frect.w);
|
||||||
|
rect.h = static_cast<int>(frect.h);
|
||||||
|
return rect;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline SDL_Point toSDLPoint(const SDL_FPoint& fpoint) {
|
||||||
|
SDL_Point point;
|
||||||
|
point.x = static_cast<int>(fpoint.x);
|
||||||
|
point.y = static_cast<int>(fpoint.y);
|
||||||
|
return point;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user