From b2e3c5f6cc915bd96336583c8f79f3067ac74bbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matt=C3=A9o=20Delabre?= Date: Tue, 15 Mar 2016 22:09:40 +0100 Subject: [PATCH] Mise en cache de l'inverse des masses --- include/object.hpp | 6 ++++++ src/object.cpp | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/object.hpp b/include/object.hpp index c037648..2434c08 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -18,6 +18,7 @@ private: sf::VertexArray velocityLine; float mass; + float inv_mass; float charge; float restitution; int layer; @@ -80,6 +81,11 @@ public: */ float getMass(); + /** + * Récupère l'inverse de la masse de l'objet (en cache) + */ + float getMassInvert(); + /** * Modifie la masse de l'objet */ diff --git a/src/object.cpp b/src/object.cpp index efbff84..3e41c3f 100644 --- a/src/object.cpp +++ b/src/object.cpp @@ -5,7 +5,7 @@ Object::Object(float x, float y) : acceleration(0, 0), velocity(0, 0), position(x, y), accelerationLine(sf::Lines, 2), velocityLine(sf::Lines, 2), - mass(1.f), charge(0.f), restitution(0.f), layer(10) {} + mass(1.f), inv_mass(1.f), charge(0.f), restitution(0.f), layer(10) {} sf::Vector2f Object::getForces(EngineState& state) { sf::Vector2f forces(0, 0); @@ -72,8 +72,23 @@ float Object::getMass() { return mass; } +float Object::getMassInvert() { + if (inv_mass >= 0) { + return inv_mass; + } + + if (mass == 0) { + inv_mass = 0; + return inv_mass; + } + + inv_mass = 1 / mass; + return inv_mass; +} + void Object::setMass(float set_mass) { mass = set_mass; + inv_mass = -1.f; } float Object::getCharge() {