Implementar sistema polimórfico de figuras 3D (Sphere + Cube)
- Crear interfaz abstracta Shape con métodos virtuales - Refactorizar RotoBall → SphereShape (clase polimórfica) - Implementar CubeShape con triple rotación (X/Y/Z) - Distribución inteligente en cubo: vértices/centros/grid 3D - Cambiar controles: F=toggle, Q/W/E/R/T/Y/U/I=figuras, B=temas - Actualizar SimulationMode: ROTOBALL → SHAPE - Añadir enum ShapeType (8 figuras: Sphere/Cube/Helix/Torus/etc.) - Incluir source/shapes/*.cpp en CMakeLists.txt - Física compartida escalable entre todas las figuras - Roadmap: 6 figuras pendientes (Helix/Torus/Wave/Cylinder/Icosahedron/Atom) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
168
source/shapes/cube_shape.cpp
Normal file
168
source/shapes/cube_shape.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
#include "cube_shape.h"
|
||||
#include "../defines.h"
|
||||
#include <cmath>
|
||||
|
||||
void CubeShape::generatePoints(int num_points, float screen_width, float screen_height) {
|
||||
num_points_ = num_points;
|
||||
size_ = screen_height * CUBE_SIZE_FACTOR;
|
||||
|
||||
// Limpiar vectores anteriores
|
||||
base_x_.clear();
|
||||
base_y_.clear();
|
||||
base_z_.clear();
|
||||
|
||||
// Seleccionar estrategia según cantidad de pelotas
|
||||
if (num_points <= 8) {
|
||||
generateVertices();
|
||||
} else if (num_points <= 26) {
|
||||
generateVerticesAndCenters();
|
||||
} else {
|
||||
generateVolumetricGrid();
|
||||
}
|
||||
|
||||
// Si sobran posiciones, repetir en espiral (distribución uniforme)
|
||||
while (static_cast<int>(base_x_.size()) < num_points) {
|
||||
base_x_.push_back(base_x_[base_x_.size() % base_x_.size()]);
|
||||
base_y_.push_back(base_y_[base_y_.size() % base_y_.size()]);
|
||||
base_z_.push_back(base_z_[base_z_.size() % base_z_.size()]);
|
||||
}
|
||||
}
|
||||
|
||||
void CubeShape::update(float delta_time, float screen_width, float screen_height) {
|
||||
// Recalcular tamaño por si cambió resolución (F4)
|
||||
size_ = screen_height * CUBE_SIZE_FACTOR;
|
||||
|
||||
// Actualizar ángulos de rotación en los 3 ejes (efecto Rubik)
|
||||
angle_x_ += CUBE_ROTATION_SPEED_X * delta_time;
|
||||
angle_y_ += CUBE_ROTATION_SPEED_Y * delta_time;
|
||||
angle_z_ += CUBE_ROTATION_SPEED_Z * delta_time;
|
||||
}
|
||||
|
||||
void CubeShape::getPoint3D(int index, float& x, float& y, float& z) const {
|
||||
if (index >= static_cast<int>(base_x_.size())) {
|
||||
x = y = z = 0.0f;
|
||||
return;
|
||||
}
|
||||
|
||||
// Obtener posición base
|
||||
float x_base = base_x_[index];
|
||||
float y_base = base_y_[index];
|
||||
float z_base = base_z_[index];
|
||||
|
||||
// Aplicar rotación en eje Z
|
||||
float cos_z = cosf(angle_z_);
|
||||
float sin_z = sinf(angle_z_);
|
||||
float x_rot_z = x_base * cos_z - y_base * sin_z;
|
||||
float y_rot_z = x_base * sin_z + y_base * cos_z;
|
||||
float z_rot_z = z_base;
|
||||
|
||||
// Aplicar rotación en eje Y
|
||||
float cos_y = cosf(angle_y_);
|
||||
float sin_y = sinf(angle_y_);
|
||||
float x_rot_y = x_rot_z * cos_y + z_rot_z * sin_y;
|
||||
float y_rot_y = y_rot_z;
|
||||
float z_rot_y = -x_rot_z * sin_y + z_rot_z * cos_y;
|
||||
|
||||
// Aplicar rotación en eje X
|
||||
float cos_x = cosf(angle_x_);
|
||||
float sin_x = sinf(angle_x_);
|
||||
float x_final = x_rot_y;
|
||||
float y_final = y_rot_y * cos_x - z_rot_y * sin_x;
|
||||
float z_final = y_rot_y * sin_x + z_rot_y * cos_x;
|
||||
|
||||
// Retornar coordenadas finales rotadas
|
||||
x = x_final;
|
||||
y = y_final;
|
||||
z = z_final;
|
||||
}
|
||||
|
||||
float CubeShape::getScaleFactor(float screen_height) const {
|
||||
// Factor de escala para física: proporcional al tamaño del cubo
|
||||
// Tamaño base = 60px (resolución 320x240, factor 0.25)
|
||||
const float BASE_SIZE = 60.0f;
|
||||
float current_size = screen_height * CUBE_SIZE_FACTOR;
|
||||
return current_size / BASE_SIZE;
|
||||
}
|
||||
|
||||
// Métodos auxiliares privados
|
||||
|
||||
void CubeShape::generateVertices() {
|
||||
// 8 vértices del cubo: todas las combinaciones de (±size, ±size, ±size)
|
||||
for (int x_sign : {-1, 1}) {
|
||||
for (int y_sign : {-1, 1}) {
|
||||
for (int z_sign : {-1, 1}) {
|
||||
base_x_.push_back(x_sign * size_);
|
||||
base_y_.push_back(y_sign * size_);
|
||||
base_z_.push_back(z_sign * size_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CubeShape::generateVerticesAndCenters() {
|
||||
// 1. Añadir 8 vértices
|
||||
generateVertices();
|
||||
|
||||
// 2. Añadir 6 centros de caras
|
||||
// Caras: X=±size (Y,Z varían), Y=±size (X,Z varían), Z=±size (X,Y varían)
|
||||
base_x_.push_back(size_); base_y_.push_back(0); base_z_.push_back(0); // +X
|
||||
base_x_.push_back(-size_); base_y_.push_back(0); base_z_.push_back(0); // -X
|
||||
base_x_.push_back(0); base_y_.push_back(size_); base_z_.push_back(0); // +Y
|
||||
base_x_.push_back(0); base_y_.push_back(-size_);base_z_.push_back(0); // -Y
|
||||
base_x_.push_back(0); base_y_.push_back(0); base_z_.push_back(size_); // +Z
|
||||
base_x_.push_back(0); base_y_.push_back(0); base_z_.push_back(-size_); // -Z
|
||||
|
||||
// 3. Añadir 12 centros de aristas
|
||||
// Aristas paralelas a X (4), Y (4), Z (4)
|
||||
// Paralelas a X (Y y Z en vértices, X=0)
|
||||
for (int y_sign : {-1, 1}) {
|
||||
for (int z_sign : {-1, 1}) {
|
||||
base_x_.push_back(0);
|
||||
base_y_.push_back(y_sign * size_);
|
||||
base_z_.push_back(z_sign * size_);
|
||||
}
|
||||
}
|
||||
// Paralelas a Y (X y Z en vértices, Y=0)
|
||||
for (int x_sign : {-1, 1}) {
|
||||
for (int z_sign : {-1, 1}) {
|
||||
base_x_.push_back(x_sign * size_);
|
||||
base_y_.push_back(0);
|
||||
base_z_.push_back(z_sign * size_);
|
||||
}
|
||||
}
|
||||
// Paralelas a Z (X y Y en vértices, Z=0)
|
||||
for (int x_sign : {-1, 1}) {
|
||||
for (int y_sign : {-1, 1}) {
|
||||
base_x_.push_back(x_sign * size_);
|
||||
base_y_.push_back(y_sign * size_);
|
||||
base_z_.push_back(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void CubeShape::generateVolumetricGrid() {
|
||||
// Calcular dimensión del grid cúbico: N³ ≈ num_points
|
||||
int grid_dim = static_cast<int>(ceilf(cbrtf(static_cast<float>(num_points_))));
|
||||
if (grid_dim < 3) grid_dim = 3; // Mínimo grid 3x3x3
|
||||
|
||||
float step = (2.0f * size_) / (grid_dim - 1); // Espacio entre puntos
|
||||
|
||||
for (int ix = 0; ix < grid_dim; ix++) {
|
||||
for (int iy = 0; iy < grid_dim; iy++) {
|
||||
for (int iz = 0; iz < grid_dim; iz++) {
|
||||
float x = -size_ + ix * step;
|
||||
float y = -size_ + iy * step;
|
||||
float z = -size_ + iz * step;
|
||||
|
||||
base_x_.push_back(x);
|
||||
base_y_.push_back(y);
|
||||
base_z_.push_back(z);
|
||||
|
||||
// Si ya tenemos suficientes puntos, salir
|
||||
if (static_cast<int>(base_x_.size()) >= num_points_) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
37
source/shapes/cube_shape.h
Normal file
37
source/shapes/cube_shape.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "shape.h"
|
||||
#include <vector>
|
||||
|
||||
// Figura: Cubo 3D rotante
|
||||
// Distribución:
|
||||
// - 1-8 pelotas: Solo vértices (8 puntos)
|
||||
// - 9-26 pelotas: Vértices + centros de caras + centros de aristas (26 puntos)
|
||||
// - 27+ pelotas: Grid volumétrico 3D uniforme
|
||||
// Comportamiento: Rotación simultánea en ejes X, Y, Z (efecto Rubik)
|
||||
class CubeShape : public Shape {
|
||||
private:
|
||||
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
|
||||
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
|
||||
float angle_z_ = 0.0f; // Ángulo de rotación en eje Z (rad)
|
||||
float size_ = 0.0f; // Mitad del lado del cubo (píxeles)
|
||||
int num_points_ = 0; // Cantidad de puntos generados
|
||||
|
||||
// Posiciones base 3D (sin rotar) - se calculan en generatePoints()
|
||||
std::vector<float> base_x_;
|
||||
std::vector<float> base_y_;
|
||||
std::vector<float> base_z_;
|
||||
|
||||
public:
|
||||
void generatePoints(int num_points, float screen_width, float screen_height) override;
|
||||
void update(float delta_time, float screen_width, float screen_height) override;
|
||||
void getPoint3D(int index, float& x, float& y, float& z) const override;
|
||||
const char* getName() const override { return "CUBE"; }
|
||||
float getScaleFactor(float screen_height) const override;
|
||||
|
||||
private:
|
||||
// Métodos auxiliares para distribución de puntos
|
||||
void generateVertices(); // 8 vértices
|
||||
void generateVerticesAndCenters(); // 26 puntos (vértices + caras + aristas)
|
||||
void generateVolumetricGrid(); // Grid 3D para muchas pelotas
|
||||
};
|
||||
30
source/shapes/shape.h
Normal file
30
source/shapes/shape.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
// Interfaz abstracta para todas las figuras 3D
|
||||
class Shape {
|
||||
public:
|
||||
virtual ~Shape() = default;
|
||||
|
||||
// Generar distribución inicial de puntos en la figura
|
||||
// num_points: cantidad de pelotas a distribuir
|
||||
// screen_width/height: dimensiones del área de juego (para escalar)
|
||||
virtual void generatePoints(int num_points, float screen_width, float screen_height) = 0;
|
||||
|
||||
// Actualizar animación de la figura (rotación, deformación, etc.)
|
||||
// delta_time: tiempo transcurrido desde último frame
|
||||
// screen_width/height: dimensiones actuales (puede cambiar con F4)
|
||||
virtual void update(float delta_time, float screen_width, float screen_height) = 0;
|
||||
|
||||
// Obtener posición 3D del punto i después de transformaciones (rotación, etc.)
|
||||
// index: índice del punto (0 a num_points-1)
|
||||
// x, y, z: coordenadas 3D en espacio mundo (centradas en 0,0,0)
|
||||
virtual void getPoint3D(int index, float& x, float& y, float& z) const = 0;
|
||||
|
||||
// Obtener nombre de la figura para debug display
|
||||
virtual const char* getName() const = 0;
|
||||
|
||||
// Obtener factor de escala para ajustar física según tamaño de figura
|
||||
// screen_height: altura actual de pantalla
|
||||
// Retorna: factor multiplicador para constantes de física (spring_k, damping, etc.)
|
||||
virtual float getScaleFactor(float screen_height) const = 0;
|
||||
};
|
||||
58
source/shapes/sphere_shape.cpp
Normal file
58
source/shapes/sphere_shape.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
#include "sphere_shape.h"
|
||||
#include "../defines.h"
|
||||
#include <cmath>
|
||||
|
||||
void SphereShape::generatePoints(int num_points, float screen_width, float screen_height) {
|
||||
num_points_ = num_points;
|
||||
radius_ = screen_height * ROTOBALL_RADIUS_FACTOR;
|
||||
// Las posiciones 3D se calculan en getPoint3D() usando Fibonacci Sphere
|
||||
}
|
||||
|
||||
void SphereShape::update(float delta_time, float screen_width, float screen_height) {
|
||||
// Recalcular radio por si cambió resolución (F4)
|
||||
radius_ = screen_height * ROTOBALL_RADIUS_FACTOR;
|
||||
|
||||
// Actualizar ángulos de rotación
|
||||
angle_y_ += ROTOBALL_ROTATION_SPEED_Y * delta_time;
|
||||
angle_x_ += ROTOBALL_ROTATION_SPEED_X * delta_time;
|
||||
}
|
||||
|
||||
void SphereShape::getPoint3D(int index, float& x, float& y, float& z) const {
|
||||
// Algoritmo Fibonacci Sphere para distribución uniforme
|
||||
const float golden_ratio = (1.0f + sqrtf(5.0f)) / 2.0f;
|
||||
const float angle_increment = PI * 2.0f * golden_ratio;
|
||||
|
||||
float t = static_cast<float>(index) / static_cast<float>(num_points_);
|
||||
float phi = acosf(1.0f - 2.0f * t); // Latitud
|
||||
float theta = angle_increment * static_cast<float>(index); // Longitud
|
||||
|
||||
// Convertir coordenadas esféricas a cartesianas
|
||||
float x_base = cosf(theta) * sinf(phi) * radius_;
|
||||
float y_base = sinf(theta) * sinf(phi) * radius_;
|
||||
float z_base = cosf(phi) * radius_;
|
||||
|
||||
// Aplicar rotación en eje Y
|
||||
float cos_y = cosf(angle_y_);
|
||||
float sin_y = sinf(angle_y_);
|
||||
float x_rot = x_base * cos_y - z_base * sin_y;
|
||||
float z_rot = x_base * sin_y + z_base * cos_y;
|
||||
|
||||
// Aplicar rotación en eje X
|
||||
float cos_x = cosf(angle_x_);
|
||||
float sin_x = sinf(angle_x_);
|
||||
float y_rot = y_base * cos_x - z_rot * sin_x;
|
||||
float z_final = y_base * sin_x + z_rot * cos_x;
|
||||
|
||||
// Retornar coordenadas finales rotadas
|
||||
x = x_rot;
|
||||
y = y_rot;
|
||||
z = z_final;
|
||||
}
|
||||
|
||||
float SphereShape::getScaleFactor(float screen_height) const {
|
||||
// Factor de escala para física: proporcional al radio
|
||||
// Radio base = 80px (resolución 320x240)
|
||||
const float BASE_RADIUS = 80.0f;
|
||||
float current_radius = screen_height * ROTOBALL_RADIUS_FACTOR;
|
||||
return current_radius / BASE_RADIUS;
|
||||
}
|
||||
21
source/shapes/sphere_shape.h
Normal file
21
source/shapes/sphere_shape.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "shape.h"
|
||||
|
||||
// Figura: Esfera 3D con distribución uniforme (Fibonacci Sphere Algorithm)
|
||||
// Comportamiento: Rotación dual en ejes X e Y
|
||||
// Uso anterior: RotoBall
|
||||
class SphereShape : public Shape {
|
||||
private:
|
||||
float angle_x_ = 0.0f; // Ángulo de rotación en eje X (rad)
|
||||
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
|
||||
float radius_ = 0.0f; // Radio de la esfera (píxeles)
|
||||
int num_points_ = 0; // Cantidad de puntos generados
|
||||
|
||||
public:
|
||||
void generatePoints(int num_points, float screen_width, float screen_height) override;
|
||||
void update(float delta_time, float screen_width, float screen_height) override;
|
||||
void getPoint3D(int index, float& x, float& y, float& z) const override;
|
||||
const char* getName() const override { return "SPHERE"; }
|
||||
float getScaleFactor(float screen_height) const override;
|
||||
};
|
||||
Reference in New Issue
Block a user