From 76165e4345f85ff1593e1dee4aca4d43fcfee4df Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Tue, 9 Dec 2025 13:38:18 +0100 Subject: [PATCH] limitada la velocitat angular dels debris i transformada en velocitat lineal tangencial --- source/core/defaults.hpp | 5 ++++ source/game/effects/debris_manager.cpp | 41 +++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/source/core/defaults.hpp b/source/core/defaults.hpp index cfe5e33..e3ca926 100644 --- a/source/core/defaults.hpp +++ b/source/core/defaults.hpp @@ -107,6 +107,11 @@ constexpr float SHRINK_RATE = 0.5f; // Reducció de mida (factor/s) constexpr float FACTOR_HERENCIA_MIN = 0.7f; // Mínimo 70% del drotacio heredat constexpr float FACTOR_HERENCIA_MAX = 1.0f; // Màxim 100% del drotacio heredat constexpr float FRICCIO_ANGULAR = 0.5f; // Desacceleració angular (rad/s²) + +// Angular velocity cap for trajectory inheritance +// Excess above this threshold is converted to tangential linear velocity +// Prevents "vortex trap" problem with high-rotation enemies +constexpr float VELOCITAT_ROT_MAX = 1.5f; // rad/s (~86°/s) } // namespace Debris } // namespace Physics diff --git a/source/game/effects/debris_manager.cpp b/source/game/effects/debris_manager.cpp index 146c37e..749fccf 100644 --- a/source/game/effects/debris_manager.cpp +++ b/source/game/effects/debris_manager.cpp @@ -112,20 +112,53 @@ void DebrisManager::explotar(const std::shared_ptr& shape, debris->velocitat.y = direccio.y * speed + velocitat_objecte.y; debris->acceleracio = Defaults::Physics::Debris::ACCELERACIO; - // 6. Herència de velocitat angular + // 6. Herència de velocitat angular amb cap + conversió d'excés - // 6a. Rotació de TRAYECTORIA (sempre hereda si velocitat_angular > 0) + // 6a. Rotació de TRAYECTORIA amb cap + conversió tangencial if (std::abs(velocitat_angular) > 0.01f) { + // FASE 1: Aplicar herència i variació (igual que abans) float factor_herencia = Defaults::Physics::Debris::FACTOR_HERENCIA_MIN + (std::rand() / static_cast(RAND_MAX)) * (Defaults::Physics::Debris::FACTOR_HERENCIA_MAX - Defaults::Physics::Debris::FACTOR_HERENCIA_MIN); - debris->velocitat_rot = velocitat_angular * factor_herencia; + + float velocitat_ang_heretada = velocitat_angular * factor_herencia; float variacio = (std::rand() / static_cast(RAND_MAX)) * 0.2f - 0.1f; - debris->velocitat_rot *= (1.0f + variacio); + velocitat_ang_heretada *= (1.0f + variacio); + + // FASE 2: Aplicar cap i calcular excés + constexpr float CAP = Defaults::Physics::Debris::VELOCITAT_ROT_MAX; + float abs_ang = std::abs(velocitat_ang_heretada); + float sign_ang = (velocitat_ang_heretada >= 0.0f) ? 1.0f : -1.0f; + + if (abs_ang > CAP) { + // Excés: convertir a velocitat tangencial + float excess = abs_ang - CAP; + + // Radi de la forma (enemics = 20 px) + float radius = 20.0f; + + // Velocitat tangencial = ω_excés × radi + float v_tangential = excess * radius; + + // Direcció tangencial: perpendicular a la radial (90° CCW) + // Si direccio = (dx, dy), tangent = (-dy, dx) + float tangent_x = -direccio.y; + float tangent_y = direccio.x; + + // Afegir velocitat tangencial (suma vectorial) + debris->velocitat.x += tangent_x * v_tangential; + debris->velocitat.y += tangent_y * v_tangential; + + // Aplicar cap a velocitat angular (preservar signe) + debris->velocitat_rot = sign_ang * CAP; + } else { + // Per sota del cap: comportament normal + debris->velocitat_rot = velocitat_ang_heretada; + } } else { debris->velocitat_rot = 0.0f; // Nave: sin curvas }