From bcbeaba50400d539ff4814cdee01ec556d6297ac Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Fri, 3 Oct 2025 21:45:21 +0200 Subject: [PATCH] Implementar figura WAVE_GRID (malla ondeante 3D) - Tecla W MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- source/defines.h | 7 +++ source/engine.cpp | 10 +++- source/shapes/wave_grid_shape.cpp | 93 +++++++++++++++++++++++++++++++ source/shapes/wave_grid_shape.h | 24 ++++++++ 4 files changed, 131 insertions(+), 3 deletions(-) create mode 100644 source/shapes/wave_grid_shape.cpp create mode 100644 source/shapes/wave_grid_shape.h diff --git a/source/defines.h b/source/defines.h index e5f29e5..237f5e3 100644 --- a/source/defines.h +++ b/source/defines.h @@ -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%) diff --git a/source/engine.cpp b/source/engine.cpp index 284fde2..2905a48 100644 --- a/source/engine.cpp +++ b/source/engine.cpp @@ -23,9 +23,10 @@ #include "ball.h" // for Ball #include "external/dbgtxt.h" // for dbg_init, dbg_print #include "external/texture.h" // for Texture -#include "shapes/sphere_shape.h" // for SphereShape -#include "shapes/cube_shape.h" // for CubeShape -#include "shapes/helix_shape.h" // for HelixShape +#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(); break; + case ShapeType::WAVE_GRID: + active_shape_ = std::make_unique(); + break; // Futuras figuras se añadirán aquí default: active_shape_ = std::make_unique(); // Fallback diff --git a/source/shapes/wave_grid_shape.cpp b/source/shapes/wave_grid_shape.cpp new file mode 100644 index 0000000..ab1330f --- /dev/null +++ b/source/shapes/wave_grid_shape.cpp @@ -0,0 +1,93 @@ +#include "wave_grid_shape.h" +#include "../defines.h" +#include + +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(sqrtf(static_cast(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(col) / static_cast(grid_cols_ - 1)) * 2.0f - 1.0f; + float v = (static_cast(row) / static_cast(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; +} diff --git a/source/shapes/wave_grid_shape.h b/source/shapes/wave_grid_shape.h new file mode 100644 index 0000000..d00aac2 --- /dev/null +++ b/source/shapes/wave_grid_shape.h @@ -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; +};