#include <iostream>
#include <cmath>
#include <SFML/Graphics.hpp> // Asegúrate de tener SFML instalada
class Pendulum {
private:
float length; // Longitud del péndulo
float angle; // Ángulo en radianes
float angularVelocity; // Velocidad angular
float angularAcceleration; // Aceleración angular
float gravity; // Gravedad
public:
Pendulum(float len, float ang) : length(len), angle(ang), angularVelocity(0), angularAcceleration(0), gravity(9.81) {}
void update() {
// Ecuación del péndulo simple
angularAcceleration = (-1 * gravity / length) * sin(angle);
angularVelocity += angularAcceleration;
angle += angularVelocity;
// Damping para simular la resistencia del aire
angularVelocity *= 0.99;
}
void draw(sf::RenderWindow &window) {
float x = length * sin(angle);
float y = length * cos(angle);
sf::Vertex line[] = {
sf::Vertex(sf::Vector2f(400, 0)), // Punto de anclaje
sf::Vertex(sf::Vector2f(400 + x * 100, y * 100)) // Punto del péndulo
};
window.draw(line, 2, sf::Lines);
}
};
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Pendulum simulation");
Pendulum pendulum(1.0f, M_PI / 4); // Longitud de 1.0 m y ángulo inicial de 45 grados
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
pendulum.update();
window.clear();
pendulum.draw(window);
window.display();
}
return 0;
}