diff --git a/include/physics_object.hpp b/include/physics_object.hpp index 0436ae5..ce95211 100644 --- a/include/physics_object.hpp +++ b/include/physics_object.hpp @@ -8,19 +8,28 @@ class PhysicsObject : public Object { protected: - sf::Vector2f velocity; float mass; + sf::Vector2f acceleration; + sf::VertexArray accelLine; + sf::Vector2f velocity; + sf::VertexArray velLine; /** * Calcule les forces appliquées à l'objet */ virtual sf::Vector2f getForces(State state); - static constexpr float GRAVITY = 275; + static constexpr float GRAVITY = 20; public: PhysicsObject(float x, float y) : - Object(x, y), mass(1) {} + Object(x, y), mass(1), accelLine(sf::LinesStrip, 2), + velLine(sf::LinesStrip, 2) {} + + /** + * Dessine l'objet dans la fenêtre donnée + */ + virtual void draw(sf::RenderWindow& window); /** * Met à jour la physique de l'objet juste avant le dessin d'une frame diff --git a/src/ball.cpp b/src/ball.cpp index cd0a9b2..c20cdbe 100644 --- a/src/ball.cpp +++ b/src/ball.cpp @@ -1,6 +1,8 @@ #include "ball.hpp" void Ball::draw(sf::RenderWindow& window) { + PhysicsObject::draw(window); + // chargement de la texture de test if (!texture.loadFromFile("./res/ball.png")) { // erreur diff --git a/src/physics_object.cpp b/src/physics_object.cpp index 280da42..32f1c36 100644 --- a/src/physics_object.cpp +++ b/src/physics_object.cpp @@ -1,9 +1,27 @@ #include "physics_object.hpp" +void PhysicsObject::draw(sf::RenderWindow& window) { + velLine[0].position = position; + velLine[0].color = sf::Color::Green; + velLine[1].position = position + velocity * 1.f; + velLine[1].color = sf::Color::Green; + + accelLine[0].position = position; + accelLine[0].color = sf::Color::Red; + accelLine[1].position = position + acceleration * 1.f; + accelLine[1].color = sf::Color::Red; + + window.draw(velLine); + window.draw(accelLine); +} + void PhysicsObject::update(State state) { - sf::Vector2f acceleration = getForces(state) / mass; - velocity += acceleration * state.delta; + // intégration de la vitesse dans la position position += velocity * state.delta; + + // intégration des forces appliquées sur l'objet dans la vitesse + acceleration = getForces(state) / mass; + velocity += acceleration * state.delta; } sf::Vector2f PhysicsObject::getVelocity() {