From 5ba41b6fbf0cec260e399e8b46b4c4f8fb9595ea Mon Sep 17 00:00:00 2001 From: "chappuis.daniel" Date: Wed, 1 Sep 2010 20:59:35 +0000 Subject: [PATCH] Correction of a bug because the interpolation factor was not computed at the right place git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@384 92aac97c-a6ce-11dd-a772-7fcde58d38e6 --- sources/reactphysics3d/body/RigidBody.h | 2 -- .../reactphysics3d/engine/PhysicsEngine.cpp | 27 ++++++++++++++----- sources/reactphysics3d/engine/PhysicsEngine.h | 4 ++- sources/reactphysics3d/engine/Timer.h | 9 +++---- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/sources/reactphysics3d/body/RigidBody.h b/sources/reactphysics3d/body/RigidBody.h index 467faeab..e2f2b215 100644 --- a/sources/reactphysics3d/body/RigidBody.h +++ b/sources/reactphysics3d/body/RigidBody.h @@ -189,8 +189,6 @@ inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const { // Set the interpolation factor of the body inline void RigidBody::setInterpolationFactor(double factor) { - assert(factor >= 0.0 && factor <= 1.0); - // Set the factor interpolationFactor = factor; } diff --git a/sources/reactphysics3d/engine/PhysicsEngine.cpp b/sources/reactphysics3d/engine/PhysicsEngine.cpp index 0a4a3b19..ee09800b 100644 --- a/sources/reactphysics3d/engine/PhysicsEngine.cpp +++ b/sources/reactphysics3d/engine/PhysicsEngine.cpp @@ -47,7 +47,7 @@ PhysicsEngine::~PhysicsEngine() { // Update the physics simulation void PhysicsEngine::update() throw (logic_error) { - bool existCollision = false; // TODO : Delete this if we don't need it + bool existCollision = false; // Check that the timer is running if (timer.getIsRunning()) { @@ -84,6 +84,9 @@ void PhysicsEngine::update() throw (logic_error) { // Clear the added and removed bodies from last update() method call world->clearAddedAndRemovedBodies(); } + + // Compute and set the interpolation factor to all the bodies + setInterpolationFactorToAllBodies(); } else { // Call to update() but the timer is not running // Throw an exception @@ -138,17 +141,13 @@ void PhysicsEngine::updateAllBodiesMotion() { // If the body state has changed, we have to update some informations in the rigid body rigidBody->update(); } - - // Update the interpolation factor of the rigid body - // This one will be used to compute the interpolated state - rigidBody->setInterpolationFactor(timer.getInterpolationFactor()); } } // Update the position and orientation of a body // Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new // orientation of the body - void PhysicsEngine::updatePositionAndOrientationOfBody(Body* body, const Vector3D& newLinVelocity, const Vector3D& newAngVelocity) { +void PhysicsEngine::updatePositionAndOrientationOfBody(Body* body, const Vector3D& newLinVelocity, const Vector3D& newAngVelocity) { double dt = timer.getTimeStep(); RigidBody* rigidBody = dynamic_cast(body); @@ -169,6 +168,22 @@ void PhysicsEngine::updateAllBodiesMotion() { rigidBody->setOrientation(rigidBody->getOrientation() + Quaternion(newAngVelocity.getX(), newAngVelocity.getY(), newAngVelocity.getZ(), 0) * rigidBody->getOrientation() * 0.5 * dt); } +// Compute and set the interpolation factor to all bodies +void PhysicsEngine::setInterpolationFactorToAllBodies() { + // Compute the interpolation factor + double factor = timer.computeInterpolationFactor(); + assert(factor >= 0.0 && factor <= 1.0); + + // Set the factor to all bodies + for (vector::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) { + + RigidBody* rigidBody = dynamic_cast(*it); + assert(rigidBody); + + rigidBody->setInterpolationFactor(factor); + } +} + // Apply the gravity force to all bodies of the physics world void PhysicsEngine::applyGravity() { diff --git a/sources/reactphysics3d/engine/PhysicsEngine.h b/sources/reactphysics3d/engine/PhysicsEngine.h index 7af2999d..9b3a1e8d 100644 --- a/sources/reactphysics3d/engine/PhysicsEngine.h +++ b/sources/reactphysics3d/engine/PhysicsEngine.h @@ -45,8 +45,10 @@ class PhysicsEngine { void updateAllBodiesMotion(); // Compute the motion of all bodies and update their positions and orientations void updatePositionAndOrientationOfBody(Body* body, const Vector3D& newLinVelocity, const Vector3D& newAngVelocity); // Update the position and orientation of a body + void setInterpolationFactorToAllBodies(); // Compute and set the interpolation factor to all bodies void applyGravity(); // Apply the gravity force to all bodies - public : + +public : PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument); // Constructor ~PhysicsEngine(); // Destructor diff --git a/sources/reactphysics3d/engine/Timer.h b/sources/reactphysics3d/engine/Timer.h index 4b47f71e..8cbcdfcd 100644 --- a/sources/reactphysics3d/engine/Timer.h +++ b/sources/reactphysics3d/engine/Timer.h @@ -56,7 +56,7 @@ class Timer { bool isPossibleToTakeStep() const; // True if it's possible to take a new step void update(); // Compute the time since the last update() call and add it to the accumulator void nextStep(); // Take a new step => update the timer by adding the timeStep value to the current time - double getInterpolationFactor() const; // Compute and return the interpolation factor between two body states + double computeInterpolationFactor(); // Compute the interpolation factor }; // --- Inline functions --- // @@ -107,7 +107,7 @@ inline void Timer::stop() { // True if it's possible to take a new step inline bool Timer::isPossibleToTakeStep() const { - return accumulator >= timeStep; + return (accumulator >= timeStep); } // Take a new step => update the timer by adding the timeStep value to the current time @@ -121,9 +121,8 @@ inline void Timer::nextStep() { accumulator -= timeStep; } -// Compute and return the interpolation factor between two body states -inline double Timer::getInterpolationFactor() const { - // Compute and return the interpolation factor +// Compute the interpolation factor +inline double Timer::computeInterpolationFactor() { return (accumulator / timeStep); }