Mise en cache de l'inverse des masses

This commit is contained in:
Mattéo Delabre 2016-03-15 22:09:40 +01:00
parent a99f1ccd0a
commit b2e3c5f6cc
2 changed files with 22 additions and 1 deletions

View File

@ -18,6 +18,7 @@ private:
sf::VertexArray velocityLine; sf::VertexArray velocityLine;
float mass; float mass;
float inv_mass;
float charge; float charge;
float restitution; float restitution;
int layer; int layer;
@ -80,6 +81,11 @@ public:
*/ */
float getMass(); float getMass();
/**
* Récupère l'inverse de la masse de l'objet (en cache)
*/
float getMassInvert();
/** /**
* Modifie la masse de l'objet * Modifie la masse de l'objet
*/ */

View File

@ -5,7 +5,7 @@ Object::Object(float x, float y) :
acceleration(0, 0), velocity(0, 0), position(x, y), acceleration(0, 0), velocity(0, 0), position(x, y),
accelerationLine(sf::Lines, 2), accelerationLine(sf::Lines, 2),
velocityLine(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 Object::getForces(EngineState& state) {
sf::Vector2f forces(0, 0); sf::Vector2f forces(0, 0);
@ -72,8 +72,23 @@ float Object::getMass() {
return mass; 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) { void Object::setMass(float set_mass) {
mass = set_mass; mass = set_mass;
inv_mass = -1.f;
} }
float Object::getCharge() { float Object::getCharge() {