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
This commit is contained in:
parent
f3510fb281
commit
5ba41b6fbf
|
@ -189,8 +189,6 @@ inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const {
|
||||||
|
|
||||||
// Set the interpolation factor of the body
|
// Set the interpolation factor of the body
|
||||||
inline void RigidBody::setInterpolationFactor(double factor) {
|
inline void RigidBody::setInterpolationFactor(double factor) {
|
||||||
assert(factor >= 0.0 && factor <= 1.0);
|
|
||||||
|
|
||||||
// Set the factor
|
// Set the factor
|
||||||
interpolationFactor = factor;
|
interpolationFactor = factor;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ PhysicsEngine::~PhysicsEngine() {
|
||||||
|
|
||||||
// Update the physics simulation
|
// Update the physics simulation
|
||||||
void PhysicsEngine::update() throw (logic_error) {
|
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
|
// Check that the timer is running
|
||||||
if (timer.getIsRunning()) {
|
if (timer.getIsRunning()) {
|
||||||
|
@ -84,6 +84,9 @@ void PhysicsEngine::update() throw (logic_error) {
|
||||||
// Clear the added and removed bodies from last update() method call
|
// Clear the added and removed bodies from last update() method call
|
||||||
world->clearAddedAndRemovedBodies();
|
world->clearAddedAndRemovedBodies();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Compute and set the interpolation factor to all the bodies
|
||||||
|
setInterpolationFactorToAllBodies();
|
||||||
}
|
}
|
||||||
else { // Call to update() but the timer is not running
|
else { // Call to update() but the timer is not running
|
||||||
// Throw an exception
|
// 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
|
// If the body state has changed, we have to update some informations in the rigid body
|
||||||
rigidBody->update();
|
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
|
// Update the position and orientation of a body
|
||||||
// Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new
|
// Use the Semi-Implicit Euler (Sympletic Euler) method to compute the new position and the new
|
||||||
// orientation of the body
|
// 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();
|
double dt = timer.getTimeStep();
|
||||||
|
|
||||||
RigidBody* rigidBody = dynamic_cast<RigidBody*>(body);
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(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);
|
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<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
||||||
|
|
||||||
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
||||||
|
assert(rigidBody);
|
||||||
|
|
||||||
|
rigidBody->setInterpolationFactor(factor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Apply the gravity force to all bodies of the physics world
|
// Apply the gravity force to all bodies of the physics world
|
||||||
void PhysicsEngine::applyGravity() {
|
void PhysicsEngine::applyGravity() {
|
||||||
|
|
||||||
|
|
|
@ -45,8 +45,10 @@ class PhysicsEngine {
|
||||||
|
|
||||||
void updateAllBodiesMotion(); // Compute the motion of all bodies and update their positions and orientations
|
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 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
|
void applyGravity(); // Apply the gravity force to all bodies
|
||||||
public :
|
|
||||||
|
public :
|
||||||
PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument); // Constructor
|
PhysicsEngine(PhysicsWorld* world, double timeStep) throw (std::invalid_argument); // Constructor
|
||||||
~PhysicsEngine(); // Destructor
|
~PhysicsEngine(); // Destructor
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ class Timer {
|
||||||
bool isPossibleToTakeStep() const; // True if it's possible to take a new step
|
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 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
|
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 --- //
|
// --- Inline functions --- //
|
||||||
|
@ -107,7 +107,7 @@ inline void Timer::stop() {
|
||||||
|
|
||||||
// True if it's possible to take a new step
|
// True if it's possible to take a new step
|
||||||
inline bool Timer::isPossibleToTakeStep() const {
|
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
|
// 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;
|
accumulator -= timeStep;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute and return the interpolation factor between two body states
|
// Compute the interpolation factor
|
||||||
inline double Timer::getInterpolationFactor() const {
|
inline double Timer::computeInterpolationFactor() {
|
||||||
// Compute and return the interpolation factor
|
|
||||||
return (accumulator / timeStep);
|
return (accumulator / timeStep);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user