Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

rigidbody.inl

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #define STATEVECTOR RigidBody::StateVector
00024 
00025 inline
00026 STATEVECTOR::StateVector() {
00027 }
00028 
00029 inline
00030 STATEVECTOR::StateVector(const Vector3 &x, const Quaternion &q,
00031                          const Vector3 &P, const Vector3 &L) {
00032 //  assert(feq(q.Norm(), 1));
00033 
00034     m_x = x;
00035     m_q = q;
00036     m_P = P;
00037     m_L = L;
00038 }
00039 
00040 inline
00041 STATEVECTOR STATEVECTOR::operator+(const STATEVECTOR &s) const {
00042     return STATEVECTOR(m_x+s.m_x, m_q+s.m_q, m_P+s.m_P, m_L+s.m_L);
00043 }
00044 
00045 inline
00046 STATEVECTOR STATEVECTOR::operator*(Real r) const {
00047     return STATEVECTOR(m_x*r, m_q*r, m_P*r, m_L*r);
00048 }
00049 
00050 inline
00051 RigidBody::StateVectorDE::StateVectorDE(const RigidBody *parent) {
00052     assert(parent);
00053 
00054     m_parent = parent;
00055 }
00056 
00057 inline
00058 Real RigidBody::GetMass() const {
00059 //  assert(!m_is_frozen);
00060 
00061     return m_mass;
00062 }
00063 
00064 inline
00065 const Matrix3 &RigidBody::GetInertiaTensor() const {    // body space
00066 //  assert(!m_is_frozen);
00067 
00068     return m_inertia;
00069 }
00070 
00071 inline
00072 Matrix3 RigidBody::GetWorldInertiaTensor() const {  // world space
00073 //  assert(!m_is_frozen);
00074 
00075     assert(feq(m_state.m_q.Norm(), 1));
00076 
00077     Matrix3 R = Matrix3::Rotation(m_state.m_q); // rotation matrix associated to the body's orientation
00078 
00079     assert(feq(R.Determinant(), 1));
00080 
00081     return R * m_inertia * R.Transpose();   // inertia tensor in world space
00082 }
00083 
00084 inline
00085 void RigidBody::Freeze() {
00086     m_state.m_P = 0;    // no linear velocity
00087     m_state.m_L = 0;    // no angular velocity
00088     m_total_force = 0;  // no force acting
00089     m_total_torque = 0; // no torque acting
00090 
00091     m_is_frozen = true;
00092 }
00093 
00094 inline
00095 void RigidBody::Unfreeze() {
00096     m_is_frozen = false;
00097 }
00098 
00099 inline
00100 bool RigidBody::IsFrozen() const {
00101     return m_is_frozen;
00102 }
00103 
00104 inline
00105 void RigidBody::SetPosition(const Vector3 &x) { // world space
00106     m_state.m_x = x;
00107 }
00108 
00109 inline
00110 const Vector3 &RigidBody::GetPosition() const { // world space
00111     return m_state.m_x;
00112 }
00113 
00114 inline
00115 void RigidBody::SetOrientation(const Quaternion &q) {   // world space
00116     m_state.m_q = q;
00117 }
00118 
00119 inline
00120 const Quaternion &RigidBody::GetOrientation() const {   // world space
00121     return m_state.m_q;
00122 }
00123 
00124 inline
00125 void RigidBody::SetLinearVelocity(const Vector3 &v) {   // world space
00126     assert(!m_is_frozen);
00127 
00128     if(!m_is_frozen)
00129         m_state.m_P = m_mass * v;
00130 }
00131 
00132 inline
00133 Vector3 RigidBody::GetLinearVelocity() const {  // world space
00134     return m_is_frozen ? 0 : 1.0 / m_mass * m_state.m_P;
00135 }
00136 
00137 inline
00138 void RigidBody::SetAngularVelocity(const Vector3 &omega) {  // world space
00139     assert(!m_is_frozen);
00140 
00141     if(!m_is_frozen)
00142         m_state.m_L = GetWorldInertiaTensor() * omega;
00143 }
00144 
00145 inline
00146 Vector3 RigidBody::GetAngularVelocity() const { // world space
00147     return m_is_frozen ? 0 : GetWorldInertiaTensor().Inverse() * m_state.m_L;
00148 }
00149 
00150 inline
00151 Real RigidBody::GetKineticEnergy() const {
00152     if(m_is_frozen)
00153         return 0;
00154 
00155     Real linear_term = 0.5 * (GetMass() * GetLinearVelocity() * GetLinearVelocity());
00156     Real angular_term = 0.5 * (GetWorldInertiaTensor() * GetAngularVelocity() * GetAngularVelocity());
00157 
00158     return linear_term + angular_term;
00159 }
00160 
00161 inline
00162 Vector3 RigidBody::TransformVectorToWorld(const Vector3 &v) const { // p in body space, result in world space
00163     assert(feq(m_state.m_q.Norm(), 1));
00164 
00165     return (m_state.m_q * Quaternion(0, v) * m_state.m_q.Conjugate()).m_v;
00166 }
00167 
00168 inline
00169 Vector3 RigidBody::TransformVectorToBody(const Vector3 &v) const {  // p in world space, result in body space
00170     assert(feq(m_state.m_q.Norm(), 1));
00171 
00172     return (m_state.m_q.Conjugate() * Quaternion(0, v) * m_state.m_q).m_v;
00173 }
00174 
00175 inline
00176 Vector3 RigidBody::TransformPointToWorld(const Vector3 &p) const {  // p in body space, result in world space
00177     return TransformVectorToWorld(p) + GetPosition();
00178 }
00179 
00180 inline
00181 Vector3 RigidBody::TransformPointToBody(const Vector3 &p) const {   // p in world space, result in body space
00182     return TransformVectorToBody(p - GetPosition());
00183 }
00184 
00185 inline
00186 Vector3 RigidBody::GetPointVelocity(const Vector3 &p) const {   // p in body space, result in world space
00187     return GetLinearVelocity() + (GetAngularVelocity() ^ TransformVectorToWorld(p));
00188 }
00189 
00190 inline
00191 void RigidBody::ApplyForce(const Vector3 &f) {  // world space
00192     if(!m_is_frozen)
00193         m_total_force += f;
00194 }
00195 
00196 inline
00197 void RigidBody::ApplyForce(const Vector3 &f, const Vector3 &p) {    // f in world space, p in body space
00198     if(!m_is_frozen) {
00199         m_total_force += f;
00200         m_total_torque += TransformVectorToWorld(p) ^ f;
00201     }
00202 }
00203 
00204 inline
00205 void RigidBody::ApplyTorque(const Vector3 &t) {     // t in world space
00206     if(!m_is_frozen) {
00207         m_total_torque += t;
00208     }
00209 }
00210 
00211 inline
00212 void RigidBody::ZeroForce() {
00213     m_total_force = 0;
00214 }
00215 
00216 inline
00217 void RigidBody::ZeroTorque() {
00218     m_total_torque = 0;
00219 }
00220 
00221 inline
00222 Vector3 RigidBody::GetTotalForce() const {  // world space
00223     return m_total_force;
00224 }
00225 
00226 inline
00227 Vector3 RigidBody::GetTotalTorque() const { // world space
00228     return m_total_torque;
00229 }
00230 
00231 inline
00232 void RigidBody::ApplyLinearImpulse(const Vector3 &j) {  // world space
00233     if(!m_is_frozen)
00234         m_state.m_P += j;
00235 }
00236 
00237 inline
00238 void RigidBody::ApplyAngularImpulse(const Vector3 &j) { // world space
00239     if(!m_is_frozen)
00240         m_state.m_L += j;
00241 }
00242 
00243 inline
00244 void RigidBody::SetState(const STATEVECTOR &s) {
00245     if(!m_is_frozen)
00246         m_state = s;
00247 }
00248 
00249 inline
00250 STATEVECTOR RigidBody::GetState() const {
00251     return m_state;
00252 }
00253 
00254 #undef STATEVECTOR

Generated on Tue May 11 01:31:52 2004 for toxic by doxygen 1.3.6