first commit
[blok] / Box2D / Source / Dynamics / Joints / b2Joint.h
1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18
19 #ifndef JOINT_H
20 #define JOINT_H
21
22 #include "../../Common/b2Math.h"
23
24 class b2Body;
25 class b2Joint;
26 struct b2TimeStep;
27 class b2BlockAllocator;
28
29 enum b2JointType
30 {
31         e_unknownJoint,
32         e_revoluteJoint,
33         e_prismaticJoint,
34         e_distanceJoint,
35         e_pulleyJoint,
36         e_mouseJoint,
37         e_gearJoint
38 };
39
40 enum b2LimitState
41 {
42         e_inactiveLimit,
43         e_atLowerLimit,
44         e_atUpperLimit,
45         e_equalLimits
46 };
47
48 struct b2Jacobian
49 {
50         b2Vec2 linear1;
51         float32 angular1;
52         b2Vec2 linear2;
53         float32 angular2;
54
55         void SetZero();
56         void Set(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2);
57         float32 Compute(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2);
58 };
59
60 /// A joint edge is used to connect bodies and joints together
61 /// in a joint graph where each body is a node and each joint
62 /// is an edge. A joint edge belongs to a doubly linked list
63 /// maintained in each attached body. Each joint has two joint
64 /// nodes, one for each attached body.
65 struct b2JointEdge
66 {
67         b2Body* other;                  ///< provides quick access to the other body attached.
68         b2Joint* joint;                 ///< the joint
69         b2JointEdge* prev;              ///< the previous joint edge in the body's joint list
70         b2JointEdge* next;              ///< the next joint edge in the body's joint list
71 };
72
73 /// Joint definitions are used to construct joints.
74 struct b2JointDef
75 {
76         b2JointDef()
77         {
78                 type = e_unknownJoint;
79                 userData = NULL;
80                 body1 = NULL;
81                 body2 = NULL;
82                 collideConnected = false;
83         }
84
85         /// The joint type is set automatically for concrete joint types.
86         b2JointType type;
87
88         /// Use this to attach application specific data to your joints.
89         void* userData;
90
91         /// The first attached body.
92         b2Body* body1;
93
94         /// The second attached body.
95         b2Body* body2;
96
97         /// Set this flag to true if the attached bodies should collide.
98         bool collideConnected;
99 };
100
101 /// The base joint class. Joints are used to constraint two bodies together in
102 /// various fashions. Some joints also feature limits and motors.
103 class b2Joint
104 {
105 public:
106
107         /// Get the type of the concrete joint.
108         b2JointType GetType() const;
109
110         /// Get the first body attached to this joint.
111         b2Body* GetBody1();
112
113         /// Get the second body attached to this joint.
114         b2Body* GetBody2();
115
116         /// Get the anchor point on body1 in world coordinates.
117         virtual b2Vec2 GetAnchor1() const = 0;
118
119         /// Get the anchor point on body2 in world coordinates.
120         virtual b2Vec2 GetAnchor2() const = 0;
121
122         /// Get the reaction force on body2 at the joint anchor.
123         virtual b2Vec2 GetReactionForce() const = 0;
124
125         /// Get the reaction torque on body2.
126         virtual float32 GetReactionTorque() const = 0;
127
128         /// Get the next joint the world joint list.
129         b2Joint* GetNext();
130
131         /// Get the user data pointer.
132         void* GetUserData();
133
134         /// Set the user data pointer.
135         void SetUserData(void* data);
136
137         //--------------- Internals Below -------------------
138 protected:
139         friend class b2World;
140         friend class b2Body;
141         friend class b2Island;
142
143         static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
144         static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
145
146         b2Joint(const b2JointDef* def);
147         virtual ~b2Joint() {}
148
149         virtual void InitVelocityConstraints(const b2TimeStep& step) = 0;
150         virtual void SolveVelocityConstraints(const b2TimeStep& step) = 0;
151
152         // This returns true if the position errors are within tolerance.
153         virtual void InitPositionConstraints() {}
154         virtual bool SolvePositionConstraints() = 0;
155
156         b2JointType m_type;
157         b2Joint* m_prev;
158         b2Joint* m_next;
159         b2JointEdge m_node1;
160         b2JointEdge m_node2;
161         b2Body* m_body1;
162         b2Body* m_body2;
163
164         float32 m_inv_dt;
165
166         bool m_islandFlag;
167         bool m_collideConnected;
168
169         void* m_userData;
170 };
171
172 inline void b2Jacobian::SetZero()
173 {
174         linear1.SetZero(); angular1 = 0.0f;
175         linear2.SetZero(); angular2 = 0.0f;
176 }
177
178 inline void b2Jacobian::Set(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2)
179 {
180         linear1 = x1; angular1 = a1;
181         linear2 = x2; angular2 = a2;
182 }
183
184 inline float32 b2Jacobian::Compute(const b2Vec2& x1, float32 a1, const b2Vec2& x2, float32 a2)
185 {
186         return b2Dot(linear1, x1) + angular1 * a1 + b2Dot(linear2, x2) + angular2 * a2;
187 }
188
189 inline b2JointType b2Joint::GetType() const
190 {
191         return m_type;
192 }
193
194 inline b2Body* b2Joint::GetBody1()
195 {
196         return m_body1;
197 }
198
199 inline b2Body* b2Joint::GetBody2()
200 {
201         return m_body2;
202 }
203
204 inline b2Joint* b2Joint::GetNext()
205 {
206         return m_next;
207 }
208
209 inline void* b2Joint::GetUserData()
210 {
211         return m_userData;
212 }
213
214 inline void b2Joint::SetUserData(void* data)
215 {
216         m_userData = data;
217 }
218
219 #endif