From b9ddb0e2f15e5be3cd4d2b795adec444ab50e75b Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Sun, 5 Sep 2010 07:56:24 +0000 Subject: [PATCH] Modification of the way the memory is free in the PhysicsWorld git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@385 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- sources/reactphysics3d/body/RigidBody.h | 1 - .../reactphysics3d/engine/PhysicsWorld.cpp | 76 +++---------------- sources/reactphysics3d/engine/PhysicsWorld.h | 73 ++++++++++++++---- 3 files changed, 70 insertions(+), 80 deletions(-) diff --git a/sources/reactphysics3d/body/RigidBody.h b/sources/reactphysics3d/body/RigidBody.h index e2f2b215..d475a603 100644 --- a/sources/reactphysics3d/body/RigidBody.h +++ b/sources/reactphysics3d/body/RigidBody.h @@ -48,7 +48,6 @@ class RigidBody : public Body { Matrix3x3 inertiaTensorLocal; // Local inertia tensor of the body (in body coordinates) Matrix3x3 inertiaTensorLocalInverse; // Inverse of the inertia tensor of the body (in body coordinates) double massInverse; // Inverse of the mass of the body - double interpolationFactor; // Interpolation factor used for the state interpolation double restitution; // Coefficient of restitution (between 0 and 1), 1 for a very boucing body diff --git a/sources/reactphysics3d/engine/PhysicsWorld.cpp b/sources/reactphysics3d/engine/PhysicsWorld.cpp index fcb6d61e..53f3f96d 100644 --- a/sources/reactphysics3d/engine/PhysicsWorld.cpp +++ b/sources/reactphysics3d/engine/PhysicsWorld.cpp @@ -33,72 +33,8 @@ PhysicsWorld::PhysicsWorld(const Vector3D& gravity) // Destructor PhysicsWorld::~PhysicsWorld() { - -} - -// Add a body to the physics world -void PhysicsWorld::addBody(Body* body) throw(invalid_argument) { - // Check if the body pointer is not null - if (body != 0) { - // Check if the body pointer isn't already in the bodyList - for(vector::iterator it = bodies.begin(); it != bodies.end(); ++it) { - if (*it == body) { - // The body is already in the bodyList, therefore we throw an exception - throw invalid_argument("Exception in PhysicsWorld::addBody() : The argument body is already in the PhysicsWorld"); - } - } - - // The body isn't already in the bodyList, therefore we add it to the list - bodies.push_back(body); - addedBodies.push_back(body); - vector::iterator it = find(removedBodies.begin(), removedBodies.end(), body); - if (it != removedBodies.end()) { - removedBodies.erase(it); - } - } - else { - // Throw an exception - throw invalid_argument("Exception in PhysicsWorld::addBody() : The argument pointer cannot be NULL"); - } -} - -// Remove a body from the physics world -void PhysicsWorld::removeBody(Body const* const body) throw(invalid_argument) { - // Check if the body pointer is not null - if (body != 0) { - // Look for the body to remove in the bodyList - vector::iterator it = bodies.begin(); - while(it != bodies.end() && *it != body) { - // Increment the iterator - ++it; - } - - // If we have found the body to remove in the bodyList - if (*it == body) { - // Remove the body - bodies.erase(it); - addedBodies.erase(it); - removedBodies.push_back(*it); - } else { - // The body is not in the bodyList, therfore we throw an exception - throw invalid_argument("Exception in PhysicsWorld::removeBody() : The argument body to remove is not in the PhysicsWorld"); - } - } - else { - // Throw an exception - throw invalid_argument("Exception in PhysicsWorld::removeBody() : The argument pointer cannot be NULL"); - } -} - -// Add a constraint into the physics world -void PhysicsWorld::addConstraint(Constraint* constraint) throw(invalid_argument) { - assert(constraint != 0); - constraints.push_back(constraint); -} - -// Remove a constraint -void PhysicsWorld::removeConstraint(Constraint* constraint) throw(invalid_argument) { - // TODO : Implement this method + // Remove and free the memory of all constraints + removeAllConstraints(); } // Remove all collision contacts constraints @@ -121,3 +57,11 @@ void PhysicsWorld::removeAllContactConstraints() { } } +// Remove all constraints in the physics world and also delete them (free their memory) +void PhysicsWorld::removeAllConstraints() { + for (vector::iterator it = constraints.begin(); it != constraints.end(); it++) { + delete *it; + } + constraints.clear(); +} + diff --git a/sources/reactphysics3d/engine/PhysicsWorld.h b/sources/reactphysics3d/engine/PhysicsWorld.h index 3378318e..20d5325d 100644 --- a/sources/reactphysics3d/engine/PhysicsWorld.h +++ b/sources/reactphysics3d/engine/PhysicsWorld.h @@ -22,7 +22,7 @@ // Libraries #include -#include +#include #include "../mathematics/mathematics.h" #include "../body/Body.h" #include "../constraint/Constraint.h" @@ -51,24 +51,71 @@ class PhysicsWorld { PhysicsWorld(const Vector3D& gravity); // Constructor virtual ~PhysicsWorld(); // Destructor - void addBody(Body* body) throw(std::invalid_argument); // Add a body to the physics world - void removeBody(Body const* const body) throw(std::invalid_argument); // Remove a body from the physics world + void addBody(Body* body); // Add a body to the physics world + void removeBody(Body const* const body); // Remove a body from the physics world void clearAddedAndRemovedBodies(); // Clear the addedBodies and removedBodies sets Vector3D getGravity() const; // Return the gravity vector of the world bool getIsGravityOn() const; // Return if the gravity is on void setIsGratityOn(bool isGravityOn); // Set the isGravityOn attribute - void addConstraint(Constraint* constraint) throw(std::invalid_argument); // Add a constraint - void removeConstraint(Constraint* constraint) throw(std::invalid_argument); // Remove a constraint - void removeAllContactConstraints(); // Remove all collision contacts constraints - std::vector::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list - std::vector::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list - std::vector::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world - std::vector::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world - std::vector& getAddedBodies(); // Return the added bodies since last update of the physics engine - std::vector& getRemovedBodies(); // Retrun the removed bodies since last update of the physics engine + void addConstraint(Constraint* constraint); // Add a constraint + void removeConstraint(Constraint* constraint); // Remove a constraint + void removeAllContactConstraints(); // Remove all collision contacts constraints + void removeAllConstraints(); // Remove all constraints and delete them (free their memory) + std::vector::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list + std::vector::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list + std::vector::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world + std::vector::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world + std::vector& getAddedBodies(); // Return the added bodies since last update of the physics engine + std::vector& getRemovedBodies(); // Retrun the removed bodies since last update of the physics engine }; -// --- Inline functions --- // +// Add a body to the physics world +inline void PhysicsWorld::addBody(Body* body) { + std::vector::iterator it; + + assert(body); + it = std::find(bodies.begin(), bodies.end(), body); + assert(it == bodies.end()); + + // The body isn't already in the bodyList, therefore we add it to the list + bodies.push_back(body); + addedBodies.push_back(body); + it = std::find(removedBodies.begin(), removedBodies.end(), body); + if (it != removedBodies.end()) { + removedBodies.erase(it); + } +} + +// Remove a body from the physics world +inline void PhysicsWorld::removeBody(Body const* const body) { + std::vector::iterator it; + + assert(body); + it = std::find(bodies.begin(), bodies.end(), body); + assert(*it == body); + + // Remove the body + bodies.erase(it); + addedBodies.erase(it); + removedBodies.push_back(*it); +} + +// Add a constraint into the physics world +inline void PhysicsWorld::addConstraint(Constraint* constraint) { + assert(constraint != 0); + constraints.push_back(constraint); +} + +// Remove a constraint and free its memory +inline void PhysicsWorld::removeConstraint(Constraint* constraint) { + std::vector::iterator it; + + assert(constraint); + it = std::find(constraints.begin(), constraints.end(), constraint); + assert(*it == constraint); + delete *it; + constraints.erase(it); +} // Clear the addedBodies and removedBodies sets inline void PhysicsWorld::clearAddedAndRemovedBodies() {