b2WeldJoint.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2011 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 
20 #include <Box2D/Dynamics/b2Body.h>
22 
23 // Point-to-point constraint
24 // C = p2 - p1
25 // Cdot = v2 - v1
26 // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
27 // J = [-I -r1_skew I r2_skew ]
28 // Identity used:
29 // w k % (rx i + ry j) = w * (-ry i + rx j)
30 
31 // Angle constraint
32 // C = angle2 - angle1 - referenceAngle
33 // Cdot = w2 - w1
34 // J = [0 0 -1 0 0 1]
35 // K = invI1 + invI2
36 
37 void b2WeldJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
38 {
39  bodyA = bA;
40  bodyB = bB;
41  localAnchorA = bodyA->GetLocalPoint(anchor);
42  localAnchorB = bodyB->GetLocalPoint(anchor);
44 }
45 
47 : b2Joint(def)
48 {
54 
56 }
57 
59 {
68 
69  float32 aA = data.positions[m_indexA].a;
70  b2Vec2 vA = data.velocities[m_indexA].v;
71  float32 wA = data.velocities[m_indexA].w;
72 
73  float32 aB = data.positions[m_indexB].a;
74  b2Vec2 vB = data.velocities[m_indexB].v;
75  float32 wB = data.velocities[m_indexB].w;
76 
77  b2Rot qA(aA), qB(aB);
78 
81 
82  // J = [-I -r1_skew I r2_skew]
83  // [ 0 -1 0 1]
84  // r_skew = [-ry; rx]
85 
86  // Matlab
87  // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
88  // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
89  // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
90 
91  float32 mA = m_invMassA, mB = m_invMassB;
92  float32 iA = m_invIA, iB = m_invIB;
93 
94  b2Mat33 K;
95  K.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
96  K.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
97  K.ez.x = -m_rA.y * iA - m_rB.y * iB;
98  K.ex.y = K.ey.x;
99  K.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
100  K.ez.y = m_rA.x * iA + m_rB.x * iB;
101  K.ex.z = K.ez.x;
102  K.ey.z = K.ez.y;
103  K.ez.z = iA + iB;
104 
105  if (m_frequencyHz > 0.0f)
106  {
107  K.GetInverse22(&m_mass);
108 
109  float32 invM = iA + iB;
110  float32 m = invM > 0.0f ? 1.0f / invM : 0.0f;
111 
112  float32 C = aB - aA - m_referenceAngle;
113 
114  // Frequency
115  float32 omega = 2.0f * b2_pi * m_frequencyHz;
116 
117  // Damping coefficient
118  float32 d = 2.0f * m * m_dampingRatio * omega;
119 
120  // Spring stiffness
121  float32 k = m * omega * omega;
122 
123  // magic formulas
124  float32 h = data.step.dt;
125  m_gamma = h * (d + h * k);
126  m_gamma = m_gamma != 0.0f ? 1.0f / m_gamma : 0.0f;
127  m_bias = C * h * k * m_gamma;
128 
129  invM += m_gamma;
130  m_mass.ez.z = invM != 0.0f ? 1.0f / invM : 0.0f;
131  }
132  else if (K.ez.z == 0.0f)
133  {
134  K.GetInverse22(&m_mass);
135  m_gamma = 0.0f;
136  m_bias = 0.0f;
137  }
138  else
139  {
141  m_gamma = 0.0f;
142  m_bias = 0.0f;
143  }
144 
145  if (data.step.warmStarting)
146  {
147  // Scale impulses to support a variable time step.
148  m_impulse *= data.step.dtRatio;
149 
151 
152  vA -= mA * P;
153  wA -= iA * (b2Cross(m_rA, P) + m_impulse.z);
154 
155  vB += mB * P;
156  wB += iB * (b2Cross(m_rB, P) + m_impulse.z);
157  }
158  else
159  {
160  m_impulse.SetZero();
161  }
162 
163  data.velocities[m_indexA].v = vA;
164  data.velocities[m_indexA].w = wA;
165  data.velocities[m_indexB].v = vB;
166  data.velocities[m_indexB].w = wB;
167 }
168 
170 {
171  b2Vec2 vA = data.velocities[m_indexA].v;
172  float32 wA = data.velocities[m_indexA].w;
173  b2Vec2 vB = data.velocities[m_indexB].v;
174  float32 wB = data.velocities[m_indexB].w;
175 
176  float32 mA = m_invMassA, mB = m_invMassB;
177  float32 iA = m_invIA, iB = m_invIB;
178 
179  if (m_frequencyHz > 0.0f)
180  {
181  float32 Cdot2 = wB - wA;
182 
183  float32 impulse2 = -m_mass.ez.z * (Cdot2 + m_bias + m_gamma * m_impulse.z);
184  m_impulse.z += impulse2;
185 
186  wA -= iA * impulse2;
187  wB += iB * impulse2;
188 
189  b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
190 
191  b2Vec2 impulse1 = -b2Mul22(m_mass, Cdot1);
192  m_impulse.x += impulse1.x;
193  m_impulse.y += impulse1.y;
194 
195  b2Vec2 P = impulse1;
196 
197  vA -= mA * P;
198  wA -= iA * b2Cross(m_rA, P);
199 
200  vB += mB * P;
201  wB += iB * b2Cross(m_rB, P);
202  }
203  else
204  {
205  b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
206  float32 Cdot2 = wB - wA;
207  b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2);
208 
209  b2Vec3 impulse = -b2Mul(m_mass, Cdot);
210  m_impulse += impulse;
211 
212  b2Vec2 P(impulse.x, impulse.y);
213 
214  vA -= mA * P;
215  wA -= iA * (b2Cross(m_rA, P) + impulse.z);
216 
217  vB += mB * P;
218  wB += iB * (b2Cross(m_rB, P) + impulse.z);
219  }
220 
221  data.velocities[m_indexA].v = vA;
222  data.velocities[m_indexA].w = wA;
223  data.velocities[m_indexB].v = vB;
224  data.velocities[m_indexB].w = wB;
225 }
226 
228 {
229  b2Vec2 cA = data.positions[m_indexA].c;
230  float32 aA = data.positions[m_indexA].a;
231  b2Vec2 cB = data.positions[m_indexB].c;
232  float32 aB = data.positions[m_indexB].a;
233 
234  b2Rot qA(aA), qB(aB);
235 
236  float32 mA = m_invMassA, mB = m_invMassB;
237  float32 iA = m_invIA, iB = m_invIB;
238 
241 
242  float32 positionError, angularError;
243 
244  b2Mat33 K;
245  K.ex.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
246  K.ey.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
247  K.ez.x = -rA.y * iA - rB.y * iB;
248  K.ex.y = K.ey.x;
249  K.ey.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
250  K.ez.y = rA.x * iA + rB.x * iB;
251  K.ex.z = K.ez.x;
252  K.ey.z = K.ez.y;
253  K.ez.z = iA + iB;
254 
255  if (m_frequencyHz > 0.0f)
256  {
257  b2Vec2 C1 = cB + rB - cA - rA;
258 
259  positionError = C1.Length();
260  angularError = 0.0f;
261 
262  b2Vec2 P = -K.Solve22(C1);
263 
264  cA -= mA * P;
265  aA -= iA * b2Cross(rA, P);
266 
267  cB += mB * P;
268  aB += iB * b2Cross(rB, P);
269  }
270  else
271  {
272  b2Vec2 C1 = cB + rB - cA - rA;
273  float32 C2 = aB - aA - m_referenceAngle;
274 
275  positionError = C1.Length();
276  angularError = b2Abs(C2);
277 
278  b2Vec3 C(C1.x, C1.y, C2);
279 
280  b2Vec3 impulse;
281  if (K.ez.z > 0.0f)
282  {
283  impulse = -K.Solve33(C);
284  }
285  else
286  {
287  b2Vec2 impulse2 = -K.Solve22(C1);
288  impulse.Set(impulse2.x, impulse2.y, 0.0f);
289  }
290 
291  b2Vec2 P(impulse.x, impulse.y);
292 
293  cA -= mA * P;
294  aA -= iA * (b2Cross(rA, P) + impulse.z);
295 
296  cB += mB * P;
297  aB += iB * (b2Cross(rB, P) + impulse.z);
298  }
299 
300  data.positions[m_indexA].c = cA;
301  data.positions[m_indexA].a = aA;
302  data.positions[m_indexB].c = cB;
303  data.positions[m_indexB].a = aB;
304 
305  return positionError <= b2_linearSlop && angularError <= b2_angularSlop;
306 }
307 
309 {
311 }
312 
314 {
316 }
317 
319 {
321  return inv_dt * P;
322 }
323 
325 {
326  return inv_dt * m_impulse.z;
327 }
328 
330 {
331  int32 indexA = m_bodyA->m_islandIndex;
332  int32 indexB = m_bodyB->m_islandIndex;
333 
334  b2Log(" b2WeldJointDef jd;\n");
335  b2Log(" jd.bodyA = bodies[%d];\n", indexA);
336  b2Log(" jd.bodyB = bodies[%d];\n", indexB);
337  b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
338  b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
339  b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
340  b2Log(" jd.referenceAngle = %.15lef;\n", m_referenceAngle);
341  b2Log(" jd.frequencyHz = %.15lef;\n", m_frequencyHz);
342  b2Log(" jd.dampingRatio = %.15lef;\n", m_dampingRatio);
343  b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
344 }
d
b2Velocity * velocities
Definition: b2TimeStep.h:67
float32 m_invMass
Definition: b2Body.h:455
int32 m_islandIndex
Definition: b2Body.h:434
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2Math.h:432
b2Vec2 m_localAnchorA
Definition: b2WeldJoint.h:106
void SolveVelocityConstraints(const b2SolverData &data)
float32 referenceAngle
The bodyB angle minus bodyA angle in the reference state (radians).
Definition: b2WeldJoint.h:50
float32 m_invMassA
Definition: b2WeldJoint.h:119
void b2Log(const char *string,...)
Logging function.
Definition: b2Settings.cpp:38
b2Vec3 ex
Definition: b2Math.h:294
b2Vec2 m_rB
Definition: b2WeldJoint.h:116
float32 a
Definition: b2TimeStep.h:52
#define b2_linearSlop
Definition: b2Settings.h:68
float32 m_dampingRatio
Definition: b2WeldJoint.h:102
#define b2_pi
Definition: b2Settings.h:40
b2Vec2 localAnchorB
The local anchor point relative to bodyB&#39;s origin.
Definition: b2WeldJoint.h:47
void GetInverse22(b2Mat33 *M) const
Definition: b2Math.cpp:56
b2TimeStep step
Definition: b2TimeStep.h:65
b2Vec2 c
Definition: b2TimeStep.h:51
float32 w
Definition: b2TimeStep.h:59
bool m_collideConnected
Definition: b2Joint.h:181
float32 dtRatio
Definition: b2TimeStep.h:42
float32 y
Definition: b2Math.h:178
float32 dampingRatio
The damping ratio. 0 = no damping, 1 = critical damping.
Definition: b2WeldJoint.h:57
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition: b2Body.h:556
float32 m_referenceAngle
Definition: b2WeldJoint.h:108
float32 GetAngle() const
Definition: b2Body.h:484
b2Vec3 ez
Definition: b2Math.h:294
Solver Data.
Definition: b2TimeStep.h:63
b2Vec2 m_localCenterA
Definition: b2WeldJoint.h:117
int32 m_index
Definition: b2Joint.h:178
A 2D column vector.
Definition: b2Math.h:52
b2Vec2 b2Mul22(const b2Mat33 &A, const b2Vec2 &v)
Multiply a matrix times a vector.
Definition: b2Math.h:533
signed int int32
Definition: b2Settings.h:31
b2Vec2 localCenter
local center of mass position
Definition: b2Math.h:392
float32 b2Cross(const b2Vec2 &a, const b2Vec2 &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: b2Math.h:411
float32 m_bias
Definition: b2WeldJoint.h:103
A 2D column vector with 3 elements.
Definition: b2Math.h:143
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:152
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
b2Vec3 ey
Definition: b2Math.h:294
void Dump()
Dump to b2Log.
b2Vec2 GetAnchorA() const
Get the anchor point on bodyA in world coordinates.
b2Vec2 v
Definition: b2TimeStep.h:58
float32 m_invMassB
Definition: b2WeldJoint.h:120
b2Vec2 m_localCenterB
Definition: b2WeldJoint.h:118
int32 m_indexB
Definition: b2WeldJoint.h:114
float32 GetReactionTorque(float32 inv_dt) const
Get the reaction torque on bodyB in N*m.
int32 m_indexA
Definition: b2WeldJoint.h:113
float32 x
Definition: b2Math.h:178
b2Vec2 localAnchorA
The local anchor point relative to bodyA&#39;s origin.
Definition: b2WeldJoint.h:44
float32 m_invI
Definition: b2Body.h:458
void InitVelocityConstraints(const b2SolverData &data)
Definition: b2WeldJoint.cpp:58
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
b2Vec2 GetReactionForce(float32 inv_dt) const
Get the reaction force on bodyB at the joint anchor in Newtons.
b2Vec3 m_impulse
Definition: b2WeldJoint.h:110
float32 m_invIA
Definition: b2WeldJoint.h:121
b2Body * m_bodyA
Definition: b2Joint.h:175
float32 m_invIB
Definition: b2WeldJoint.h:122
#define b2_angularSlop
Definition: b2Settings.h:72
float32 m_frequencyHz
Definition: b2WeldJoint.h:101
A 3-by-3 matrix. Stored in column-major order.
Definition: b2Math.h:256
float32 y
Definition: b2Math.h:139
b2Vec2 GetLocalPoint(const b2Vec2 &worldPoint) const
Definition: b2Body.h:566
b2Mat33 m_mass
Definition: b2WeldJoint.h:123
b2Position * positions
Definition: b2TimeStep.h:66
b2Vec2 Solve22(const b2Vec2 &b) const
Definition: b2Math.cpp:41
b2WeldJoint(const b2WeldJointDef *def)
Definition: b2WeldJoint.cpp:46
b2Vec3 Solve33(const b2Vec3 &b) const
Definition: b2Math.cpp:25
float32 m_gamma
Definition: b2WeldJoint.h:109
b2Vec2 m_localAnchorB
Definition: b2WeldJoint.h:107
float32 frequencyHz
Definition: b2WeldJoint.h:54
const GLdouble * m
T b2Abs(T a)
Definition: b2Math.h:615
void GetSymInverse33(b2Mat33 *M) const
Returns the zero matrix if singular.
Definition: b2Math.cpp:71
bool warmStarting
Definition: b2TimeStep.h:45
Rotation.
Definition: b2Math.h:298
void Set(float32 x_, float32 y_, float32 z_)
Set this vector to some specified coordinates.
Definition: b2Math.h:155
float32 z
Definition: b2Math.h:178
bool SolvePositionConstraints(const b2SolverData &data)
float32 x
Definition: b2Math.h:139
b2Body * bodyA
The first attached body.
Definition: b2Joint.h:92
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
Definition: b2WeldJoint.cpp:37
float32 dt
Definition: b2TimeStep.h:40
float32 Length() const
Get the length of this vector (the norm).
Definition: b2Math.h:100
b2Body * bodyB
The second attached body.
Definition: b2Joint.h:95
b2Body * m_bodyB
Definition: b2Joint.h:176
b2Sweep m_sweep
Definition: b2Body.h:437
float float32
Definition: b2Settings.h:35
b2Vec2 GetAnchorB() const
Get the anchor point on bodyB in world coordinates.
b2Vec2 m_rA
Definition: b2WeldJoint.h:115
CArrayDouble< 6 > C
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


mvsim
Author(s):
autogenerated on Fri May 7 2021 03:05:51