🔧 Problema corregido: - BOUNCE_VARIATION_PERCENT permitía rebotes con ganancia de energía (0.95-1.05) - Pelotas podían rebotar más alto que su posición inicial (violaba física) ✨ Cambios aplicados: - Renombrado: BOUNCE_VARIATION_PERCENT → BOUNCE_RANDOM_LOSS_PERCENT - Valor aumentado: 0.05f (5%) → 0.1f (10%) para mayor variabilidad - Lógica corregida: rango 0.90-1.00 (solo pérdida, nunca ganancia) - Comentarios actualizados para claridad semántica 📊 Impacto físico: - Antes: 0.75 × (0.95-1.05) = 0.7125-0.7875 ❌ (ganancia posible) - Después: 0.75 × (0.90-1.00) = 0.675-0.750 ✅ (solo pérdida) 🎯 Resultado: Física realista - las pelotas siempre pierden energía en rebotes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
223 lines
7.9 KiB
C++
223 lines
7.9 KiB
C++
#include "ball.h"
|
|
|
|
#include <stdlib.h> // for rand
|
|
|
|
#include <cmath> // for fabs
|
|
|
|
#include "defines.h" // for BALL_SIZE, 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, GravityDirection gravity_dir)
|
|
: sprite_(std::make_unique<Sprite>(texture)),
|
|
pos_({x, 0.0f, BALL_SIZE, 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, 0, BALL_SIZE, 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_direction_ = gravity_dir;
|
|
on_surface_ = false;
|
|
stopped_ = false;
|
|
// Coeficiente base IGUAL para todas las pelotas (solo variación por rebote individual)
|
|
loss_ = BASE_BOUNCE_COEFFICIENT; // Coeficiente fijo para todas las pelotas
|
|
}
|
|
|
|
// Actualiza la lógica de la clase
|
|
void Ball::update(float deltaTime) {
|
|
if (stopped_) {
|
|
return;
|
|
}
|
|
|
|
// Aplica la gravedad según la dirección (píxeles/segundo²)
|
|
if (!on_surface_) {
|
|
switch (gravity_direction_) {
|
|
case GravityDirection::DOWN:
|
|
vy_ += gravity_force_ * deltaTime;
|
|
break;
|
|
case GravityDirection::UP:
|
|
vy_ -= gravity_force_ * deltaTime;
|
|
break;
|
|
case GravityDirection::LEFT:
|
|
vx_ -= gravity_force_ * deltaTime;
|
|
break;
|
|
case GravityDirection::RIGHT:
|
|
vx_ += gravity_force_ * deltaTime;
|
|
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;
|
|
stopped_ = true;
|
|
}
|
|
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;
|
|
stopped_ = true;
|
|
}
|
|
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) {
|
|
if (stopped_) {
|
|
vx_ = vx_ + (vx * 60.0f); // Convertir a píxeles/segundo
|
|
}
|
|
vy_ = vy_ + (vy * 60.0f); // Convertir a píxeles/segundo
|
|
on_surface_ = false;
|
|
stopped_ = 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;
|
|
}
|
|
|
|
// 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
|
|
stopped_ = false; // Reactivar movimiento
|
|
} |