diff --git a/source/boids_mgr/boid_manager.cpp b/source/boids_mgr/boid_manager.cpp index f70e469..01f172d 100644 --- a/source/boids_mgr/boid_manager.cpp +++ b/source/boids_mgr/boid_manager.cpp @@ -253,22 +253,30 @@ void BoidManager::applyCohesion(Ball* boid, float delta_time) { center_of_mass_x /= count; center_of_mass_y /= count; - // Dirección hacia el centro - float steer_x = (center_of_mass_x - center_x) * BOID_COHESION_WEIGHT * delta_time; - float steer_y = (center_of_mass_y - center_y) * BOID_COHESION_WEIGHT * delta_time; + // FASE 1.4: Normalizar dirección hacia el centro (CRÍTICO - antes no estaba normalizado!) + float dx_to_center = center_of_mass_x - center_x; + float dy_to_center = center_of_mass_y - center_y; + float distance_to_center = std::sqrt(dx_to_center * dx_to_center + dy_to_center * dy_to_center); - // Limitar fuerza máxima de steering - float steer_mag = std::sqrt(steer_x * steer_x + steer_y * steer_y); - if (steer_mag > BOID_MAX_FORCE) { - steer_x = (steer_x / steer_mag) * BOID_MAX_FORCE; - steer_y = (steer_y / steer_mag) * BOID_MAX_FORCE; + // Solo aplicar si hay distancia al centro (evitar división por cero) + if (distance_to_center > 0.1f) { + // Normalizar vector dirección (fuerza independiente de distancia) + float steer_x = (dx_to_center / distance_to_center) * BOID_COHESION_WEIGHT * delta_time; + float steer_y = (dy_to_center / distance_to_center) * BOID_COHESION_WEIGHT * delta_time; + + // Limitar fuerza máxima de steering + float steer_mag = std::sqrt(steer_x * steer_x + steer_y * steer_y); + if (steer_mag > BOID_MAX_FORCE) { + steer_x = (steer_x / steer_mag) * BOID_MAX_FORCE; + steer_y = (steer_y / steer_mag) * BOID_MAX_FORCE; + } + + float vx, vy; + boid->getVelocity(vx, vy); + vx += steer_x; + vy += steer_y; + boid->setVelocity(vx, vy); } - - float vx, vy; - boid->getVelocity(vx, vy); - vx += steer_x; - vy += steer_y; - boid->setVelocity(vx, vy); } } diff --git a/source/defines.h b/source/defines.h index 0360b71..5c0e7e7 100644 --- a/source/defines.h +++ b/source/defines.h @@ -289,16 +289,16 @@ constexpr float LOGO_FLIP_TRIGGER_MAX = 0.80f; // 80% máximo de progres constexpr int LOGO_FLIP_WAIT_PROBABILITY = 50; // 50% probabilidad de elegir el camino "esperar flip" // Configuración de Modo BOIDS (comportamiento de enjambre) -// FASE 1.1: Parámetros rebalanceados para evitar clustering -constexpr float BOID_SEPARATION_RADIUS = 25.0f; // Radio para evitar colisiones (píxeles) -constexpr float BOID_ALIGNMENT_RADIUS = 40.0f; // Radio para alinear velocidad con vecinos -constexpr float BOID_COHESION_RADIUS = 60.0f; // Radio para moverse hacia centro del grupo (REDUCIDO) -constexpr float BOID_SEPARATION_WEIGHT = 3.0f; // Peso de separación (TRIPLICADO - alta prioridad) -constexpr float BOID_ALIGNMENT_WEIGHT = 1.0f; // Peso de alineación (sin cambios) -constexpr float BOID_COHESION_WEIGHT = 0.5f; // Peso de cohesión (REDUCIDO a la mitad) -constexpr float BOID_MAX_SPEED = 3.0f; // Velocidad máxima (píxeles/frame) -constexpr float BOID_MAX_FORCE = 0.5f; // Fuerza máxima de steering (QUINTUPLICADO) -constexpr float BOID_MIN_SPEED = 0.5f; // Velocidad mínima (NUEVO - evita boids estáticos) +// FASE 1.1 REVISADA: Parámetros ajustados tras detectar cohesión mal normalizada +constexpr float BOID_SEPARATION_RADIUS = 30.0f; // Radio para evitar colisiones (píxeles) +constexpr float BOID_ALIGNMENT_RADIUS = 50.0f; // Radio para alinear velocidad con vecinos +constexpr float BOID_COHESION_RADIUS = 80.0f; // Radio para moverse hacia centro del grupo +constexpr float BOID_SEPARATION_WEIGHT = 1.5f; // Peso de separación +constexpr float BOID_ALIGNMENT_WEIGHT = 1.0f; // Peso de alineación +constexpr float BOID_COHESION_WEIGHT = 0.001f; // Peso de cohesión (MICRO - 1000x menor por falta de normalización) +constexpr float BOID_MAX_SPEED = 2.5f; // Velocidad máxima (píxeles/frame - REDUCIDA) +constexpr float BOID_MAX_FORCE = 0.05f; // Fuerza máxima de steering (REDUCIDA para evitar aceleración excesiva) +constexpr float BOID_MIN_SPEED = 0.3f; // Velocidad mínima (evita boids estáticos) constexpr float PI = 3.14159265358979323846f; // Constante PI