diff --git a/sources/reactphysics3d/engine/DynamicEngine.cpp b/sources/reactphysics3d/engine/DynamicEngine.cpp index 20a13313..ff2238d5 100644 --- a/sources/reactphysics3d/engine/DynamicEngine.cpp +++ b/sources/reactphysics3d/engine/DynamicEngine.cpp @@ -24,9 +24,13 @@ using namespace reactphysics3d; // Constructor -DynamicEngine::DynamicEngine(DynamicWorld& world, const Time& timeStep) +DynamicEngine::DynamicEngine(DynamicWorld* world, const Time& timeStep) :PhysicsEngine(world, timeStep) { - + // Check if the pointer to the world is not NULL + if (world == 0) { + // Throw an exception + throw std::invalid_argument("Exception in PhysicsEngine constructor : World pointer cannot be NULL"); + } } // Copy-constructor @@ -52,30 +56,33 @@ void DynamicEngine::updateBodyState(RigidBody* const rigidBody) { // Update the physics simulation void DynamicEngine::update() { - // While the time accumulator is not empty - while(timer.getAccumulator() >= timer.getTimeStep().getValue()) { + // Check if the physics simulation is running + if (timer.getIsRunning()) { + // While the time accumulator is not empty + while(timer.getAccumulator() >= timer.getTimeStep().getValue()) { + // For each body in the dynamic world + for(std::vector::const_iterator it = world->getBodyListStartIterator(); it != world->getBodyListEndIterator(); ++it) { + // If the body is a RigidBody and if the rigid body motion is enabled + RigidBody* rigidBody = dynamic_cast(*it); + if (rigidBody && rigidBody->getIsMotionEnabled()) { + // Update the state of the rigid body + updateBodyState(rigidBody); + } + } + + // Update the timer + timer.update(); + } + // For each body in the dynamic world - for(std::vector::const_iterator it = world.getBodyListStartIterator(); it != world.getBodyListEndIterator(); ++it) { + for(std::vector::const_iterator it = world->getBodyListStartIterator(); it != world->getBodyListEndIterator(); ++it) { // If the body is a RigidBody and if the rigid body motion is enabled RigidBody* rigidBody = dynamic_cast(*it); if (rigidBody && rigidBody->getIsMotionEnabled()) { - // Update the state of the rigid body - updateBodyState(rigidBody); + // Update the interpolation factor of the rigid body + // This one will be used to compute the interpolated state + rigidBody->setInterpolationFactor(timer.getInterpolationFactor()); } } - - // Update the timer - timer.update(); - } - - // For each body in the dynamic world - for(std::vector::const_iterator it = world.getBodyListStartIterator(); it != world.getBodyListEndIterator(); ++it) { - // If the body is a RigidBody and if the rigid body motion is enabled - RigidBody* rigidBody = dynamic_cast(*it); - if (rigidBody && rigidBody->getIsMotionEnabled()) { - // Update the interpolation factor of the rigid body - // This one will be used to compute the interpolated state - rigidBody->setInterpolationFactor(timer.getInterpolationFactor()); - } } } diff --git a/sources/reactphysics3d/engine/DynamicEngine.h b/sources/reactphysics3d/engine/DynamicEngine.h index 4fda081d..8c6f5bb1 100644 --- a/sources/reactphysics3d/engine/DynamicEngine.h +++ b/sources/reactphysics3d/engine/DynamicEngine.h @@ -46,7 +46,7 @@ class DynamicEngine : public PhysicsEngine { void updateBodyState(RigidBody* const rigidBody); // Update the state of a rigid body public : - DynamicEngine(DynamicWorld& world, const Time& timeStep); // Constructor + DynamicEngine(DynamicWorld* world, const Time& timeStep); // Constructor DynamicEngine(const DynamicEngine& engine); // Copy-constructor virtual ~DynamicEngine(); // Destructor diff --git a/sources/reactphysics3d/engine/PhysicsEngine.cpp b/sources/reactphysics3d/engine/PhysicsEngine.cpp index b5293d34..361ce365 100644 --- a/sources/reactphysics3d/engine/PhysicsEngine.cpp +++ b/sources/reactphysics3d/engine/PhysicsEngine.cpp @@ -24,9 +24,13 @@ using namespace reactphysics3d; // Constructor -PhysicsEngine::PhysicsEngine(PhysicsWorld& world, const Time& timeStep) +PhysicsEngine::PhysicsEngine(PhysicsWorld* world, const Time& timeStep) throw (std::invalid_argument) : world(world), timer(Time(0.0), timeStep) { - + // Check if the pointer to the world is not NULL + if (world == 0) { + // Throw an exception + throw std::invalid_argument("Exception in PhysicsEngine constructor : World pointer cannot be NULL"); + } } // Copy-constructor diff --git a/sources/reactphysics3d/engine/PhysicsEngine.h b/sources/reactphysics3d/engine/PhysicsEngine.h index c1d36172..2759ddad 100644 --- a/sources/reactphysics3d/engine/PhysicsEngine.h +++ b/sources/reactphysics3d/engine/PhysicsEngine.h @@ -35,17 +35,42 @@ namespace reactphysics3d { */ class PhysicsEngine { protected : - PhysicsWorld world; // Physics world of the physics engine + PhysicsWorld* world; // Pointer to the physics world of the physics engine Timer timer; // Timer of the physics engine public : - PhysicsEngine(PhysicsWorld& world, const Time& timeStep); // Constructor - PhysicsEngine(const PhysicsEngine& engine); // Copy-constructor - virtual ~PhysicsEngine(); // Destructor + PhysicsEngine(PhysicsWorld* world, const Time& timeStep) throw (std::invalid_argument); // Constructor + PhysicsEngine(const PhysicsEngine& engine); // Copy-constructor + virtual ~PhysicsEngine(); // Destructor - virtual void update()=0; // Update the physics simulation + virtual void start(); // Start the physics simulation + virtual void stop(); // Stop the physics simulation + virtual void update()=0; // Update the physics simulation + void initializeDisplayTime(const Time& displayTime); // Initialize the display time + void updateDisplayTime(const Time& newDisplayTime); // Update the display time }; +// --- Inline functions --- // + +// Start the physics simulation +inline void PhysicsEngine::start() { + timer.setIsRunning(true); +} + +inline void PhysicsEngine::stop() { + // TODO : Implement this method +} + +// Initialize the display time +inline void PhysicsEngine::initializeDisplayTime(const Time& displayTime) { + timer.setCurrentDisplayTime(displayTime); +} + +// Update the display time +inline void PhysicsEngine::updateDisplayTime(const Time& newDisplayTime) { + timer.updateDisplayTime(newDisplayTime); +} + } #endif diff --git a/sources/reactphysics3d/engine/Timer.cpp b/sources/reactphysics3d/engine/Timer.cpp index ae9199b4..fde22eff 100644 --- a/sources/reactphysics3d/engine/Timer.cpp +++ b/sources/reactphysics3d/engine/Timer.cpp @@ -30,7 +30,6 @@ Timer::Timer(const Time& initialTime, const Time& timeStep) throw(std::invalid_a if (timeStep.getValue() != 0.0) { accumulator = 0.0; isRunning = false; - } else { // We throw an exception diff --git a/sources/reactphysics3d/engine/Timer.h b/sources/reactphysics3d/engine/Timer.h index fc9882a7..5d8f0437 100644 --- a/sources/reactphysics3d/engine/Timer.h +++ b/sources/reactphysics3d/engine/Timer.h @@ -23,6 +23,7 @@ // Libraries #include "../physics/physics.h" #include + #include // Namespace ReactPhysics3D namespace reactphysics3d { @@ -52,7 +53,8 @@ class Timer { void setTime(const Time& time); // Set the current time bool getIsRunning() const; // Return if the timer is running void setIsRunning(bool isRunning); // Set if the timer is running - double getAccumulator() const; // Return the accumulator value + double getAccumulator() const; // Return the accumulator value + void setCurrentDisplayTime(const Time& displayTime); // Set the current display time void update(); // Update the timer double getInterpolationFactor() const; // Compute and return the interpolation factor between two body states @@ -103,6 +105,11 @@ inline double Timer::getAccumulator() const { return accumulator; } +// Set the current display time +inline void Timer::setCurrentDisplayTime(const Time& currentDisplayTime) { + this->currentDisplayTime = currentDisplayTime; +} + // Update the timer inline void Timer::update() { // Update the current time of the physics engine