arreglos en updateColliderPoints()
This commit is contained in:
@@ -339,16 +339,49 @@ void Player::setColor(Uint8 color) {
|
||||
}
|
||||
|
||||
// Actualiza los puntos de colisión
|
||||
// Sistema dinámico que adapta el número de puntos al tamaño del sprite
|
||||
// Para sprites más grandes que los tiles (8px), genera puntos adicionales
|
||||
// evitando que se atraviesen tiles al haber huecos entre colliders
|
||||
void Player::updateColliderPoints() {
|
||||
const SDL_FRect RECT = getRect();
|
||||
collider_points_[0] = {.x = RECT.x, .y = RECT.y};
|
||||
collider_points_[1] = {.x = RECT.x + 7, .y = RECT.y};
|
||||
collider_points_[2] = {.x = RECT.x + 7, .y = RECT.y + 7};
|
||||
collider_points_[3] = {.x = RECT.x, .y = RECT.y + 7};
|
||||
collider_points_[4] = {.x = RECT.x, .y = RECT.y + 8};
|
||||
collider_points_[5] = {.x = RECT.x + 7, .y = RECT.y + 8};
|
||||
collider_points_[6] = {.x = RECT.x + 7, .y = RECT.y + 15};
|
||||
collider_points_[7] = {.x = RECT.x, .y = RECT.y + 15};
|
||||
|
||||
// Calcular cuántos puntos necesitamos por cada dimensión
|
||||
// Regla: un punto cada ~7px (menos que Tile::SIZE=8px) para garantizar detección
|
||||
// Como mínimo 2 puntos por borde (esquinas)
|
||||
constexpr int MAX_SPACING = 7; // Máxima distancia entre puntos (menor que tile size de 8px)
|
||||
|
||||
const int points_per_width = std::max(2, (WIDTH / MAX_SPACING) + 1);
|
||||
const int points_per_height = std::max(2, (HEIGHT / MAX_SPACING) + 1);
|
||||
|
||||
// Limpiar y reservar espacio para los nuevos puntos
|
||||
collider_points_.clear();
|
||||
collider_points_.reserve(2 * points_per_width + 2 * points_per_height - 4); // Perímetro sin duplicar esquinas
|
||||
|
||||
// Generar puntos distribuidos uniformemente por el perímetro del rectángulo
|
||||
|
||||
// Borde superior (izquierda a derecha)
|
||||
for (int i = 0; i < points_per_width; ++i) {
|
||||
const float offset = (WIDTH - 1) * static_cast<float>(i) / static_cast<float>(points_per_width - 1);
|
||||
collider_points_.push_back({.x = RECT.x + offset, .y = RECT.y});
|
||||
}
|
||||
|
||||
// Borde derecho (arriba a abajo, sin repetir esquina superior derecha)
|
||||
for (int i = 1; i < points_per_height; ++i) {
|
||||
const float offset = (HEIGHT - 1) * static_cast<float>(i) / static_cast<float>(points_per_height - 1);
|
||||
collider_points_.push_back({.x = RECT.x + WIDTH - 1, .y = RECT.y + offset});
|
||||
}
|
||||
|
||||
// Borde inferior (derecha a izquierda, sin repetir esquina inferior derecha)
|
||||
for (int i = 1; i < points_per_width; ++i) {
|
||||
const float offset = (WIDTH - 1) * static_cast<float>(points_per_width - 1 - i) / static_cast<float>(points_per_width - 1);
|
||||
collider_points_.push_back({.x = RECT.x + offset, .y = RECT.y + HEIGHT - 1});
|
||||
}
|
||||
|
||||
// Borde izquierdo (abajo a arriba, sin repetir esquinas)
|
||||
for (int i = 1; i < points_per_height - 1; ++i) {
|
||||
const float offset = (HEIGHT - 1) * static_cast<float>(points_per_height - 1 - i) / static_cast<float>(points_per_height - 1);
|
||||
collider_points_.push_back({.x = RECT.x, .y = RECT.y + offset});
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza los puntos de los pies
|
||||
|
||||
Reference in New Issue
Block a user