Files
vibe3_physics/source/ball.cpp
Sergio Valor ef2f5bea01 Sistema de convergencia para LOGO MODE (resolución escalable)
Implementa sistema adaptativo que evita interrupciones prematuras
en resoluciones altas. El timing ahora se ajusta según convergencia
de partículas en lugar de usar intervalos fijos.

Cambios:
- Ball: getDistanceToTarget() para medir distancia a objetivo
- Engine: shape_convergence_, logo_convergence_threshold_ y tiempos escalados
- defines.h: LOGO_CONVERGENCE_MIN/MAX (75-100%)
- updateShape(): Cálculo de % de pelotas convergidas
- toggleShapeMode(): Genera threshold aleatorio al entrar en LOGO
- setState(): Escala logo_min/max_time con resolución (base 720p)
- updateDemoMode(): Dispara cuando (tiempo>=MIN AND convergencia>=threshold) OR tiempo>=MAX

Funcionamiento:
1. Al entrar a SHAPE en LOGO: threshold random 75-100%, tiempos escalados con altura
2. Cada frame: calcula % pelotas cerca de objetivo (shape_convergence_)
3. Dispara acción cuando: (tiempo>=MIN AND convergencia>=threshold) OR tiempo>=MAX
4. Resultado: En 720p funciona como antes, en 1440p espera convergencia real

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-05 01:25:55 +02:00

398 lines
14 KiB
C++

