diff --git a/source/defines.h b/source/defines.h index 237f5e3..d31fc34 100644 --- a/source/defines.h +++ b/source/defines.h @@ -114,6 +114,13 @@ constexpr float WAVE_GRID_FREQUENCY = 3.0f; // Frecuencia de ondas (ciclo 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) +// Configuración de Torus (toroide/donut 3D) +constexpr float TORUS_MAJOR_RADIUS_FACTOR = 0.25f; // Radio mayor R (centro torus a centro tubo) +constexpr float TORUS_MINOR_RADIUS_FACTOR = 0.12f; // Radio menor r (grosor del tubo) +constexpr float TORUS_ROTATION_SPEED_X = 0.6f; // Velocidad rotación eje X (rad/s) +constexpr float TORUS_ROTATION_SPEED_Y = 0.9f; // Velocidad rotación eje Y (rad/s) +constexpr float TORUS_ROTATION_SPEED_Z = 0.3f; // Velocidad rotación eje Z (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 2905a48..2e2bdd5 100644 --- a/source/engine.cpp +++ b/source/engine.cpp @@ -27,6 +27,7 @@ #include "shapes/cube_shape.h" // for CubeShape #include "shapes/helix_shape.h" // for HelixShape #include "shapes/wave_grid_shape.h" // for WaveGridShape +#include "shapes/torus_shape.h" // for TorusShape // Función auxiliar para obtener la ruta del directorio del ejecutable std::string getExecutableDirectory() { @@ -1073,6 +1074,9 @@ void Engine::activateShape(ShapeType type) { case ShapeType::HELIX: active_shape_ = std::make_unique(); break; + case ShapeType::TORUS: + active_shape_ = std::make_unique(); + break; case ShapeType::WAVE_GRID: active_shape_ = std::make_unique(); break; diff --git a/source/shapes/torus_shape.cpp b/source/shapes/torus_shape.cpp new file mode 100644 index 0000000..4d4af13 --- /dev/null +++ b/source/shapes/torus_shape.cpp @@ -0,0 +1,96 @@ +#include "torus_shape.h" +#include "../defines.h" +#include + +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(sqrtf(static_cast(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(ring) / static_cast(num_rings)) * 2.0f * PI; + float v = (static_cast(point_in_ring) / static_cast(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; +} diff --git a/source/shapes/torus_shape.h b/source/shapes/torus_shape.h new file mode 100644 index 0000000..8a1d7e5 --- /dev/null +++ b/source/shapes/torus_shape.h @@ -0,0 +1,23 @@ +#pragma once + +#include "shape.h" + +// Figura: Torus/Toroide 3D (donut/rosquilla) +// Comportamiento: Superficie toroidal con rotación triple (X, Y, Z) +// Ecuaciones: x = (R + r*cos(v))*cos(u), y = (R + r*cos(v))*sin(u), z = r*sin(v) +class TorusShape : 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 major_radius_ = 0.0f; // Radio mayor R (del centro al tubo) + float minor_radius_ = 0.0f; // Radio menor r (grosor del tubo) + 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 "TORUS"; } + float getScaleFactor(float screen_height) const override; +};