Implementar física de atracción con resorte en RotoBall

Reemplazar interpolación lineal artificial por sistema de fuerzas físicamente
realista usando Ley de Hooke con amortiguación variable.

## Cambios Principales

### Sistema de Física Implementado

**Fuerza de Resorte (Hooke's Law):**
```cpp
F_spring = k * (target - position)
F_damping = c * velocity
F_total = F_spring - F_damping
acceleration = F_total / mass
```

**Constantes (defines.h):**
- `ROTOBALL_SPRING_K = 300.0f`: Rigidez del resorte
- `ROTOBALL_DAMPING_BASE = 15.0f`: Amortiguación lejos del punto
- `ROTOBALL_DAMPING_NEAR = 50.0f`: Amortiguación cerca (estabilización)
- `ROTOBALL_NEAR_THRESHOLD = 5.0f`: Distancia considerada "cerca"
- `ROTOBALL_MAX_FORCE = 1000.0f`: Límite de seguridad

### Nuevas Funciones (Ball class)

- `enableRotoBallAttraction(bool)`: Activa/desactiva atracción física
- `applyRotoBallForce(target_x, target_y, deltaTime)`: Aplica fuerza de resorte

### Comportamiento Físico

**Al entrar (PHYSICS → ROTOBALL):**
1. Pelotas mantienen velocidad actual (vx, vy)
2. Fuerza de atracción las acelera hacia puntos en esfera rotante
3. Amortiguación variable evita oscilaciones infinitas
4. Convergen al punto con aceleración natural

**Durante RotoBall:**
- Punto destino rota constantemente
- Fuerza se recalcula cada frame hacia posición rotada
- Pelotas "persiguen" su punto móvil
- Efecto: Convergencia orgánica con ligera oscilación

**Al salir (ROTOBALL → PHYSICS):**
1. Atracción se desactiva
2. Pelotas conservan velocidad tangencial actual
3. Gravedad vuelve a aplicarse
4. Caen con la inercia que traían de la esfera

### Archivos Modificados

- `defines.h`: 5 nuevas constantes físicas
- `ball.h/cpp`: Sistema de resorte completo
- `engine.cpp`: Enable/disable atracción en toggle, updateRotoBall() usa física
- `CLAUDE.md`: Documentación técnica completa

## Ventajas del Sistema

 Física realista con conservación de momento
 Transición orgánica (no artificial)
 Inercia preservada entrada/salida
 Amortiguación automática (no oscila infinito)
 Constantes ajustables para tuning
 Performance: O(1) por pelota

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-03 13:24:07 +02:00
parent 22e3356f80
commit 91ab6487b3
5 changed files with 192 additions and 38 deletions

View File

@@ -50,6 +50,7 @@ Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> te
target_x_ = pos_.x;
target_y_ = pos_.y;
depth_brightness_ = 1.0f;
rotoball_attraction_active_ = false;
}
// Actualiza la lógica de la clase
@@ -277,4 +278,64 @@ void Ball::setRotoBallScreenPosition(float x, float y) {
void Ball::setDepthBrightness(float brightness) {
depth_brightness_ = brightness;
}
// Activar/desactivar atracción física hacia esfera RotoBall
void Ball::enableRotoBallAttraction(bool enable) {
rotoball_attraction_active_ = enable;
}
// Aplicar fuerza de resorte hacia punto objetivo en esfera rotante
void Ball::applyRotoBallForce(float target_x, float target_y, float deltaTime) {
if (!rotoball_attraction_active_) return;
// Calcular vector diferencia (dirección hacia el target)
float diff_x = target_x - pos_.x;
float diff_y = target_y - pos_.y;
// Calcular distancia al punto objetivo
float distance = sqrtf(diff_x * diff_x + diff_y * diff_y);
// Fuerza de resorte (Ley de Hooke: F = -k * x)
float spring_force_x = ROTOBALL_SPRING_K * diff_x;
float spring_force_y = ROTOBALL_SPRING_K * diff_y;
// Amortiguación variable: más cerca del punto = más amortiguación (estabilización)
float damping = (distance < ROTOBALL_NEAR_THRESHOLD)
? ROTOBALL_DAMPING_NEAR
: ROTOBALL_DAMPING_BASE;
// Fuerza de amortiguación (proporcional a la velocidad)
float damping_force_x = damping * vx_;
float damping_force_y = damping * vy_;
// Fuerza total = Resorte - Amortiguación
float total_force_x = spring_force_x - damping_force_x;
float total_force_y = spring_force_y - damping_force_y;
// Limitar magnitud de fuerza (evitar explosiones numéricas)
float force_magnitude = sqrtf(total_force_x * total_force_x + total_force_y * total_force_y);
if (force_magnitude > ROTOBALL_MAX_FORCE) {
float scale = ROTOBALL_MAX_FORCE / force_magnitude;
total_force_x *= scale;
total_force_y *= scale;
}
// Aplicar aceleración (F = ma, asumiendo m = 1 para simplificar)
// a = F/m, pero m=1, así que a = F
vx_ += total_force_x * deltaTime;
vy_ += total_force_y * deltaTime;
// Actualizar posición con física normal (velocidad integrada)
pos_.x += vx_ * deltaTime;
pos_.y += vy_ * deltaTime;
// Mantener pelotas dentro de los límites de pantalla
if (pos_.x < 0) pos_.x = 0;
if (pos_.x + pos_.w > screen_width_) pos_.x = screen_width_ - pos_.w;
if (pos_.y < 0) pos_.y = 0;
if (pos_.y + pos_.h > screen_height_) pos_.y = screen_height_ - pos_.h;
// Actualizar sprite para renderizado
sprite_->setPos({pos_.x, pos_.y});
}