Files
vibe3_physics/source/shapes/wave_grid_shape.cpp
Sergio Valor bcbeaba504 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>
2025-10-03 21:45:21 +02:00

94 lines
3.2 KiB
C++

#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;
}