2010-09-09 19:41:14 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
|
|
|
* Copyright (c) 2010 Daniel Chappuis *
|
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy *
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal *
|
|
|
|
* in the Software without restriction, including without limitation the rights *
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is *
|
|
|
|
* furnished to do so, subject to the following conditions: *
|
|
|
|
* *
|
|
|
|
* The above copyright notice and this permission notice shall be included in *
|
|
|
|
* all copies or substantial portions of the Software. *
|
|
|
|
* *
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN *
|
|
|
|
* THE SOFTWARE. *
|
|
|
|
********************************************************************************/
|
2009-02-10 16:05:57 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "PhysicsEngine.h"
|
|
|
|
|
|
|
|
// We want to use the ReactPhysics3D namespace
|
|
|
|
using namespace reactphysics3d;
|
2010-08-18 14:50:36 +00:00
|
|
|
using namespace std;
|
2009-02-10 16:05:57 +00:00
|
|
|
|
|
|
|
// Constructor
|
2010-08-18 14:50:36 +00:00
|
|
|
PhysicsEngine::PhysicsEngine(PhysicsWorld* world, double timeStep) throw (invalid_argument)
|
|
|
|
: world(world), timer(timeStep), collisionDetection(world), constraintSolver(world) {
|
2009-02-13 11:26:15 +00:00
|
|
|
// Check if the pointer to the world is not NULL
|
|
|
|
if (world == 0) {
|
|
|
|
// Throw an exception
|
2010-08-18 14:50:36 +00:00
|
|
|
throw invalid_argument("Error : The argument world to the PhysicsEngine constructor cannot be NULL");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the timeStep is positive
|
|
|
|
if (timeStep <= 0.0) {
|
|
|
|
// Throw an exception
|
|
|
|
throw invalid_argument("Error : The timeStep argument to the PhysicsEngine constructor have to be greater than zero");
|
2009-02-13 11:26:15 +00:00
|
|
|
}
|
2009-02-12 12:38:27 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 16:05:57 +00:00
|
|
|
// Destructor
|
|
|
|
PhysicsEngine::~PhysicsEngine() {
|
2010-07-19 19:35:35 +00:00
|
|
|
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Update the physics simulation
|
|
|
|
void PhysicsEngine::update() throw (logic_error) {
|
2010-09-01 20:59:35 +00:00
|
|
|
bool existCollision = false;
|
2010-02-11 11:31:49 +00:00
|
|
|
|
2010-08-18 14:50:36 +00:00
|
|
|
// Check that the timer is running
|
2010-02-11 11:31:49 +00:00
|
|
|
if (timer.getIsRunning()) {
|
2010-08-18 14:50:36 +00:00
|
|
|
|
|
|
|
// Compute the time since the last update() call and update the timer
|
|
|
|
timer.update();
|
|
|
|
|
2010-07-19 19:35:35 +00:00
|
|
|
// Apply the gravity force to all bodies
|
|
|
|
applyGravity();
|
|
|
|
|
2010-02-11 11:31:49 +00:00
|
|
|
// While the time accumulator is not empty
|
2010-08-18 14:50:36 +00:00
|
|
|
while(timer.isPossibleToTakeStep()) {
|
2010-07-19 19:35:35 +00:00
|
|
|
existCollision = false;
|
2010-08-27 09:21:33 +00:00
|
|
|
|
2010-07-19 19:35:35 +00:00
|
|
|
// Compute the collision detection
|
|
|
|
if (collisionDetection.computeCollisionDetection()) {
|
|
|
|
existCollision = true;
|
|
|
|
|
|
|
|
// Solve constraints
|
2010-07-28 11:19:00 +00:00
|
|
|
constraintSolver.solve(timer.getTimeStep());
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the timer
|
2010-08-18 14:50:36 +00:00
|
|
|
timer.nextStep();
|
2010-02-11 11:31:49 +00:00
|
|
|
|
2010-07-19 19:35:35 +00:00
|
|
|
// Update the position and orientation of each body
|
|
|
|
updateAllBodiesMotion();
|
2010-02-11 11:31:49 +00:00
|
|
|
|
2010-08-27 09:21:33 +00:00
|
|
|
// Cleanup of the constraint solver
|
2010-07-19 19:35:35 +00:00
|
|
|
if (existCollision) {
|
2010-08-27 09:21:33 +00:00
|
|
|
constraintSolver.cleanup();
|
2010-07-19 19:35:35 +00:00
|
|
|
}
|
2010-08-05 15:06:34 +00:00
|
|
|
|
|
|
|
// Clear the added and removed bodies from last update() method call
|
|
|
|
world->clearAddedAndRemovedBodies();
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
2010-09-01 20:59:35 +00:00
|
|
|
|
|
|
|
// Compute and set the interpolation factor to all the bodies
|
|
|
|
setInterpolationFactorToAllBodies();
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
2010-08-18 14:50:36 +00:00
|
|
|
else { // Call to update() but the timer is not running
|
|
|
|
// Throw an exception
|
|
|
|
throw logic_error("Error : The PhysicsEngine::start() method have to be called before calling PhysicsEngine::update()");
|
|
|
|
}
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:52:16 +00:00
|
|
|
// Compute the motion of all bodies and update their positions and orientations
|
|
|
|
// First this method compute the vector V2 = V_constraint + V_forces + V1 where
|
|
|
|
// V_constraint = dt * (M^-1 * J^T * lambda) and V_forces = dt * (M^-1 * F_ext)
|
|
|
|
// V2 is the final velocity after the timestep and V1 is the velocity before the
|
|
|
|
// timestep.
|
|
|
|
// After having computed the velocity V2, this method will update the position
|
|
|
|
// and orientation of each body.
|
|
|
|
// This method uses the semi-implicit Euler method to update the position and
|
|
|
|
// orientation of the body
|
|
|
|
void PhysicsEngine::updateAllBodiesMotion() {
|
2010-07-28 11:19:00 +00:00
|
|
|
double dt = timer.getTimeStep();
|
2010-07-05 20:52:16 +00:00
|
|
|
Vector3D newLinearVelocity;
|
|
|
|
Vector3D newAngularVelocity;
|
|
|
|
|
|
|
|
// For each body of thephysics world
|
2010-08-18 14:50:36 +00:00
|
|
|
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
|
|
|
assert(rigidBody);
|
|
|
|
|
|
|
|
// If the body is able to move
|
|
|
|
if (rigidBody->getIsMotionEnabled()) {
|
|
|
|
newLinearVelocity.setAllValues(0.0, 0.0, 0.0);
|
|
|
|
newAngularVelocity.setAllValues(0.0, 0.0, 0.0);
|
|
|
|
|
|
|
|
// If it's a constrained body
|
|
|
|
if (constraintSolver.isConstrainedBody(*it)) {
|
|
|
|
// Get the constrained linear and angular velocities from the constraint solver
|
|
|
|
newLinearVelocity = constraintSolver.getConstrainedLinearVelocityOfBody(*it);
|
|
|
|
newAngularVelocity = constraintSolver.getConstrainedAngularVelocityOfBody(*it);
|
|
|
|
}
|
2010-02-11 11:31:49 +00:00
|
|
|
|
2010-07-05 20:52:16 +00:00
|
|
|
// Compute V_forces = dt * (M^-1 * F_ext) which is the velocity of the body due to the
|
|
|
|
// external forces and torques.
|
2010-07-24 17:14:29 +00:00
|
|
|
newLinearVelocity = newLinearVelocity + dt * rigidBody->getMassInverse() * rigidBody->getExternalForce();
|
|
|
|
newAngularVelocity = newAngularVelocity + dt * rigidBody->getInertiaTensorInverseWorld() * rigidBody->getExternalTorque();
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
// Add the velocity V1 to the new velocity
|
2010-07-24 17:14:29 +00:00
|
|
|
newLinearVelocity = newLinearVelocity + rigidBody->getLinearVelocity();
|
|
|
|
newAngularVelocity = newAngularVelocity + rigidBody->getAngularVelocity();
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
// Update the position and the orientation of the body according to the new velocity
|
|
|
|
updatePositionAndOrientationOfBody(*it, newLinearVelocity, newAngularVelocity);
|
|
|
|
|
|
|
|
// If the body state has changed, we have to update some informations in the rigid body
|
|
|
|
rigidBody->update();
|
|
|
|
}
|
2010-02-11 11:31:49 +00:00
|
|
|
}
|
2010-07-05 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2010-09-01 20:59:35 +00:00
|
|
|
void PhysicsEngine::updatePositionAndOrientationOfBody(Body* body, const Vector3D& newLinVelocity, const Vector3D& newAngVelocity) {
|
2010-07-28 11:19:00 +00:00
|
|
|
double dt = timer.getTimeStep();
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(body);
|
|
|
|
assert(rigidBody);
|
2010-02-11 11:31:49 +00:00
|
|
|
|
2010-07-24 17:14:29 +00:00
|
|
|
// Update the old position and orientation of the body
|
|
|
|
rigidBody->updateOldPositionAndOrientation();
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
// Normalize the orientation quaternion
|
2010-07-24 17:14:29 +00:00
|
|
|
rigidBody->setOrientation(rigidBody->getOrientation().getUnit());
|
2009-02-10 16:05:57 +00:00
|
|
|
|
2010-07-05 20:52:16 +00:00
|
|
|
// Update the linear and angular velocity of the body
|
2010-07-24 17:14:29 +00:00
|
|
|
rigidBody->setLinearVelocity(newLinVelocity);
|
|
|
|
rigidBody->setAngularVelocity(newAngVelocity);
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
// Update the position and the orientation of the body
|
2010-07-24 17:14:29 +00:00
|
|
|
rigidBody->setPosition(rigidBody->getPosition() + newLinVelocity * dt);
|
|
|
|
rigidBody->setOrientation(rigidBody->getOrientation() + Quaternion(newAngVelocity.getX(), newAngVelocity.getY(), newAngVelocity.getZ(), 0) * rigidBody->getOrientation() * 0.5 * dt);
|
2010-07-05 20:52:16 +00:00
|
|
|
}
|
|
|
|
|
2010-09-01 20:59:35 +00:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:52:16 +00:00
|
|
|
// Apply the gravity force to all bodies of the physics world
|
|
|
|
void PhysicsEngine::applyGravity() {
|
|
|
|
|
|
|
|
// For each body of the physics world
|
2010-08-18 14:50:36 +00:00
|
|
|
for (vector<Body*>::iterator it=world->getBodiesBeginIterator(); it != world->getBodiesEndIterator(); it++) {
|
2010-07-05 20:52:16 +00:00
|
|
|
|
|
|
|
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*it);
|
|
|
|
assert(rigidBody);
|
|
|
|
|
|
|
|
// If the gravity force is on
|
|
|
|
if(world->getIsGravityOn()) {
|
|
|
|
// Apply the current gravity force to the body
|
2010-07-24 17:14:29 +00:00
|
|
|
rigidBody->setExternalForce(rigidBody->getMass() * world->getGravity());
|
2010-07-05 20:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-10 16:05:57 +00:00
|
|
|
}
|