Fix issue in RigidBody::applyForce() method

This commit is contained in:
Daniel Chappuis 2014-06-29 21:59:06 +02:00
parent 3aa05ef61a
commit 114360337c
2 changed files with 41 additions and 6 deletions

View File

@ -95,16 +95,45 @@ void RigidBody::setType(BodyType type) {
mExternalTorque.setToZero(); mExternalTorque.setToZero();
} }
// Set the local inertia tensor of the body (in body coordinates) // Set the local inertia tensor of the body (in local-space coordinates)
void RigidBody::setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal) { void RigidBody::setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal) {
if (mType != DYNAMIC) return;
mInertiaTensorLocal = inertiaTensorLocal; mInertiaTensorLocal = inertiaTensorLocal;
// Recompute the inverse local inertia tensor // Compute the inverse local inertia tensor
if (mType == DYNAMIC) { mInertiaTensorLocalInverse = mInertiaTensorLocal.getInverse();
mInertiaTensorLocalInverse = mInertiaTensorLocal.getInverse(); }
// Set the local center of mass of the body (in local-space coordinates)
void RigidBody::setCenterOfMassLocal(const Vector3& centerOfMassLocal) {
if (mType != DYNAMIC) return;
const Vector3 oldCenterOfMass = mCenterOfMassWorld;
mCenterOfMassLocal = centerOfMassLocal;
// Compute the center of mass in world-space coordinates
mCenterOfMassWorld = mTransform * mCenterOfMassLocal;
// Update the linear velocity of the center of mass
mLinearVelocity += mAngularVelocity.cross(mCenterOfMassWorld - oldCenterOfMass);
}
// Set the mass of the rigid body
void RigidBody::setMass(decimal mass) {
if (mType != DYNAMIC) return;
mInitMass = mass;
if (mInitMass > decimal(0.0)) {
mMassInverse = decimal(1.0) / mInitMass;
} }
else { else {
mInertiaTensorLocalInverse = Matrix3x3::zero(); mInitMass = decimal(1.0);
mMassInverse = decimal(1.0);
} }
} }

View File

@ -149,6 +149,12 @@ class RigidBody : public CollisionBody {
/// Set the local inertia tensor of the body (in body coordinates) /// Set the local inertia tensor of the body (in body coordinates)
void setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal); void setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal);
/// Set the local center of mass of the body (in local-space coordinates)
void setCenterOfMassLocal(const Vector3& centerOfMassLocal);
/// Set the mass of the rigid body
void setMass(decimal mass);
/// Return the inertia tensor in world coordinates. /// Return the inertia tensor in world coordinates.
Matrix3x3 getInertiaTensorWorld() const; Matrix3x3 getInertiaTensorWorld() const;
@ -386,7 +392,7 @@ inline void RigidBody::applyForce(const Vector3& force, const Vector3& point) {
// Add the force and torque // Add the force and torque
mExternalForce += force; mExternalForce += force;
mExternalTorque += (point - mTransform.getPosition()).cross(force); mExternalTorque += (point - mCenterOfMassWorld).cross(force);
} }
// Apply an external torque to the body. // Apply an external torque to the body.