From 50210b669ea583016f295ca7f884f9e06c8b5ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Sun, 13 Mar 2016 13:31:41 +0100 Subject: [PATCH] =?UTF-8?q?Dessin=20des=20vecteurs=20acc=C3=A9l=C3=A9ratio?= =?UTF-8?q?n=20et=20vitesse=20pour=20les=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/physics_object.hpp | 15 ++++++++++++--- src/ball.cpp | 2 ++ src/physics_object.cpp | 22 ++++++++++++++++++++-- 3 files changed, 34 insertions(+), 5 deletions(-) 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() {