Files
orni_attack/source/core/rendering/primitives.cpp
2025-12-03 09:42:45 +01:00

67 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// primitives.cpp - Implementació de funcions geomètriques
// © 1999 Visente i Sergi (versió Pascal)
// © 2025 Port a C++20 amb SDL3
//
// ==============================================================================
// DEPRECATED: Use Shape system instead (.shp files + ShapeLoader)
// ==============================================================================
#include "primitives.hpp"
#include <cmath>
#include "core/defaults.hpp"
float modul(const Punt& p) {
// Càlcul de la magnitud d'un vector: sqrt(x² + y²)
return std::sqrt(p.x * p.x + p.y * p.y);
}
void diferencia(const Punt& o, const Punt& d, Punt& p) {
// Resta de vectors (origen - destí)
p.x = o.x - d.x;
p.y = o.y - d.y;
}
int distancia(const Punt& o, const Punt& d) {
// Distància entre dos punts
Punt p;
diferencia(o, d, p);
return static_cast<int>(std::round(modul(p)));
}
float angle_punt(const Punt& p) {
// Càlcul de l'angle d'un punt (arctan)
if (p.y != 0) {
return std::atan(p.x / p.y);
}
return 0.0f;
}
void crear_poligon_regular(Poligon& pol, uint8_t n, float r) {
// Crear un polígon regular amb n costats i radi r
// Distribueix els punts uniformement al voltant d'un cercle
float interval = 2.0f * Defaults::Math::PI / n;
float act = 0.0f;
for (uint8_t i = 0; i < n; i++) {
pol.ipuntx[i].r = r;
pol.ipuntx[i].angle = act;
act += interval;
}
// Inicialitzar propietats del polígon
pol.centre.x = 320.0f;
pol.centre.y = 200.0f;
pol.angle = 0.0f;
// Convertir velocitat de px/frame a px/s: 2 px/frame × 20 FPS = 40 px/s
pol.velocitat = Defaults::Physics::ENEMY_SPEED * 20.0f;
pol.n = n;
// Convertir rotació de rad/frame a rad/s: 0.0785 rad/frame × 20 FPS = 1.57
// rad/s (~90°/s)
pol.drotacio = 0.078539816f * 20.0f;
pol.rotacio = 0.0f;
pol.esta = true;
}