diff --git a/sources/reactphysics3d/engine/CollisionWorld.cpp b/sources/reactphysics3d/engine/CollisionWorld.cpp index f6d917e6..eddcd5f9 100644 --- a/sources/reactphysics3d/engine/CollisionWorld.cpp +++ b/sources/reactphysics3d/engine/CollisionWorld.cpp @@ -19,6 +19,7 @@ // Libraries #include "CollisionWorld.h" +#include "../constraint/Contact.h" // We want to use the ReactPhysics3D namespace using namespace reactphysics3d; @@ -31,7 +32,10 @@ CollisionWorld::CollisionWorld(const Vector3D& gravity) // Destructor CollisionWorld::~CollisionWorld() { - + // Delete all the constraint + for (std::vector::iterator it = constraintList.begin(); it != constraintList.end(); ) { + delete (*it); + } } // Add a constraint @@ -43,3 +47,23 @@ void CollisionWorld::addConstraint(Constraint* constraint) throw(std::invalid_ar void CollisionWorld::removeConstraint(Constraint* constraint) throw(std::invalid_argument) { // TODO : Implement this method } + + // Remove all collision contacts constraints +void CollisionWorld::removeAllContactConstraints() { + // For all constraints + for (std::vector::iterator it = constraintList.begin(); it != constraintList.end(); ) { + + // Try a downcasting + Contact* contact = dynamic_cast(*it); + + // If the constraint is a contact + if (contact != 0) { + // Delete the contact + delete (*it); + it = constraintList.erase(it); + } + else { + ++it; + } + } +} diff --git a/sources/reactphysics3d/engine/CollisionWorld.h b/sources/reactphysics3d/engine/CollisionWorld.h index 7e4dc4e5..fce56dd2 100644 --- a/sources/reactphysics3d/engine/CollisionWorld.h +++ b/sources/reactphysics3d/engine/CollisionWorld.h @@ -47,6 +47,7 @@ class CollisionWorld : public DynamicWorld { 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::const_iterator getConstraintListStartIterator() const; // Return a start iterator on the constraint list std::vector::const_iterator getConstraintListEndIterator() const; // Return a end iterator on the constraint list }; diff --git a/sources/reactphysics3d/engine/DynamicEngine.cpp b/sources/reactphysics3d/engine/DynamicEngine.cpp index 16848350..e829c6a4 100644 --- a/sources/reactphysics3d/engine/DynamicEngine.cpp +++ b/sources/reactphysics3d/engine/DynamicEngine.cpp @@ -51,7 +51,7 @@ DynamicEngine::~DynamicEngine() { } // Update the state of a rigid body -void DynamicEngine::updateBodyState(RigidBody* const rigidBody) { +void DynamicEngine::updateBodyState(RigidBody* const rigidBody, const Time& timeStep) { // If the gravity force is on if(world->getIsGravityOn()) { @@ -63,12 +63,15 @@ void DynamicEngine::updateBodyState(RigidBody* const rigidBody) { rigidBody->updatePreviousBodyState(); // Integrate the current body state at time t to get the next state at time t + dt - integrationAlgorithm->integrate(rigidBody->getCurrentBodyState(), timer.getTime(), timer.getTimeStep()); + integrationAlgorithm->integrate(rigidBody->getCurrentBodyState(), timer.getTime(), timeStep); + + // If the body state has changed, we have to update some informations in the rigid body + rigidBody->update(); + } // Update the physics simulation void DynamicEngine::update() { - // Check if the physics simulation is running if (timer.getIsRunning()) { // While the time accumulator is not empty @@ -79,7 +82,7 @@ void DynamicEngine::update() { RigidBody* rigidBody = dynamic_cast(*it); if (rigidBody && rigidBody->getIsMotionEnabled()) { // Update the state of the rigid body - updateBodyState(rigidBody); + updateBodyState(rigidBody, timer.getTimeStep()); } } diff --git a/sources/reactphysics3d/engine/DynamicEngine.h b/sources/reactphysics3d/engine/DynamicEngine.h index 8744498c..92886e6e 100644 --- a/sources/reactphysics3d/engine/DynamicEngine.h +++ b/sources/reactphysics3d/engine/DynamicEngine.h @@ -43,14 +43,14 @@ class DynamicEngine : public PhysicsEngine { protected : IntegrationAlgorithm* integrationAlgorithm; // Integration algorithm used to solve differential equations of movement - void updateBodyState(RigidBody* const rigidBody); // Update the state of a rigid body + void updateBodyState(RigidBody* const rigidBody, const Time& timeStep); // Update the state of a rigid body public : DynamicEngine(DynamicWorld* world, const Time& timeStep); // Constructor DynamicEngine(const DynamicEngine& engine); // Copy-constructor virtual ~DynamicEngine(); // Destructor - void update(); // Update the physics simulation + virtual void update(); // Update the physics simulation }; } diff --git a/sources/reactphysics3d/engine/PhysicsEngine.h b/sources/reactphysics3d/engine/PhysicsEngine.h index 9cdddf22..550b969f 100644 --- a/sources/reactphysics3d/engine/PhysicsEngine.h +++ b/sources/reactphysics3d/engine/PhysicsEngine.h @@ -58,7 +58,7 @@ inline void PhysicsEngine::start() { } inline void PhysicsEngine::stop() { - // TODO : Implement this method + timer.setIsRunning(false); } // Initialize the display time diff --git a/sources/reactphysics3d/engine/Timer.h b/sources/reactphysics3d/engine/Timer.h index 04f8221b..8c61fe36 100644 --- a/sources/reactphysics3d/engine/Timer.h +++ b/sources/reactphysics3d/engine/Timer.h @@ -112,11 +112,14 @@ inline void Timer::setCurrentDisplayTime(const Time& currentDisplayTime) { // Update the timer inline void Timer::update() { - // Update the current time of the physics engine - time.setValue(time.getValue() + timeStep.getValue()); + // Check if the timer is running + if (isRunning) { + // Update the current time of the physics engine + time.setValue(time.getValue() + timeStep.getValue()); - // Update the accumulator value - accumulator -= timeStep.getValue(); + // Update the accumulator value + accumulator -= timeStep.getValue(); + } } // Compute and return the interpolation factor between two body states