Implementar figura TORUS (toroide/donut 3D) - Tecla R
- Nueva clase TorusShape con ecuaciones paramétricas - Distribución uniforme en anillos y puntos por anillo - Rotación triple simultánea (X, Y, Z) - Radios: major=0.25, minor=0.12 (proporción altura) - 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:
96
source/shapes/torus_shape.cpp
Normal file
96
source/shapes/torus_shape.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "torus_shape.h"
|
||||
#include "../defines.h"
|
||||
#include <cmath>
|
||||
|
||||
void TorusShape::generatePoints(int num_points, float screen_width, float screen_height) {
|
||||
num_points_ = num_points;
|
||||
major_radius_ = screen_height * TORUS_MAJOR_RADIUS_FACTOR;
|
||||
minor_radius_ = screen_height * TORUS_MINOR_RADIUS_FACTOR;
|
||||
// Las posiciones 3D se calculan en getPoint3D() usando ecuaciones paramétricas del torus
|
||||
}
|
||||
|
||||
void TorusShape::update(float delta_time, float screen_width, float screen_height) {
|
||||
// Recalcular radios por si cambió resolución (F4)
|
||||
major_radius_ = screen_height * TORUS_MAJOR_RADIUS_FACTOR;
|
||||
minor_radius_ = screen_height * TORUS_MINOR_RADIUS_FACTOR;
|
||||
|
||||
// Actualizar ángulos de rotación (triple rotación XYZ)
|
||||
angle_x_ += TORUS_ROTATION_SPEED_X * delta_time;
|
||||
angle_y_ += TORUS_ROTATION_SPEED_Y * delta_time;
|
||||
angle_z_ += TORUS_ROTATION_SPEED_Z * delta_time;
|
||||
}
|
||||
|
||||
void TorusShape::getPoint3D(int index, float& x, float& y, float& z) const {
|
||||
// Distribuir puntos uniformemente en la superficie del torus
|
||||
// Usamos distribución aproximadamente uniforme basada en área
|
||||
|
||||
// Calcular número aproximado de anillos y puntos por anillo
|
||||
int num_rings = static_cast<int>(sqrtf(static_cast<float>(num_points_) * 0.5f));
|
||||
if (num_rings < 2) num_rings = 2;
|
||||
|
||||
int points_per_ring = num_points_ / num_rings;
|
||||
if (points_per_ring < 3) points_per_ring = 3;
|
||||
|
||||
// Obtener parámetros u y v del índice
|
||||
int ring = index / points_per_ring;
|
||||
int point_in_ring = index % points_per_ring;
|
||||
|
||||
// Si nos pasamos del número de anillos, usar el último
|
||||
if (ring >= num_rings) {
|
||||
ring = num_rings - 1;
|
||||
point_in_ring = index - (ring * points_per_ring);
|
||||
if (point_in_ring >= points_per_ring) {
|
||||
point_in_ring = points_per_ring - 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Parámetros u y v normalizados [0, 2π]
|
||||
float u = (static_cast<float>(ring) / static_cast<float>(num_rings)) * 2.0f * PI;
|
||||
float v = (static_cast<float>(point_in_ring) / static_cast<float>(points_per_ring)) * 2.0f * PI;
|
||||
|
||||
// Ecuaciones paramétricas del torus
|
||||
// x = (R + r*cos(v)) * cos(u)
|
||||
// y = (R + r*cos(v)) * sin(u)
|
||||
// z = r * sin(v)
|
||||
float cos_v = cosf(v);
|
||||
float sin_v = sinf(v);
|
||||
float cos_u = cosf(u);
|
||||
float sin_u = sinf(u);
|
||||
|
||||
float radius_at_v = major_radius_ + minor_radius_ * cos_v;
|
||||
|
||||
float x_base = radius_at_v * cos_u;
|
||||
float y_base = radius_at_v * sin_u;
|
||||
float z_base = minor_radius_ * sin_v;
|
||||
|
||||
// Aplicar rotación en eje X
|
||||
float cos_x = cosf(angle_x_);
|
||||
float sin_x = sinf(angle_x_);
|
||||
float y_rot_x = y_base * cos_x - z_base * sin_x;
|
||||
float z_rot_x = y_base * sin_x + z_base * cos_x;
|
||||
|
||||
// Aplicar rotación en eje Y
|
||||
float cos_y = cosf(angle_y_);
|
||||
float sin_y = sinf(angle_y_);
|
||||
float x_rot_y = x_base * cos_y - z_rot_x * sin_y;
|
||||
float z_rot_y = x_base * sin_y + z_rot_x * cos_y;
|
||||
|
||||
// Aplicar rotación en eje Z
|
||||
float cos_z = cosf(angle_z_);
|
||||
float sin_z = sinf(angle_z_);
|
||||
float x_final = x_rot_y * cos_z - y_rot_x * sin_z;
|
||||
float y_final = x_rot_y * sin_z + y_rot_x * cos_z;
|
||||
|
||||
// Retornar coordenadas finales rotadas
|
||||
x = x_final;
|
||||
y = y_final;
|
||||
z = z_rot_y;
|
||||
}
|
||||
|
||||
float TorusShape::getScaleFactor(float screen_height) const {
|
||||
// Factor de escala para física: proporcional al radio mayor
|
||||
// Radio mayor base = 60px (0.25 * 240px en resolución 320x240)
|
||||
const float BASE_RADIUS = 60.0f;
|
||||
float current_radius = screen_height * TORUS_MAJOR_RADIUS_FACTOR;
|
||||
return current_radius / BASE_RADIUS;
|
||||
}
|
||||
Reference in New Issue
Block a user