Remove the gravity from the rigid body external force and allow the user to enable/disable the gravity for each body

This commit is contained in:
Daniel Chappuis 2013-06-27 19:53:13 +02:00
parent 3f90564149
commit d58db2e6f2
4 changed files with 29 additions and 28 deletions

View File

@ -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);

View File

@ -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

View File

@ -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<RigidBody*>::iterator it;
for (it = getRigidBodiesBeginIterator(); it != getRigidBodiesEndIterator(); ++it) {
RigidBody* rigidBody = dynamic_cast<RigidBody*>(*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,

View File

@ -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();