#include "ball.h"
#include <stdlib.h> // for rand
#include <cmath> // for fabs
#include "defines.h" // for Color, SCREEN_HEIGHT, GRAVITY_FORCE
class Texture;
// Función auxiliar para generar pérdida aleatoria en rebotes
float generateBounceVariation() {
// Genera un valor entre 0 y BOUNCE_RANDOM_LOSS_PERCENT (solo pérdida adicional)
float loss = (rand() % 1000) / 1000.0f * BOUNCE_RANDOM_LOSS_PERCENT;
return 1.0f - loss; // Retorna multiplicador (ej: 0.90 - 1.00 para 10% max pérdida)
}
// Función auxiliar para generar pérdida lateral aleatoria
float generateLateralLoss() {
// Genera un valor entre 0 y LATERAL_LOSS_PERCENT
float loss = (rand() % 1000) / 1000.0f * LATERAL_LOSS_PERCENT;
return 1.0f - loss; // Retorna multiplicador (ej: 0.98 - 1.0 para 0-2% pérdida)
}
// Constructor
Ball::Ball(float x, float vx, float vy, Color color, std::shared_ptr<Texture> texture, int screen_width, int screen_height, int ball_size, GravityDirection gravity_dir, float mass_factor)
: sprite_(std::make_unique<Sprite>(texture)),
pos_({x, 0.0f, static_cast<float>(ball_size), static_cast<float>(ball_size)}) {
// Convertir velocidades de píxeles/frame a píxeles/segundo (multiplicar por 60)
vx_ = vx * 60.0f;
vy_ = vy * 60.0f;
sprite_->setPos({pos_.x, pos_.y});
sprite_->setSize(ball_size, ball_size);
sprite_->setClip({0.0f, 0.0f, static_cast<float>(ball_size), static_cast<float>(ball_size)});
color_ = color;
// Convertir gravedad de píxeles/frame² a píxeles/segundo² (multiplicar por 60²)
gravity_force_ = GRAVITY_FORCE * 60.0f * 60.0f;
gravity_mass_factor_ = mass_factor; // Factor de masa individual para esta pelota
gravity_direction_ = gravity_dir;
screen_width_ = screen_width; // Dimensiones del terreno de juego
screen_height_ = screen_height;
on_surface_ = false;
// Coeficiente base IGUAL para todas las pelotas (solo variación por rebote individual)
loss_ = BASE_BOUNCE_COEFFICIENT; // Coeficiente fijo para todas las pelotas
// Inicializar valores RotoBall
pos_3d_x_ = 0.0f;
pos_3d_y_ = 0.0f;
pos_3d_z_ = 0.0f;
target_x_ = pos_.x;
target_y_ = pos_.y;
depth_brightness_ = 1.0f;
depth_scale_ = 1.0f;
rotoball_attraction_active_ = false;
}
// Actualiza la lógica de la clase
void Ball::update(float deltaTime) {
// Aplica la gravedad según la dirección (píxeles/segundo²)
if (!on_surface_) {
// Aplicar gravedad multiplicada por factor de masa individual
float effective_gravity = gravity_force_ * gravity_mass_factor_ * deltaTime;
switch (gravity_direction_) {
case GravityDirection::DOWN:
vy_ += effective_gravity;
break;
case GravityDirection::UP:
vy_ -= effective_gravity;
break;
case GravityDirection::LEFT:
vx_ -= effective_gravity;
break;
case GravityDirection::RIGHT:
vx_ += effective_gravity;
break;
}
}
// Actualiza la posición en función de la velocidad (píxeles/segundo)
if (!on_surface_) {
pos_.x += vx_ * deltaTime;
pos_.y += vy_ * deltaTime;
} else {
// Si está en superficie, mantener posición según dirección de gravedad
switch (gravity_direction_) {
case GravityDirection::DOWN:
pos_.y = screen_height_ - pos_.h;
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
break;
case GravityDirection::UP:
pos_.y = 0;
pos_.x += vx_ * deltaTime; // Seguir moviéndose en X
break;
case GravityDirection::LEFT:
pos_.x = 0;
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
break;
case GravityDirection::RIGHT:
pos_.x = screen_width_ - pos_.w;
pos_.y += vy_ * deltaTime; // Seguir moviéndose en Y
break;
}
}
// Comprueba las colisiones con el lateral izquierdo
if (pos_.x < 0) {
pos_.x = 0;
if (gravity_direction_ == GravityDirection::LEFT) {
// Colisión con superficie de gravedad - aplicar variación aleatoria
vx_ = -vx_ * loss_ * generateBounceVariation();
if (std::fabs(vx_) < 6.0f) {
vx_ = 0.0f;
on_surface_ = true;
}
} else {
// Rebote normal - con pérdida lateral aleatoria
vx_ = -vx_ * generateLateralLoss();
}
// Pérdida lateral en velocidad vertical también
vy_ *= generateLateralLoss();
}
// Comprueba las colisiones con el lateral derecho
if (pos_.x + pos_.w > screen_width_) {
pos_.x = screen_width_ - pos_.w;
if (gravity_direction_ == GravityDirection::RIGHT) {
// Colisión con superficie de gravedad - aplicar variación aleatoria
vx_ = -vx_ * loss_ * generateBounceVariation();
if (std::fabs(vx_) < 6.0f) {
vx_ = 0.0f;
on_surface_ = true;
}
} else {
// Rebote normal - con pérdida lateral aleatoria
vx_ = -vx_ * generateLateralLoss();
}
// Pérdida lateral en velocidad vertical también
vy_ *= generateLateralLoss();
}
// Comprueba las colisiones con la parte superior
if (pos_.y < 0) {
pos_.y = 0;
if (gravity_direction_ == GravityDirection::UP) {
// Colisión con superficie de gravedad - aplicar variación aleatoria
vy_ = -vy_ * loss_ * generateBounceVariation();
if (std::fabs(vy_) < 6.0f) {
vy_ = 0.0f;
on_surface_ = true;
}
} else {
// Rebote normal - con pérdida lateral aleatoria
vy_ = -vy_ * generateLateralLoss();
}
// Pérdida lateral en velocidad horizontal también
vx_ *= generateLateralLoss();
}
// Comprueba las colisiones con la parte inferior
if (pos_.y + pos_.h > screen_height_) {
pos_.y = screen_height_ - pos_.h;
if (gravity_direction_ == GravityDirection::DOWN) {
// Colisión con superficie de gravedad - aplicar variación aleatoria
vy_ = -vy_ * loss_ * generateBounceVariation();
if (std::fabs(vy_) < 6.0f) {
vy_ = 0.0f;
on_surface_ = true;
}
} else {
// Rebote normal - con pérdida lateral aleatoria
vy_ = -vy_ * generateLateralLoss();
}
// Pérdida lateral en velocidad horizontal también
vx_ *= generateLateralLoss();
}
// Aplica rozamiento al estar en superficie
if (on_surface_) {
// Convertir rozamiento de frame-based a time-based
float friction_factor = pow(0.97f, 60.0f * deltaTime);
switch (gravity_direction_) {
case GravityDirection::DOWN:
case GravityDirection::UP:
// Fricción en X cuando gravedad es vertical
vx_ = vx_ * friction_factor;
if (std::fabs(vx_) < 6.0f) {
vx_ = 0.0f;
}
break;
case GravityDirection::LEFT:
case GravityDirection::RIGHT:
// Fricción en Y cuando gravedad es horizontal
vy_ = vy_ * friction_factor;
if (std::fabs(vy_) < 6.0f) {
vy_ = 0.0f;
}
break;
}
}
// Actualiza la posición del sprite
sprite_->setPos({pos_.x, pos_.y});
}
// Pinta la clase
void Ball::render() {
sprite_->setColor(color_.r, color_.g, color_.b);
sprite_->render();
}
// Modifica la velocidad (convierte de frame-based a time-based)
void Ball::modVel(float vx, float vy) {
vx_ = vx_ + (vx * 60.0f); // Convertir a píxeles/segundo
vy_ = vy_ + (vy * 60.0f); // Convertir a píxeles/segundo
on_surface_ = false;
}
// Cambia la gravedad (usa la versión convertida)
void Ball::switchGravity() {
gravity_force_ = gravity_force_ == 0.0f ? (GRAVITY_FORCE * 60.0f * 60.0f) : 0.0f;
}
// Reactiva la gravedad si está desactivada
void Ball::enableGravityIfDisabled() {
if (gravity_force_ == 0.0f) {
gravity_force_ = GRAVITY_FORCE * 60.0f * 60.0f;
}
}
// Fuerza gravedad ON (siempre activa)
void Ball::forceGravityOn() {
gravity_force_ = GRAVITY_FORCE * 60.0f * 60.0f;
}
// Fuerza gravedad OFF (siempre desactiva)
void Ball::forceGravityOff() {
gravity_force_ = 0.0f;
}
// Cambia la dirección de gravedad
void Ball::setGravityDirection(GravityDirection direction) {
gravity_direction_ = direction;
on_surface_ = false; // Ya no está en superficie al cambiar dirección
}
// Aplica un pequeño empuje lateral aleatorio
void Ball::applyRandomLateralPush() {
// Generar velocidad lateral aleatoria (nunca 0)
float lateral_speed = GRAVITY_CHANGE_LATERAL_MIN + (rand() % 1000) / 1000.0f * (GRAVITY_CHANGE_LATERAL_MAX - GRAVITY_CHANGE_LATERAL_MIN);
// Signo aleatorio (+ o -)
int sign = ((rand() % 2) * 2) - 1;
lateral_speed *= sign;
// Aplicar según la dirección de gravedad actual
switch (gravity_direction_) {
case GravityDirection::UP:
case GravityDirection::DOWN:
// Gravedad vertical -> empuje lateral en X
vx_ += lateral_speed * 60.0f; // Convertir a píxeles/segundo
break;
case GravityDirection::LEFT:
case GravityDirection::RIGHT:
// Gravedad horizontal -> empuje lateral en Y
vy_ += lateral_speed * 60.0f; // Convertir a píxeles/segundo
break;
}
}
// Funciones para modo RotoBall
void Ball::setRotoBallPosition3D(float x, float y, float z) {
pos_3d_x_ = x;
pos_3d_y_ = y;
pos_3d_z_ = z;
}
void Ball::setRotoBallTarget2D(float x, float y) {
target_x_ = x;
target_y_ = y;
}
void Ball::setRotoBallScreenPosition(float x, float y) {
pos_.x = x;
pos_.y = y;
sprite_->setPos({x, y});
}
void Ball::setDepthBrightness(float brightness) {
depth_brightness_ = brightness;
}
void Ball::setDepthScale(float scale) {
depth_scale_ = scale;
}
// Activar/desactivar atracción física hacia esfera RotoBall
void Ball::enableRotoBallAttraction(bool enable) {
rotoball_attraction_active_ = enable;
// Al activar atracción, resetear flags de superficie para permitir física completa
if (enable) {
on_surface_ = false;
}
}
// Obtener distancia actual al punto objetivo (para calcular convergencia)
float Ball::getDistanceToTarget() const {
if (!rotoball_attraction_active_) return 0.0f;
float dx = target_x_ - pos_.x;
float dy = target_y_ - pos_.y;
return sqrtf(dx * dx + dy * dy);
}
// Aplicar fuerza de resorte hacia punto objetivo en figuras 3D
void Ball::applyRotoBallForce(float target_x, float target_y, float sphere_radius, float deltaTime,
float spring_k_base, float damping_base_base, float damping_near_base,
float near_threshold_base, float max_force_base) {
if (!rotoball_attraction_active_) return;
// Calcular factor de escala basado en el radio (radio base = 80px)
// Si radius=80 → scale=1.0, si radius=160 → scale=2.0, si radius=360 → scale=4.5
const float BASE_RADIUS = 80.0f;
float scale = sphere_radius / BASE_RADIUS;
// Escalar constantes de física proporcionalmente
float spring_k = spring_k_base * scale;
float damping_base = damping_base_base * scale;
float damping_near = damping_near_base * scale;
float near_threshold = near_threshold_base * scale;
float max_force = max_force_base * scale;
// 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 = spring_k * diff_x;
float spring_force_y = spring_k * diff_y;
// Amortiguación variable: más cerca del punto = más amortiguación (estabilización)
float damping = (distance < near_threshold)
? damping_near
: 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 > max_force) {
float scale_limit = max_force / force_magnitude;
total_force_x *= scale_limit;
total_force_y *= scale_limit;
}
// 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});
}
// Sistema de cambio de sprite dinámico
void Ball::updateSize(int new_size) {
// Actualizar tamaño del hitbox
pos_.w = static_cast<float>(new_size);
pos_.h = static_cast<float>(new_size);
// Actualizar sprite
sprite_->setSize(new_size, new_size);
sprite_->setClip({0.0f, 0.0f, static_cast<float>(new_size), static_cast<float>(new_size)});
}
void Ball::setTexture(std::shared_ptr<Texture> texture) {
// Actualizar textura del sprite
sprite_->setTexture(texture);
}