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
This commit is contained in:
chappuis.daniel 2010-09-05 07:56:24 +00:00
parent 5ba41b6fbf
commit b9ddb0e2f1
3 changed files with 70 additions and 80 deletions

View File

@ -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

View File

@ -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<Body*>::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<Body*>::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<Body*>::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<Constraint*>::iterator it = constraints.begin(); it != constraints.end(); it++) {
delete *it;
}
constraints.clear();
}

View File

@ -22,7 +22,7 @@
// Libraries
#include <vector>
#include <stdexcept>
#include <algorithm>
#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<Constraint*>::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list
std::vector<Constraint*>::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list
std::vector<Body*>::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world
std::vector<Body*>::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world
std::vector<Body*>& getAddedBodies(); // Return the added bodies since last update of the physics engine
std::vector<Body*>& 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<Constraint*>::iterator getConstraintsBeginIterator(); // Return a start iterator on the constraint list
std::vector<Constraint*>::iterator getConstraintsEndIterator(); // Return a end iterator on the constraint list
std::vector<Body*>::iterator getBodiesBeginIterator(); // Return an iterator to the beginning of the bodies of the physics world
std::vector<Body*>::iterator getBodiesEndIterator(); // Return an iterator to the end of the bodies of the physics world
std::vector<Body*>& getAddedBodies(); // Return the added bodies since last update of the physics engine
std::vector<Body*>& 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<Body*>::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<Body*>::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<Constraint*>::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() {