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

joints.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_JOINTS_H
00024 #define SHEEP_DYNAMICS_JOINTS_H
00025 
00026 #include "common/math/findroot.h"
00027 #include "common/math/matrix.h"
00028 #include "common/math/real.h"
00029 #include "common/math/vector.h"
00030 #include "common/math/vector3.h"
00031 #include "common/misc/htmlstream.h"
00032 #include "engine/dynamics/rigidbody.h"
00033 
00034 #include <cassert>
00035 #include <list>
00036 
00037 namespace sheep {
00038 
00039     //!\todo Rename as IJoint.
00040     class Joint {
00041     public:
00042         Joint(int dimension, int body_count);
00043         virtual ~Joint();
00044 
00045         inline int GetDimension() const;
00046         inline int GetBodyCount() const;
00047 
00048         inline RigidBody *GetBody(int n);
00049 
00050         inline Matrix *GetJ(const RigidBody *body) const;
00051         inline Vector *GetC() const;
00052 
00053         virtual void Update() = 0;
00054 
00055         virtual Real GetError() = 0;
00056         virtual void CorrectError() = 0;
00057 
00058     protected:
00059         int m_dimension;
00060         int m_body_count;
00061 
00062         RigidBody **m_bodies;
00063 
00064         Matrix **m_j;
00065         Matrix *m_zero;
00066         Vector *m_c;
00067     };
00068 
00069     class SphericalJoint : public Joint {
00070     public:
00071         // 'anchor1' and 'anchor2' are expressed in body space.
00072         SphericalJoint(RigidBody *body0, const Vector3 &anchor0,
00073             RigidBody *body1, const Vector3 &anchor1);
00074 
00075         virtual void Update();
00076 
00077         virtual Real GetError();
00078         virtual void CorrectError();
00079 
00080     private:
00081         Vector3 m_anchor0, m_anchor1;   // body space
00082     };
00083 
00084     //!\todo Modify to allow use of any piecewise curve.
00085     class FixedPathJoint : public Joint {
00086     public:
00087         // 'anchor' is expressed in body space. 'ccp' stands for
00088         // "Curve Control Points". Control points are expressed in
00089         // world space.
00090         FixedPathJoint(RigidBody *body, const Vector3 &anchor,
00091             const Vector3 *ccp, int nccp);
00092         inline virtual ~FixedPathJoint();
00093 
00094         virtual void Update();
00095 
00096         virtual Real GetError();
00097         virtual void CorrectError();
00098 
00099     private:
00100         Vector3 m_anchor;   // body space
00101 
00102         Vector3 *m_ccp;     // world space
00103         int m_nccp;
00104 
00105         class DistanceFunction : public Function1 { // actually, derivative of the distance function
00106         public:
00107             DistanceFunction(const Vector3 *q, const Vector3 &p);
00108 
00109             virtual Real EvaluateAt(Real x) const;
00110 
00111         private:
00112             Vector3 m_a, m_b, m_c, m_d;
00113             Vector3 m_p;
00114         };
00115 
00116         void project_on_curve(const Vector3 &p, int *seg, Real *t) const;
00117     };
00118 
00119     class JointManager {
00120     public:
00121         JointManager();
00122 
00123         void Insert(Joint *joint);
00124         void Remove(Joint *joint);
00125 
00126         void Setup();
00127 
00128         void ApplyContraints();
00129 
00130     private:
00131         HTMLStream *debug;
00132 
00133         typedef std::list<Joint *> joint_list;
00134         typedef std::list<RigidBody *> rigidbody_list;
00135 
00136         joint_list m_joints;
00137 
00138         rigidbody_list m_bodies;
00139 
00140         Matrix *m_invm;
00141         Matrix *m_j;
00142         Vector *m_c;
00143         Vector *m_fext;
00144 
00145         Matrix *m_a;
00146         Vector *m_b;
00147         Vector *m_lambda;
00148         Vector *m_f;
00149     };
00150 
00151 #include "joints.inl"
00152 
00153 }
00154 
00155 #endif  // !SHEEP_DYNAMICS_JOINTS_H

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