1 /*
2 * Copyright (c) 2006-2007 Erin Catto http://www.box2d.org
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 B2_JOINT_H
20 #define B2_JOINT_H
21 
22 #include <Box2D/Common/b2Math.h>
23 
24 class b2Body;
25 class b2Joint;
26 struct b2SolverData;
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 	e_wheelJoint,
39     e_weldJoint,
40 	e_frictionJoint,
41 	e_ropeJoint
42 };
43 
44 enum b2LimitState
45 {
46 	e_inactiveLimit,
47 	e_atLowerLimit,
48 	e_atUpperLimit,
49 	e_equalLimits
50 };
51 
52 struct b2Jacobian
53 {
54 	b2Vec2 linear;
55 	float32 angularA;
56 	float32 angularB;
57 };
58 
59 /// A joint edge is used to connect bodies and joints together
60 /// in a joint graph where each body is a node and each joint
61 /// is an edge. A joint edge belongs to a doubly linked list
62 /// maintained in each attached body. Each joint has two joint
63 /// nodes, one for each attached body.
64 struct b2JointEdge
65 {
66 	b2Body* other;			///< provides quick access to the other body attached.
67 	b2Joint* joint;			///< the joint
68 	b2JointEdge* prev;		///< the previous joint edge in the body's joint list
69 	b2JointEdge* next;		///< the next joint edge in the body's joint list
70 };
71 
72 /// Joint definitions are used to construct joints.
73 struct b2JointDef
74 {
b2JointDefb2JointDef75 	b2JointDef()
76 	{
77 		type = e_unknownJoint;
78 		userData = NULL;
79 		bodyA = NULL;
80 		bodyB = NULL;
81 		collideConnected = false;
82 	}
83 
84 	/// The joint type is set automatically for concrete joint types.
85 	b2JointType type;
86 
87 	/// Use this to attach application specific data to your joints.
88 	void* userData;
89 
90 	/// The first attached body.
91 	b2Body* bodyA;
92 
93 	/// The second attached body.
94 	b2Body* bodyB;
95 
96 	/// Set this flag to true if the attached bodies should collide.
97 	bool collideConnected;
98 };
99 
100 /// The base joint class. Joints are used to constraint two bodies together in
101 /// various fashions. Some joints also feature limits and motors.
102 class b2Joint
103 {
104 public:
105 
106 	/// Get the type of the concrete joint.
107 	b2JointType GetType() const;
108 
109 	/// Get the first body attached to this joint.
110 	b2Body* GetBodyA();
111 
112 	/// Get the second body attached to this joint.
113 	b2Body* GetBodyB();
114 
115 	/// Get the anchor point on bodyA in world coordinates.
116 	virtual b2Vec2 GetAnchorA() const = 0;
117 
118 	/// Get the anchor point on bodyB in world coordinates.
119 	virtual b2Vec2 GetAnchorB() const = 0;
120 
121 	/// Get the reaction force on bodyB at the joint anchor in Newtons.
122 	virtual b2Vec2 GetReactionForce(float32 inv_dt) const = 0;
123 
124 	/// Get the reaction torque on bodyB in N*m.
125 	virtual float32 GetReactionTorque(float32 inv_dt) const = 0;
126 
127 	/// Get the next joint the world joint list.
128 	b2Joint* GetNext();
129 	const b2Joint* GetNext() const;
130 
131 	/// Get the user data pointer.
132 	void* GetUserData() const;
133 
134 	/// Set the user data pointer.
135 	void SetUserData(void* data);
136 
137 	/// Short-cut function to determine if either body is inactive.
138 	bool IsActive() const;
139 
140 	/// Get collide connected.
141 	/// Note: modifying the collide connect flag won't work correctly because
142 	/// the flag is only checked when fixture AABBs begin to overlap.
143 	bool GetCollideConnected() const;
144 
145 	/// Dump this joint to the log file.
Dump()146 	virtual void Dump() { b2Log("// Dump is not supported for this joint type.\n"); }
147 
148 protected:
149 	friend class b2World;
150 	friend class b2Body;
151 	friend class b2Island;
152 	friend class b2GearJoint;
153 
154 	static b2Joint* Create(const b2JointDef* def, b2BlockAllocator* allocator);
155 	static void Destroy(b2Joint* joint, b2BlockAllocator* allocator);
156 
157 	b2Joint(const b2JointDef* def);
~b2Joint()158 	virtual ~b2Joint() {}
159 
160 	virtual void InitVelocityConstraints(const b2SolverData& data) = 0;
161 	virtual void SolveVelocityConstraints(const b2SolverData& data) = 0;
162 
163 	// This returns true if the position errors are within tolerance.
164 	virtual bool SolvePositionConstraints(const b2SolverData& data) = 0;
165 
166 	b2JointType m_type;
167 	b2Joint* m_prev;
168 	b2Joint* m_next;
169 	b2JointEdge m_edgeA;
170 	b2JointEdge m_edgeB;
171 	b2Body* m_bodyA;
172 	b2Body* m_bodyB;
173 
174 	int32 m_index;
175 
176 	bool m_islandFlag;
177 	bool m_collideConnected;
178 
179 	void* m_userData;
180 };
181 
GetType()182 inline b2JointType b2Joint::GetType() const
183 {
184 	return m_type;
185 }
186 
GetBodyA()187 inline b2Body* b2Joint::GetBodyA()
188 {
189 	return m_bodyA;
190 }
191 
GetBodyB()192 inline b2Body* b2Joint::GetBodyB()
193 {
194 	return m_bodyB;
195 }
196 
GetNext()197 inline b2Joint* b2Joint::GetNext()
198 {
199 	return m_next;
200 }
201 
GetNext()202 inline const b2Joint* b2Joint::GetNext() const
203 {
204 	return m_next;
205 }
206 
GetUserData()207 inline void* b2Joint::GetUserData() const
208 {
209 	return m_userData;
210 }
211 
SetUserData(void * data)212 inline void b2Joint::SetUserData(void* data)
213 {
214 	m_userData = data;
215 }
216 
GetCollideConnected()217 inline bool b2Joint::GetCollideConnected() const
218 {
219 	return m_collideConnected;
220 }
221 
222 #endif
223