• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 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 #include <Box2D/Dynamics/Joints/b2PulleyJoint.h>
20 #include <Box2D/Dynamics/b2Body.h>
21 #include <Box2D/Dynamics/b2TimeStep.h>
22 
23 // Pulley:
24 // length1 = norm(p1 - s1)
25 // length2 = norm(p2 - s2)
26 // C0 = (length1 + ratio * length2)_initial
27 // C = C0 - (length1 + ratio * length2)
28 // u1 = (p1 - s1) / norm(p1 - s1)
29 // u2 = (p2 - s2) / norm(p2 - s2)
30 // Cdot = -dot(u1, v1 + cross(w1, r1)) - ratio * dot(u2, v2 + cross(w2, r2))
31 // J = -[u1 cross(r1, u1) ratio * u2  ratio * cross(r2, u2)]
32 // K = J * invM * JT
33 //   = invMass1 + invI1 * cross(r1, u1)^2 + ratio^2 * (invMass2 + invI2 * cross(r2, u2)^2)
34 
Initialize(b2Body * bA,b2Body * bB,const b2Vec2 & groundA,const b2Vec2 & groundB,const b2Vec2 & anchorA,const b2Vec2 & anchorB,float32 r)35 void b2PulleyJointDef::Initialize(b2Body* bA, b2Body* bB,
36 				const b2Vec2& groundA, const b2Vec2& groundB,
37 				const b2Vec2& anchorA, const b2Vec2& anchorB,
38 				float32 r)
39 {
40 	bodyA = bA;
41 	bodyB = bB;
42 	groundAnchorA = groundA;
43 	groundAnchorB = groundB;
44 	localAnchorA = bodyA->GetLocalPoint(anchorA);
45 	localAnchorB = bodyB->GetLocalPoint(anchorB);
46 	b2Vec2 dA = anchorA - groundA;
47 	lengthA = dA.Length();
48 	b2Vec2 dB = anchorB - groundB;
49 	lengthB = dB.Length();
50 	ratio = r;
51 	b2Assert(ratio > b2_epsilon);
52 }
53 
b2PulleyJoint(const b2PulleyJointDef * def)54 b2PulleyJoint::b2PulleyJoint(const b2PulleyJointDef* def)
55 : b2Joint(def)
56 {
57 	m_groundAnchorA = def->groundAnchorA;
58 	m_groundAnchorB = def->groundAnchorB;
59 	m_localAnchorA = def->localAnchorA;
60 	m_localAnchorB = def->localAnchorB;
61 
62 	m_lengthA = def->lengthA;
63 	m_lengthB = def->lengthB;
64 
65 	b2Assert(def->ratio != 0.0f);
66 	m_ratio = def->ratio;
67 
68 	m_constant = def->lengthA + m_ratio * def->lengthB;
69 
70 	m_impulse = 0.0f;
71 }
72 
InitVelocityConstraints(const b2SolverData & data)73 void b2PulleyJoint::InitVelocityConstraints(const b2SolverData& data)
74 {
75 	m_indexA = m_bodyA->m_islandIndex;
76 	m_indexB = m_bodyB->m_islandIndex;
77 	m_localCenterA = m_bodyA->m_sweep.localCenter;
78 	m_localCenterB = m_bodyB->m_sweep.localCenter;
79 	m_invMassA = m_bodyA->m_invMass;
80 	m_invMassB = m_bodyB->m_invMass;
81 	m_invIA = m_bodyA->m_invI;
82 	m_invIB = m_bodyB->m_invI;
83 
84 	b2Vec2 cA = data.positions[m_indexA].c;
85 	float32 aA = data.positions[m_indexA].a;
86 	b2Vec2 vA = data.velocities[m_indexA].v;
87 	float32 wA = data.velocities[m_indexA].w;
88 
89 	b2Vec2 cB = data.positions[m_indexB].c;
90 	float32 aB = data.positions[m_indexB].a;
91 	b2Vec2 vB = data.velocities[m_indexB].v;
92 	float32 wB = data.velocities[m_indexB].w;
93 
94 	b2Rot qA(aA), qB(aB);
95 
96 	m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
97 	m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
98 
99 	// Get the pulley axes.
100 	m_uA = cA + m_rA - m_groundAnchorA;
101 	m_uB = cB + m_rB - m_groundAnchorB;
102 
103 	float32 lengthA = m_uA.Length();
104 	float32 lengthB = m_uB.Length();
105 
106 	if (lengthA > 10.0f * b2_linearSlop)
107 	{
108 		m_uA *= 1.0f / lengthA;
109 	}
110 	else
111 	{
112 		m_uA.SetZero();
113 	}
114 
115 	if (lengthB > 10.0f * b2_linearSlop)
116 	{
117 		m_uB *= 1.0f / lengthB;
118 	}
119 	else
120 	{
121 		m_uB.SetZero();
122 	}
123 
124 	// Compute effective mass.
125 	float32 ruA = b2Cross(m_rA, m_uA);
126 	float32 ruB = b2Cross(m_rB, m_uB);
127 
128 	float32 mA = m_invMassA + m_invIA * ruA * ruA;
129 	float32 mB = m_invMassB + m_invIB * ruB * ruB;
130 
131 	m_mass = mA + m_ratio * m_ratio * mB;
132 
133 	if (m_mass > 0.0f)
134 	{
135 		m_mass = 1.0f / m_mass;
136 	}
137 
138 	if (data.step.warmStarting)
139 	{
140 		// Scale impulses to support variable time steps.
141 		m_impulse *= data.step.dtRatio;
142 
143 		// Warm starting.
144 		b2Vec2 PA = -(m_impulse) * m_uA;
145 		b2Vec2 PB = (-m_ratio * m_impulse) * m_uB;
146 
147 		vA += m_invMassA * PA;
148 		wA += m_invIA * b2Cross(m_rA, PA);
149 		vB += m_invMassB * PB;
150 		wB += m_invIB * b2Cross(m_rB, PB);
151 	}
152 	else
153 	{
154 		m_impulse = 0.0f;
155 	}
156 
157 	data.velocities[m_indexA].v = vA;
158 	data.velocities[m_indexA].w = wA;
159 	data.velocities[m_indexB].v = vB;
160 	data.velocities[m_indexB].w = wB;
161 }
162 
SolveVelocityConstraints(const b2SolverData & data)163 void b2PulleyJoint::SolveVelocityConstraints(const b2SolverData& data)
164 {
165 	b2Vec2 vA = data.velocities[m_indexA].v;
166 	float32 wA = data.velocities[m_indexA].w;
167 	b2Vec2 vB = data.velocities[m_indexB].v;
168 	float32 wB = data.velocities[m_indexB].w;
169 
170 	b2Vec2 vpA = vA + b2Cross(wA, m_rA);
171 	b2Vec2 vpB = vB + b2Cross(wB, m_rB);
172 
173 	float32 Cdot = -b2Dot(m_uA, vpA) - m_ratio * b2Dot(m_uB, vpB);
174 	float32 impulse = -m_mass * Cdot;
175 	m_impulse += impulse;
176 
177 	b2Vec2 PA = -impulse * m_uA;
178 	b2Vec2 PB = -m_ratio * impulse * m_uB;
179 	vA += m_invMassA * PA;
180 	wA += m_invIA * b2Cross(m_rA, PA);
181 	vB += m_invMassB * PB;
182 	wB += m_invIB * b2Cross(m_rB, PB);
183 
184 	data.velocities[m_indexA].v = vA;
185 	data.velocities[m_indexA].w = wA;
186 	data.velocities[m_indexB].v = vB;
187 	data.velocities[m_indexB].w = wB;
188 }
189 
SolvePositionConstraints(const b2SolverData & data)190 bool b2PulleyJoint::SolvePositionConstraints(const b2SolverData& data)
191 {
192 	b2Vec2 cA = data.positions[m_indexA].c;
193 	float32 aA = data.positions[m_indexA].a;
194 	b2Vec2 cB = data.positions[m_indexB].c;
195 	float32 aB = data.positions[m_indexB].a;
196 
197 	b2Rot qA(aA), qB(aB);
198 
199 	b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
200 	b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
201 
202 	// Get the pulley axes.
203 	b2Vec2 uA = cA + rA - m_groundAnchorA;
204 	b2Vec2 uB = cB + rB - m_groundAnchorB;
205 
206 	float32 lengthA = uA.Length();
207 	float32 lengthB = uB.Length();
208 
209 	if (lengthA > 10.0f * b2_linearSlop)
210 	{
211 		uA *= 1.0f / lengthA;
212 	}
213 	else
214 	{
215 		uA.SetZero();
216 	}
217 
218 	if (lengthB > 10.0f * b2_linearSlop)
219 	{
220 		uB *= 1.0f / lengthB;
221 	}
222 	else
223 	{
224 		uB.SetZero();
225 	}
226 
227 	// Compute effective mass.
228 	float32 ruA = b2Cross(rA, uA);
229 	float32 ruB = b2Cross(rB, uB);
230 
231 	float32 mA = m_invMassA + m_invIA * ruA * ruA;
232 	float32 mB = m_invMassB + m_invIB * ruB * ruB;
233 
234 	float32 mass = mA + m_ratio * m_ratio * mB;
235 
236 	if (mass > 0.0f)
237 	{
238 		mass = 1.0f / mass;
239 	}
240 
241 	float32 C = m_constant - lengthA - m_ratio * lengthB;
242 	float32 linearError = b2Abs(C);
243 
244 	float32 impulse = -mass * C;
245 
246 	b2Vec2 PA = -impulse * uA;
247 	b2Vec2 PB = -m_ratio * impulse * uB;
248 
249 	cA += m_invMassA * PA;
250 	aA += m_invIA * b2Cross(rA, PA);
251 	cB += m_invMassB * PB;
252 	aB += m_invIB * b2Cross(rB, PB);
253 
254 	data.positions[m_indexA].c = cA;
255 	data.positions[m_indexA].a = aA;
256 	data.positions[m_indexB].c = cB;
257 	data.positions[m_indexB].a = aB;
258 
259 	return linearError < b2_linearSlop;
260 }
261 
GetAnchorA() const262 b2Vec2 b2PulleyJoint::GetAnchorA() const
263 {
264 	return m_bodyA->GetWorldPoint(m_localAnchorA);
265 }
266 
GetAnchorB() const267 b2Vec2 b2PulleyJoint::GetAnchorB() const
268 {
269 	return m_bodyB->GetWorldPoint(m_localAnchorB);
270 }
271 
GetReactionForce(float32 inv_dt) const272 b2Vec2 b2PulleyJoint::GetReactionForce(float32 inv_dt) const
273 {
274 	b2Vec2 P = m_impulse * m_uB;
275 	return inv_dt * P;
276 }
277 
GetReactionTorque(float32 inv_dt) const278 float32 b2PulleyJoint::GetReactionTorque(float32 inv_dt) const
279 {
280 	B2_NOT_USED(inv_dt);
281 	return 0.0f;
282 }
283 
GetGroundAnchorA() const284 b2Vec2 b2PulleyJoint::GetGroundAnchorA() const
285 {
286 	return m_groundAnchorA;
287 }
288 
GetGroundAnchorB() const289 b2Vec2 b2PulleyJoint::GetGroundAnchorB() const
290 {
291 	return m_groundAnchorB;
292 }
293 
GetLengthA() const294 float32 b2PulleyJoint::GetLengthA() const
295 {
296 	return m_lengthA;
297 }
298 
GetLengthB() const299 float32 b2PulleyJoint::GetLengthB() const
300 {
301 	return m_lengthB;
302 }
303 
GetRatio() const304 float32 b2PulleyJoint::GetRatio() const
305 {
306 	return m_ratio;
307 }
308 
GetCurrentLengthA() const309 float32 b2PulleyJoint::GetCurrentLengthA() const
310 {
311 	b2Vec2 p = m_bodyA->GetWorldPoint(m_localAnchorA);
312 	b2Vec2 s = m_groundAnchorA;
313 	b2Vec2 d = p - s;
314 	return d.Length();
315 }
316 
GetCurrentLengthB() const317 float32 b2PulleyJoint::GetCurrentLengthB() const
318 {
319 	b2Vec2 p = m_bodyB->GetWorldPoint(m_localAnchorB);
320 	b2Vec2 s = m_groundAnchorB;
321 	b2Vec2 d = p - s;
322 	return d.Length();
323 }
324 
Dump()325 void b2PulleyJoint::Dump()
326 {
327 	int32 indexA = m_bodyA->m_islandIndex;
328 	int32 indexB = m_bodyB->m_islandIndex;
329 
330 	b2Log("  b2PulleyJointDef jd;\n");
331 	b2Log("  jd.bodyA = bodies[%d];\n", indexA);
332 	b2Log("  jd.bodyB = bodies[%d];\n", indexB);
333 	b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
334 	b2Log("  jd.groundAnchorA.Set(%.15lef, %.15lef);\n", m_groundAnchorA.x, m_groundAnchorA.y);
335 	b2Log("  jd.groundAnchorB.Set(%.15lef, %.15lef);\n", m_groundAnchorB.x, m_groundAnchorB.y);
336 	b2Log("  jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
337 	b2Log("  jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
338 	b2Log("  jd.lengthA = %.15lef;\n", m_lengthA);
339 	b2Log("  jd.lengthB = %.15lef;\n", m_lengthB);
340 	b2Log("  jd.ratio = %.15lef;\n", m_ratio);
341 	b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
342 }
343 
ShiftOrigin(const b2Vec2 & newOrigin)344 void b2PulleyJoint::ShiftOrigin(const b2Vec2& newOrigin)
345 {
346 	m_groundAnchorA -= newOrigin;
347 	m_groundAnchorB -= newOrigin;
348 }
349