00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef B2_JOINT_H
00020 #define B2_JOINT_H
00021
00022 #include <Box2D/Common/b2Math.h>
00023
00024 class b2Body;
00025 class b2Joint;
00026 struct b2TimeStep;
00027 class b2BlockAllocator;
00028
00029 enum b2JointType
00030 {
00031 e_unknownJoint,
00032 e_revoluteJoint,
00033 e_prismaticJoint,
00034 e_distanceJoint,
00035 e_pulleyJoint,
00036 e_mouseJoint,
00037 e_gearJoint,
00038 e_lineJoint,
00039 e_weldJoint,
00040 e_frictionJoint,
00041 };
00042
00043 enum b2LimitState
00044 {
00045 e_inactiveLimit,
00046 e_atLowerLimit,
00047 e_atUpperLimit,
00048 e_equalLimits
00049 };
00050
00051 struct b2Jacobian
00052 {
00053 b2Vec2 linearA;
00054 float32 angularA;
00055 b2Vec2 linearB;
00056 float32 angularB;
00057
00058 void SetZero();
00059 void Set(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2);
00060 float32 Compute(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2);
00061 };
00062
00068 struct b2JointEdge
00069 {
00070 b2Body* other;
00071 b2Joint* joint;
00072 b2JointEdge* prev;
00073 b2JointEdge* next;
00074 };
00075
00077 struct b2JointDef
00078 {
00079 b2JointDef()
00080 {
00081 type = e_unknownJoint;
00082 userData = NULL;
00083 bodyA = NULL;
00084 bodyB = NULL;
00085 collideConnected = false;
00086 }
00087
00089 b2JointType type;
00090
00092 void* userData;
00093
00095 b2Body* bodyA;
00096
00098 b2Body* bodyB;
00099
00101 bool collideConnected;
00102 };
00103
00106 class b2Joint
00107 {
00108 public:
00109
00111 b2JointType GetType() const;
00112
00114 b2Body* GetBodyA();
00115
00117 b2Body* GetBodyB();
00118
00120 virtual b2Vec2 GetAnchorA() const = 0;
00121
00123 virtual b2Vec2 GetAnchorB() const = 0;
00124
00126 virtual b2Vec2 GetReactionForce(float32 inv_dt) const = 0;
00127
00129 virtual float32 GetReactionTorque(float32 inv_dt) const = 0;
00130
00132 b2Joint* GetNext();
00133
00135 void* GetUserData() const;
00136
00138 void SetUserData(void* data);
00139
00141 bool IsActive() const;
00142
00143 protected:
00144 friend class b2World;
00145 friend class b2Body;
00146 friend class b2Island;
00147
00148 static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
00149 static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
00150
00151 b2Joint(const b2JointDef* def);
00152 virtual ~b2Joint() {}
00153
00154 virtual void InitVelocityConstraints(const b2TimeStep& step) = 0;
00155 virtual void SolveVelocityConstraints(const b2TimeStep& step) = 0;
00156
00157
00158 virtual bool SolvePositionConstraints(float32 baumgarte) = 0;
00159
00160 b2JointType m_type;
00161 b2Joint* m_prev;
00162 b2Joint* m_next;
00163 b2JointEdge m_edgeA;
00164 b2JointEdge m_edgeB;
00165 b2Body* m_bodyA;
00166 b2Body* m_bodyB;
00167
00168 bool m_islandFlag;
00169 bool m_collideConnected;
00170
00171 void* m_userData;
00172
00173
00174 b2Vec2 m_localCenterA, m_localCenterB;
00175 float32 m_invMassA, m_invIA;
00176 float32 m_invMassB, m_invIB;
00177 };
00178
00179 inline void b2Jacobian::SetZero()
00180 {
00181 linearA.SetZero(); angularA = 0.0f;
00182 linearB.SetZero(); angularB = 0.0f;
00183 }
00184
00185 inline void b2Jacobian::Set(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2)
00186 {
00187 linearA = x1; angularA = a1;
00188 linearB = x2; angularB = a2;
00189 }
00190
00191 inline float32 b2Jacobian::Compute(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2)
00192 {
00193 return b2Dot(linearA, x1) + angularA * a1 + b2Dot(linearB, x2) + angularB * a2;
00194 }
00195
00196 inline b2JointType b2Joint::GetType() const
00197 {
00198 return m_type;
00199 }
00200
00201 inline b2Body* b2Joint::GetBodyA()
00202 {
00203 return m_bodyA;
00204 }
00205
00206 inline b2Body* b2Joint::GetBodyB()
00207 {
00208 return m_bodyB;
00209 }
00210
00211 inline b2Joint* b2Joint::GetNext()
00212 {
00213 return m_next;
00214 }
00215
00216 inline void* b2Joint::GetUserData() const
00217 {
00218 return m_userData;
00219 }
00220
00221 inline void b2Joint::SetUserData(void* data)
00222 {
00223 m_userData = data;
00224 }
00225
00226 #endif