2010-09-09 22:27:01 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Copyright (c) 2010-2012 Daniel Chappuis *
|
2010-09-09 22:27:01 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* This software is provided 'as-is', without any express or implied warranty. *
|
|
|
|
* In no event will the authors be held liable for any damages arising from the *
|
|
|
|
* use of this software. *
|
2010-09-09 22:27:01 +00:00
|
|
|
* *
|
2011-11-13 17:49:03 +00:00
|
|
|
* Permission is granted to anyone to use this software for any purpose, *
|
|
|
|
* including commercial applications, and to alter it and redistribute it *
|
|
|
|
* freely, subject to the following restrictions: *
|
|
|
|
* *
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim *
|
|
|
|
* that you wrote the original software. If you use this software in a *
|
|
|
|
* product, an acknowledgment in the product documentation would be *
|
|
|
|
* appreciated but is not required. *
|
|
|
|
* *
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be *
|
|
|
|
* misrepresented as being the original software. *
|
|
|
|
* *
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution. *
|
2010-09-09 22:27:01 +00:00
|
|
|
* *
|
|
|
|
********************************************************************************/
|
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
#ifndef RIGID_BODY_H
|
|
|
|
#define RIGID_BODY_H
|
2010-09-09 22:27:01 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include <cassert>
|
2012-09-18 20:09:49 +00:00
|
|
|
#include "CollisionBody.h"
|
2010-09-09 22:27:01 +00:00
|
|
|
#include "../mathematics/mathematics.h"
|
|
|
|
|
|
|
|
// Namespace reactphysics3d
|
|
|
|
namespace reactphysics3d {
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------
|
|
|
|
Class RigidBody :
|
|
|
|
This class represents a rigid body of the physics
|
|
|
|
engine. A rigid body is a non-deformable body that
|
2012-09-18 20:09:49 +00:00
|
|
|
has a constant mass. This class inherits from the
|
|
|
|
CollisionBody class.
|
2010-09-09 22:27:01 +00:00
|
|
|
-------------------------------------------------------------------
|
|
|
|
*/
|
2012-09-18 20:09:49 +00:00
|
|
|
class RigidBody : public CollisionBody {
|
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
protected :
|
2012-09-18 20:09:49 +00:00
|
|
|
|
|
|
|
// TODO : Remove the mass variable (duplicate with inverseMass)
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Attributes -------------------- //
|
|
|
|
|
|
|
|
// Mass of the body
|
|
|
|
decimal mMass;
|
|
|
|
|
|
|
|
// Linear velocity of the body
|
|
|
|
Vector3 mLinearVelocity;
|
|
|
|
|
|
|
|
// Angular velocity of the body
|
|
|
|
Vector3 mAngularVelocity;
|
|
|
|
|
|
|
|
// Current external force on the body
|
|
|
|
Vector3 mExternalForce;
|
|
|
|
|
|
|
|
// Current external torque on the body
|
|
|
|
Vector3 mExternalTorque;
|
|
|
|
|
|
|
|
// Local inertia tensor of the body (in local-space)
|
|
|
|
Matrix3x3 mInertiaTensorLocal;
|
|
|
|
|
|
|
|
// Inverse of the inertia tensor of the body
|
|
|
|
Matrix3x3 mInertiaTensorLocalInverse;
|
|
|
|
|
|
|
|
// Inverse of the mass of the body
|
|
|
|
decimal mMassInverse;
|
|
|
|
|
|
|
|
// Coefficient of restitution (between 0 and 1) where 1 is for a very bouncy body
|
|
|
|
decimal mRestitution;
|
|
|
|
|
2013-02-19 22:16:20 +00:00
|
|
|
// Friction coefficient
|
|
|
|
decimal mFrictionCoefficient;
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
// Private copy-constructor
|
|
|
|
RigidBody(const RigidBody& body);
|
|
|
|
|
|
|
|
// Private assignment operator
|
|
|
|
RigidBody& operator=(const RigidBody& body);
|
2010-09-09 22:27:01 +00:00
|
|
|
|
|
|
|
public :
|
2012-10-09 20:21:02 +00:00
|
|
|
|
|
|
|
// -------------------- Methods -------------------- //
|
|
|
|
|
|
|
|
// Constructor
|
2012-01-18 23:06:33 +00:00
|
|
|
RigidBody(const Transform& transform, decimal mass, const Matrix3x3& inertiaTensorLocal,
|
2012-10-09 20:21:02 +00:00
|
|
|
CollisionShape* collisionShape, bodyindex id); // Copy-constructor
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
virtual ~RigidBody();
|
|
|
|
|
|
|
|
// Return the mass of the body
|
|
|
|
decimal getMass() const;
|
|
|
|
|
|
|
|
// Set the mass of the body
|
|
|
|
void setMass(decimal mass);
|
|
|
|
|
|
|
|
// Return the linear velocity
|
|
|
|
Vector3 getLinearVelocity() const;
|
|
|
|
|
|
|
|
// Set the linear velocity of the body
|
|
|
|
void setLinearVelocity(const Vector3& linearVelocity);
|
|
|
|
|
|
|
|
// Return the angular velocity
|
|
|
|
Vector3 getAngularVelocity() const;
|
|
|
|
|
|
|
|
// Set the angular velocity
|
|
|
|
void setAngularVelocity(const Vector3& angularVelocity);
|
|
|
|
|
|
|
|
// Set the inverse of the mass
|
|
|
|
void setMassInverse(decimal massInverse);
|
|
|
|
|
|
|
|
// Return the current external force of the body
|
|
|
|
Vector3 getExternalForce() const;
|
|
|
|
|
|
|
|
// Set the current external force on the body
|
|
|
|
void setExternalForce(const Vector3& force);
|
|
|
|
|
|
|
|
// Return the current external torque of the body
|
|
|
|
Vector3 getExternalTorque() const;
|
|
|
|
|
|
|
|
// Set the current external torque of the body
|
|
|
|
void setExternalTorque(const Vector3& torque);
|
|
|
|
|
|
|
|
// Return the inverse of the mass of the body
|
|
|
|
decimal getMassInverse() const;
|
|
|
|
|
|
|
|
// Return the local inertia tensor of the body (in body coordinates)
|
|
|
|
Matrix3x3 getInertiaTensorLocal() const;
|
|
|
|
|
|
|
|
// Set the local inertia tensor of the body (in body coordinates)
|
|
|
|
void setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal);
|
|
|
|
|
|
|
|
// Get the inverse of the inertia tensor
|
|
|
|
Matrix3x3 getInertiaTensorLocalInverse() const;
|
|
|
|
|
|
|
|
// Return the inertia tensor in world coordinates
|
|
|
|
Matrix3x3 getInertiaTensorWorld() const;
|
|
|
|
|
|
|
|
// Return the inverse of the inertia tensor in world coordinates
|
|
|
|
Matrix3x3 getInertiaTensorInverseWorld() const;
|
2011-06-30 20:23:01 +00:00
|
|
|
|
2012-10-09 20:21:02 +00:00
|
|
|
// Get the restitution coefficient
|
|
|
|
decimal getRestitution() const;
|
|
|
|
|
|
|
|
// Set the restitution coefficient
|
|
|
|
void setRestitution(decimal restitution) throw(std::invalid_argument);
|
2013-02-19 22:16:20 +00:00
|
|
|
|
|
|
|
// Get the friction coefficient
|
|
|
|
decimal getFrictionCoefficient() const;
|
|
|
|
|
|
|
|
// Set the friction coefficient
|
|
|
|
void setFrictionCoefficient(decimal frictionCoefficient);
|
2010-09-09 22:27:01 +00:00
|
|
|
};
|
|
|
|
|
2012-09-18 20:09:49 +00:00
|
|
|
// Method that return the mass of the body
|
|
|
|
inline decimal RigidBody::getMass() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mMass;
|
2012-09-18 20:09:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Method that set the mass of the body
|
|
|
|
inline void RigidBody::setMass(decimal mass) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mMass = mass;
|
2012-09-18 20:09:49 +00:00
|
|
|
}
|
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
// Return the linear velocity
|
2011-08-18 21:02:48 +00:00
|
|
|
inline Vector3 RigidBody::getLinearVelocity() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mLinearVelocity;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the angular velocity of the body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline Vector3 RigidBody::getAngularVelocity() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mAngularVelocity;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
2011-08-18 21:02:48 +00:00
|
|
|
inline void RigidBody::setAngularVelocity(const Vector3& angularVelocity) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mAngularVelocity = angularVelocity;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the inverse of the mass
|
2012-01-18 23:06:33 +00:00
|
|
|
inline void RigidBody::setMassInverse(decimal massInverse) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mMassInverse = massInverse;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the inverse of the inertia tensor
|
|
|
|
inline Matrix3x3 RigidBody::getInertiaTensorLocalInverse() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mInertiaTensorLocalInverse;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the external force on the body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline Vector3 RigidBody::getExternalForce() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mExternalForce;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the external force on the body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline void RigidBody::setExternalForce(const Vector3& force) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mExternalForce = force;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 21:33:56 +00:00
|
|
|
// Return the current external torque on the body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline Vector3 RigidBody::getExternalTorque() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mExternalTorque;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
2012-08-02 21:33:56 +00:00
|
|
|
// Set the current external torque on the body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline void RigidBody::setExternalTorque(const Vector3& torque) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mExternalTorque = torque;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the inverse of the mass of the body
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal RigidBody::getMassInverse() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mMassInverse;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the local inertia tensor of the body (in body coordinates)
|
|
|
|
inline Matrix3x3 RigidBody::getInertiaTensorLocal() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mInertiaTensorLocal;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the local inertia tensor of the body (in body coordinates)
|
|
|
|
inline void RigidBody::setInertiaTensorLocal(const Matrix3x3& inertiaTensorLocal) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mInertiaTensorLocal = inertiaTensorLocal;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the inertia tensor in world coordinates
|
2012-10-09 20:21:02 +00:00
|
|
|
// The inertia tensor I_w in world coordinates is computed
|
|
|
|
// with the local inertia tensor I_b in body coordinates
|
2010-09-09 22:27:01 +00:00
|
|
|
// by I_w = R * I_b * R^T
|
2012-10-09 20:21:02 +00:00
|
|
|
// where R is the rotation matrix (and R^T its transpose) of
|
|
|
|
// the current orientation quaternion of the body
|
2010-09-09 22:27:01 +00:00
|
|
|
inline Matrix3x3 RigidBody::getInertiaTensorWorld() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
// Compute and return the inertia tensor in world coordinates
|
2012-10-09 20:21:02 +00:00
|
|
|
return mTransform.getOrientation().getMatrix() * mInertiaTensorLocal *
|
|
|
|
mTransform.getOrientation().getMatrix().getTranspose();
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the inverse of the inertia tensor in world coordinates
|
2012-10-09 20:21:02 +00:00
|
|
|
// The inertia tensor I_w in world coordinates is computed with the
|
|
|
|
// local inverse inertia tensor I_b^-1 in body coordinates
|
2010-09-09 22:27:01 +00:00
|
|
|
// by I_w = R * I_b^-1 * R^T
|
2012-10-09 20:21:02 +00:00
|
|
|
// where R is the rotation matrix (and R^T its transpose) of the
|
|
|
|
// current orientation quaternion of the body
|
2010-09-09 22:27:01 +00:00
|
|
|
inline Matrix3x3 RigidBody::getInertiaTensorInverseWorld() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
// Compute and return the inertia tensor in world coordinates
|
2012-10-09 20:21:02 +00:00
|
|
|
return mTransform.getOrientation().getMatrix() * mInertiaTensorLocalInverse *
|
|
|
|
mTransform.getOrientation().getMatrix().getTranspose();
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the linear velocity of the rigid body
|
2011-08-18 21:02:48 +00:00
|
|
|
inline void RigidBody::setLinearVelocity(const Vector3& linearVelocity) {
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
// If the body is able to move
|
2012-10-09 20:21:02 +00:00
|
|
|
if (mIsMotionEnabled) {
|
2010-09-09 22:27:01 +00:00
|
|
|
// Update the linear velocity of the current body state
|
2012-10-09 20:21:02 +00:00
|
|
|
mLinearVelocity = linearVelocity;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the restitution coeffficient of the rigid body
|
2012-01-18 23:06:33 +00:00
|
|
|
inline decimal RigidBody::getRestitution() const {
|
2012-10-09 20:21:02 +00:00
|
|
|
return mRestitution;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the restitution coefficient
|
2012-01-18 23:06:33 +00:00
|
|
|
inline void RigidBody::setRestitution(decimal restitution) throw(std::invalid_argument) {
|
2012-10-09 20:21:02 +00:00
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
// Check if the restitution coefficient is between 0 and 1
|
|
|
|
if (restitution >= 0.0 && restitution <= 1.0) {
|
2012-10-09 20:21:02 +00:00
|
|
|
mRestitution = restitution;
|
2010-09-09 22:27:01 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
throw std::invalid_argument("Error : the restitution coefficent must be between 0 and 1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-19 22:16:20 +00:00
|
|
|
// Get the friction coefficient
|
|
|
|
inline decimal RigidBody::getFrictionCoefficient() const {
|
|
|
|
return mFrictionCoefficient;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the friction coefficient
|
|
|
|
inline void RigidBody::setFrictionCoefficient(decimal frictionCoefficient) {
|
|
|
|
mFrictionCoefficient = frictionCoefficient;
|
|
|
|
}
|
|
|
|
|
2010-09-09 22:27:01 +00:00
|
|
|
} // End of the ReactPhyscis3D namespace
|
|
|
|
|
|
|
|
#endif
|