Implementar figura CYLINDER (cilindro 3D) - Tecla Y

- Nueva clase CylinderShape con ecuaciones paramétricas
- Distribución uniforme en anillos y circunferencia
- Rotación simple en eje Y
- Dimensiones: radius=0.25, height=0.5 (proporción altura)
- Compatible con física spring-damper y z-sorting

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-10-04 06:38:58 +02:00
parent 8b642f6903
commit ac3309ffd1
4 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,77 @@
#include "cylinder_shape.h"
#include "../defines.h"
#include <cmath>
void CylinderShape::generatePoints(int num_points, float screen_width, float screen_height) {
num_points_ = num_points;
radius_ = screen_height * CYLINDER_RADIUS_FACTOR;
height_ = screen_height * CYLINDER_HEIGHT_FACTOR;
// Las posiciones 3D se calculan en getPoint3D() usando ecuaciones paramétricas del cilindro
}
void CylinderShape::update(float delta_time, float screen_width, float screen_height) {
// Recalcular dimensiones por si cambió resolución (F4)
radius_ = screen_height * CYLINDER_RADIUS_FACTOR;
height_ = screen_height * CYLINDER_HEIGHT_FACTOR;
// Actualizar ángulo de rotación en eje Y
angle_y_ += CYLINDER_ROTATION_SPEED_Y * delta_time;
}
void CylinderShape::getPoint3D(int index, float& x, float& y, float& z) const {
// Distribuir puntos uniformemente en la superficie del cilindro
// Calcular número de anillos (altura) y puntos por anillo (circunferencia)
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 (ángulo) y v (altura) 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ámetro u (ángulo alrededor del cilindro): [0, 2π]
float u = (static_cast<float>(point_in_ring) / static_cast<float>(points_per_ring)) * 2.0f * PI;
// Parámetro v (altura normalizada): [-1, 1]
float v = (static_cast<float>(ring) / static_cast<float>(num_rings - 1)) * 2.0f - 1.0f;
if (num_rings == 1) v = 0.0f;
// Ecuaciones paramétricas del cilindro
// x = radius * cos(u)
// y = height * v
// z = radius * sin(u)
float x_base = radius_ * cosf(u);
float y_base = (height_ * 0.5f) * v; // Centrar verticalmente
float z_base = radius_ * sinf(u);
// Aplicar rotación en eje Y
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 rotadas
x = x_rot;
y = y_base;
z = z_rot;
}
float CylinderShape::getScaleFactor(float screen_height) const {
// Factor de escala para física: proporcional a la dimensión mayor (altura)
// Altura base = 120px (0.5 * 240px en resolución 320x240)
const float BASE_HEIGHT = 120.0f;
float current_height = screen_height * CYLINDER_HEIGHT_FACTOR;
return current_height / BASE_HEIGHT;
}