Implementar figura WAVE_GRID (malla ondeante 3D) - Tecla W

- Nueva clase WaveGridShape con ecuaciones de onda 2D
- Grid adaptativo según número de pelotas (1-N puntos)
- Ecuación: z = A*sin(kx*x + phase)*cos(ky*y + phase)
- Rotación lenta en Y + animación de fase rápida
- Compatible con física spring-damper y z-sorting
- Escalable con Numpad +/-

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-03 21:45:21 +02:00
parent 8cf117ea64
commit bcbeaba504
4 changed files with 131 additions and 3 deletions

View File

@@ -107,6 +107,13 @@ constexpr float HELIX_NUM_TURNS = 3.0f; // Número de vueltas complet
constexpr float HELIX_ROTATION_SPEED_Y = 1.2f; // Velocidad rotación eje Y (rad/s)
constexpr float HELIX_PHASE_SPEED = 0.5f; // Velocidad de animación vertical (rad/s)
// Configuración de Wave Grid (malla ondeante 3D)
constexpr float WAVE_GRID_SIZE_FACTOR = 0.35f; // Tamaño del grid (proporción de altura)
constexpr float WAVE_GRID_AMPLITUDE = 0.15f; // Amplitud de las ondas (proporción de altura)
constexpr float WAVE_GRID_FREQUENCY = 3.0f; // Frecuencia de ondas (ciclos por grid)
constexpr float WAVE_GRID_PHASE_SPEED = 2.0f; // Velocidad de animación de ondas (rad/s)
constexpr float WAVE_GRID_ROTATION_SPEED_Y = 0.4f; // Velocidad rotación eje Y (rad/s)
// Control manual de escala de figuras 3D (Numpad +/-)
constexpr float SHAPE_SCALE_MIN = 0.3f; // Escala mínima (30%)
constexpr float SHAPE_SCALE_MAX = 3.0f; // Escala máxima (300%)

View File

@@ -26,6 +26,7 @@
#include "shapes/sphere_shape.h" // for SphereShape
#include "shapes/cube_shape.h" // for CubeShape
#include "shapes/helix_shape.h" // for HelixShape
#include "shapes/wave_grid_shape.h" // for WaveGridShape
// Función auxiliar para obtener la ruta del directorio del ejecutable
std::string getExecutableDirectory() {
@@ -1072,6 +1073,9 @@ void Engine::activateShape(ShapeType type) {
case ShapeType::HELIX:
active_shape_ = std::make_unique<HelixShape>();
break;
case ShapeType::WAVE_GRID:
active_shape_ = std::make_unique<WaveGridShape>();
break;
// Futuras figuras se añadirán aquí
default:
active_shape_ = std::make_unique<SphereShape>(); // Fallback

View File

@@ -0,0 +1,93 @@
#include "wave_grid_shape.h"
#include "../defines.h"
#include <cmath>
void WaveGridShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
grid_size_ = screen_height * WAVE_GRID_SIZE_FACTOR;
amplitude_ = screen_height * WAVE_GRID_AMPLITUDE;
// Calcular grid cuadrado aproximado basado en número de puntos
// Queremos grid_cols * grid_rows ≈ num_points
grid_cols_ = static_cast<int>(sqrtf(static_cast<float>(num_points)));
grid_rows_ = grid_cols_;
// Ajustar para que grid_cols * grid_rows no exceda num_points
while (grid_cols_ * grid_rows_ > num_points && grid_rows_ > 1) {
grid_rows_--;
}
// Si tenemos menos puntos que celdas, ajustar columnas también
if (grid_cols_ * grid_rows_ > num_points) {
grid_cols_ = num_points / grid_rows_;
}
// Casos especiales para pocos puntos
if (num_points < 4) {
grid_cols_ = num_points;
grid_rows_ = 1;
}
}
void WaveGridShape::update(float delta_time, float screen_width, float screen_height) {
// Recalcular dimensiones por si cambió resolución (F4)
grid_size_ = screen_height * WAVE_GRID_SIZE_FACTOR;
amplitude_ = screen_height * WAVE_GRID_AMPLITUDE;
// Actualizar rotación en eje Y (horizontal)
angle_y_ += WAVE_GRID_ROTATION_SPEED_Y * delta_time;
// Actualizar fase de las ondas (animación)
phase_ += WAVE_GRID_PHASE_SPEED * delta_time;
}
void WaveGridShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Convertir índice lineal a coordenadas 2D del grid
int col = index % grid_cols_;
int row = index / grid_cols_;
// Si el índice está fuera del grid válido, colocar en origen
if (row >= grid_rows_) {
x = 0.0f;
y = 0.0f;
z = 0.0f;
return;
}
// Normalizar coordenadas del grid a rango [-1, 1]
float u = (static_cast<float>(col) / static_cast<float>(grid_cols_ - 1)) * 2.0f - 1.0f;
float v = (static_cast<float>(row) / static_cast<float>(grid_rows_ - 1)) * 2.0f - 1.0f;
// Casos especiales para grids de 1 columna/fila
if (grid_cols_ == 1) u = 0.0f;
if (grid_rows_ == 1) v = 0.0f;
// Posición base en el grid (escalada por tamaño)
float x_base = u * grid_size_;
float y_base = v * grid_size_;
// Calcular Z usando función de onda 2D
// z = amplitude * sin(frequency * x + phase) * cos(frequency * y + phase)
float kx = WAVE_GRID_FREQUENCY * PI; // Frecuencia en X
float ky = WAVE_GRID_FREQUENCY * PI; // Frecuencia en Y
float z_base = amplitude_ * sinf(kx * u + phase_) * cosf(ky * v + phase_);
// Aplicar rotación en eje Y (horizontal)
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;
// Retornar coordenadas finales
x = x_rot;
y = y_base;
z = z_rot;
}
float WaveGridShape::getScaleFactor(float screen_height) const {
// Factor de escala para física: proporcional al tamaño del grid
// Grid base = 84px (0.35 * 240px en resolución 320x240)
const float BASE_SIZE = 84.0f;
float current_size = screen_height * WAVE_GRID_SIZE_FACTOR;
return current_size / BASE_SIZE;
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include "shape.h"
// Figura: Malla ondeante 3D (Wave Grid)
// Comportamiento: Grid 2D con ondas sinusoidales en Z + rotación
// Ecuaciones: z = A * sin(kx*x + phase) * cos(ky*y + phase)
class WaveGridShape : public Shape {
private:
float angle_y_ = 0.0f; // Ángulo de rotación en eje Y (rad)
float phase_ = 0.0f; // Fase de animación de ondas (rad)
float grid_size_ = 0.0f; // Tamaño del grid (píxeles)
float amplitude_ = 0.0f; // Amplitud de las ondas (píxeles)
int grid_cols_ = 0; // Número de columnas del grid
int grid_rows_ = 0; // Número de filas del grid
int num_points_ = 0; // Cantidad total de puntos
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 "WAVE GRID"; }
float getScaleFactor(float screen_height) const override;
};