From d58db2e6f2c010f3298ab9d5942282fa31a1eb19 Mon Sep 17 00:00:00 2001 From: Daniel Chappuis Date: Thu, 27 Jun 2013 19:53:13 +0200 Subject: [PATCH] Remove the gravity from the rigid body external force and allow the user to enable/disable the gravity for each body --- src/body/RigidBody.cpp | 3 ++- src/body/RigidBody.h | 19 +++++++++++++++++++ src/engine/DynamicsWorld.cpp | 32 ++++++++------------------------ src/engine/DynamicsWorld.h | 3 --- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/body/RigidBody.cpp b/src/body/RigidBody.cpp index 40139aff..5b2022bd 100644 --- a/src/body/RigidBody.cpp +++ b/src/body/RigidBody.cpp @@ -35,7 +35,8 @@ using namespace reactphysics3d; CollisionShape *collisionShape, bodyindex id) : CollisionBody(transform, collisionShape, id), mInertiaTensorLocal(inertiaTensorLocal), mMass(mass), mInertiaTensorLocalInverse(inertiaTensorLocal.getInverse()), - mMassInverse(decimal(1.0) / mass), mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT) { + mMassInverse(decimal(1.0) / mass), mFrictionCoefficient(DEFAULT_FRICTION_COEFFICIENT), + mIsGravityEnabled(true) { mRestitution = decimal(1.0); diff --git a/src/body/RigidBody.h b/src/body/RigidBody.h index 1f340961..66a3f70c 100644 --- a/src/body/RigidBody.h +++ b/src/body/RigidBody.h @@ -79,6 +79,9 @@ class RigidBody : public CollisionBody { /// Friction coefficient decimal mFrictionCoefficient; + /// True if the gravity needs to be applied to this rigid body + bool mIsGravityEnabled; + // -------------------- Methods -------------------- // /// Private copy-constructor @@ -160,6 +163,12 @@ class RigidBody : public CollisionBody { /// Set the friction coefficient void setFrictionCoefficient(decimal frictionCoefficient); + + /// Return true if the gravity needs to be applied to this rigid body + bool isGravityEnabled() const; + + /// Set the variable to know if the gravity is applied to this rigid body + void enableGravity(bool isEnabled); }; // Method that return the mass of the body @@ -288,6 +297,16 @@ inline void RigidBody::setFrictionCoefficient(decimal frictionCoefficient) { mFrictionCoefficient = frictionCoefficient; } +// Return true if the gravity needs to be applied to this rigid body +inline bool RigidBody::isGravityEnabled() const { + return mIsGravityEnabled; +} + +// Set the variable to know if the gravity is applied to this rigid body +inline void RigidBody::enableGravity(bool isEnabled) { + mIsGravityEnabled = isEnabled; +} + } #endif diff --git a/src/engine/DynamicsWorld.cpp b/src/engine/DynamicsWorld.cpp index 5a7062e0..27a8fc12 100644 --- a/src/engine/DynamicsWorld.cpp +++ b/src/engine/DynamicsWorld.cpp @@ -88,9 +88,6 @@ void DynamicsWorld::update() { // Compute the time since the last update() call and update the timer mTimer.update(); - // Apply the gravity force to all bodies - applyGravity(); - // While the time accumulator is not empty while(mTimer.isPossibleToTakeStep()) { @@ -253,6 +250,14 @@ void DynamicsWorld::integrateRigidBodiesVelocities() { mConstrainedAngularVelocities[i] = rigidBody->getAngularVelocity() + dt * rigidBody->getInertiaTensorInverseWorld() * rigidBody->getExternalTorque(); + // If the gravity has to be applied to this rigid body + if (rigidBody->isGravityEnabled() && mIsGravityOn) { + + // Integrate the gravity force + mConstrainedLinearVelocities[i] += dt * rigidBody->getMassInverse() * + rigidBody->getMass() * mGravity; + } + // Update the old Transform of the body rigidBody->updateOldTransform(); } @@ -376,27 +381,6 @@ void DynamicsWorld::cleanupConstrainedVelocitiesArray() { mMapBodyToConstrainedVelocityIndex.clear(); } -// Apply the gravity force to all bodies of the physics world -void DynamicsWorld::applyGravity() { - - PROFILE("DynamicsWorld::applyGravity()"); - - // For each body of the physics world - set::iterator it; - for (it = getRigidBodiesBeginIterator(); it != getRigidBodiesEndIterator(); ++it) { - - RigidBody* rigidBody = dynamic_cast(*it); - assert(rigidBody != NULL); - - // If the gravity force is on - if(mIsGravityOn) { - - // Apply the current gravity force to the body - rigidBody->setExternalForce(rigidBody->getMass() * mGravity); - } - } -} - // Create a rigid body into the physics world RigidBody* DynamicsWorld::createRigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal, diff --git a/src/engine/DynamicsWorld.h b/src/engine/DynamicsWorld.h index d5412971..f911bd08 100644 --- a/src/engine/DynamicsWorld.h +++ b/src/engine/DynamicsWorld.h @@ -133,9 +133,6 @@ class DynamicsWorld : public CollisionWorld { /// Cleanup the constrained velocities array at each step void cleanupConstrainedVelocitiesArray(); - /// Apply the gravity force to all bodies - void applyGravity(); - /// Reset the boolean movement variable of each body void resetBodiesMovementVariable();