00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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;
00082 };
00083
00084
00085 class FixedPathJoint : public Joint {
00086 public:
00087
00088
00089
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;
00101
00102 Vector3 *m_ccp;
00103 int m_nccp;
00104
00105 class DistanceFunction : public Function1 {
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