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

rigidbody.h

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 #ifndef SHEEP_DYNAMICS_RIGIDBODY_H
00024 #define SHEEP_DYNAMICS_RIGIDBODY_H
00025 
00026 #include "common/math/de.h"
00027 #include "common/math/matrix3.h"
00028 #include "common/math/quaternion.h"
00029 #include "common/math/real.h"
00030 #include "common/math/vector3.h"
00031 
00032 #include <cassert>
00033 #include <cmath>    // for fabs()
00034 
00035 namespace sheep {
00036 
00037     class RigidBody {
00038     public:
00039         class StateVector {
00040         public:
00041             // Location of the center of mass.
00042             Vector3 m_x;    // world space
00043 
00044             // Orientation.
00045             Quaternion m_q; // world space
00046 
00047             // Linear momentum.
00048             Vector3 m_P;    // world space
00049 
00050             // Angular momentum.
00051             Vector3 m_L;    // world space
00052 
00053             inline StateVector();
00054             inline StateVector(const Vector3 &x, const Quaternion &q,
00055                 const Vector3 &P, const Vector3 &L);
00056 
00057             inline StateVector operator+(const StateVector &s) const;
00058             inline StateVector operator*(Real r) const;
00059         };
00060 
00061         typedef IDE1Solver<StateVector> StateDESolver;
00062 
00063         RigidBody(Real mass, const Matrix3 &inertia);
00064         ~RigidBody();
00065 
00066         inline Real GetMass() const;
00067 
00068         inline const Matrix3 &GetInertiaTensor() const; // body space
00069         inline Matrix3 GetWorldInertiaTensor() const;   // world space
00070 
00071         // Body freezing mechanism. A frozen body will not be taken into account
00072         // by the world into which it is registered. Also, a frozen body will
00073         // always report null velocities.
00074         inline void Freeze();
00075         inline void Unfreeze();
00076         inline bool IsFrozen() const;
00077 
00078         inline void SetPosition(const Vector3 &x);  // world space
00079         inline const Vector3 &GetPosition() const;  // world space
00080 
00081         inline void SetOrientation(const Quaternion &q);    // world space
00082         inline const Quaternion &GetOrientation() const;    // world space
00083 
00084         inline void SetLinearVelocity(const Vector3 &v);    // world space
00085         inline Vector3 GetLinearVelocity() const;   // world space
00086 
00087         inline void SetAngularVelocity(const Vector3 &omega);   // world space
00088         inline Vector3 GetAngularVelocity() const;  // world space
00089 
00090         inline Real GetKineticEnergy() const;
00091 
00092         inline Vector3 TransformVectorToWorld(const Vector3 &v) const;  // v in body space, result in world space
00093         inline Vector3 TransformVectorToBody(const Vector3 &v) const;   // v in world space, result in body space
00094         inline Vector3 TransformPointToWorld(const Vector3 &p) const;   // p in body space, result in world space
00095         inline Vector3 TransformPointToBody(const Vector3 &p) const;    // p in world space, result in body space
00096 
00097         // Return the velocity of a given point on the body.
00098         inline Vector3 GetPointVelocity(const Vector3 &p) const;    // p in body space, result in world space
00099 
00100         // Apply a force to the body at the center of mass.
00101         inline void ApplyForce(const Vector3 &f);   // world space
00102 
00103         // Apply a force to the body at a given point.
00104         inline void ApplyForce(const Vector3 &f, const Vector3 &p); // f in world space, p in body space
00105 
00106         // Apply a torque around the center of mass.
00107         inline void ApplyTorque(const Vector3 &t);      // t in world space
00108 
00109         // Reset force and torque accumulators.
00110         inline void ZeroForce();
00111         inline void ZeroTorque();
00112 
00113         // Get back the total force and torque applied until yet.
00114         inline Vector3 GetTotalForce() const;   // world space
00115         inline Vector3 GetTotalTorque() const;  // world space
00116 
00117         // Apply a linear impulse (instantaneous change in linear velocity).
00118         inline void ApplyLinearImpulse(const Vector3 &j);   // world space
00119 
00120         // Apply an angular impulse (instantaneous change in angular velocity).
00121         inline void ApplyAngularImpulse(const Vector3 &j);  // world space
00122 
00123         // This is temporary, utility methods.
00124         inline void SetState(const StateVector &s);
00125         inline StateVector GetState() const;
00126 
00127         // This method will update the state of the body (the body
00128         // will move and/or rotate) even if the body is frozen.
00129         void EvolveOneStep(const StateDESolver &solver, Real t, Real h);
00130 
00131     private:
00132         // Mass. Constant over simulation.
00133         Real m_mass;
00134 
00135         // Inertia tensor. Constant over simulation.
00136         Matrix3 m_inertia;  // body space
00137 
00138         bool m_is_frozen;   // a frozen body can't move nor rotate
00139 
00140         StateVector m_state;
00141 
00142         class StateVectorDE : public IDE1<StateVector> {
00143             const RigidBody *m_parent;
00144         public:
00145             inline StateVectorDE(const RigidBody *parent);
00146 
00147             virtual StateVector GetDerivativeAt(const StateVector &x, Real t) const;
00148         };
00149 
00150         friend class StateVectorDE;
00151 
00152         // Differential equation of the body state.
00153         StateVectorDE *m_state_de;
00154 
00155         // Sum of the forces applied to the body, expressed at the center of mass.
00156         Vector3 m_total_force;  // world space
00157 
00158         // Sum of the torques applied to the body, expressed at the center of mass.
00159         Vector3 m_total_torque; // world space
00160     };
00161 
00162 #include "rigidbody.inl"
00163 
00164 }
00165 
00166 #endif  // !SHEEP_DYNAMICS_RIGIDBODY_H

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