b2MotorJoint.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2006-2012 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 // Cdot = v2 - v1
25 // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
26 // J = [-I -r1_skew I r2_skew ]
27 // Identity used:
28 // w k % (rx i + ry j) = w * (-ry i + rx j)
29 
30 // Angle constraint
31 // Cdot = w2 - w1
32 // J = [0 0 -1 0 0 1]
33 // K = invI1 + invI2
34 
36 {
37  bodyA = bA;
38  bodyB = bB;
39  b2Vec2 xB = bodyB->GetPosition();
41 
42  float32 angleA = bodyA->GetAngle();
43  float32 angleB = bodyB->GetAngle();
44  angularOffset = angleB - angleA;
45 }
46 
48 : b2Joint(def)
49 {
52 
54  m_angularImpulse = 0.0f;
55 
56  m_maxForce = def->maxForce;
57  m_maxTorque = def->maxTorque;
59 }
60 
62 {
71 
72  b2Vec2 cA = data.positions[m_indexA].c;
73  float32 aA = data.positions[m_indexA].a;
74  b2Vec2 vA = data.velocities[m_indexA].v;
75  float32 wA = data.velocities[m_indexA].w;
76 
77  b2Vec2 cB = data.positions[m_indexB].c;
78  float32 aB = data.positions[m_indexB].a;
79  b2Vec2 vB = data.velocities[m_indexB].v;
80  float32 wB = data.velocities[m_indexB].w;
81 
82  b2Rot qA(aA), qB(aB);
83 
84  // Compute the effective mass matrix.
85  m_rA = b2Mul(qA, -m_localCenterA);
86  m_rB = b2Mul(qB, -m_localCenterB);
87 
88  // J = [-I -r1_skew I r2_skew]
89  // [ 0 -1 0 1]
90  // r_skew = [-ry; rx]
91 
92  // Matlab
93  // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
94  // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
95  // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
96 
97  float32 mA = m_invMassA, mB = m_invMassB;
98  float32 iA = m_invIA, iB = m_invIB;
99 
100  b2Mat22 K;
101  K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
102  K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
103  K.ey.x = K.ex.y;
104  K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
105 
106  m_linearMass = K.GetInverse();
107 
108  m_angularMass = iA + iB;
109  if (m_angularMass > 0.0f)
110  {
111  m_angularMass = 1.0f / m_angularMass;
112  }
113 
114  m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset);
115  m_angularError = aB - aA - m_angularOffset;
116 
117  if (data.step.warmStarting)
118  {
119  // Scale impulses to support a variable time step.
120  m_linearImpulse *= data.step.dtRatio;
121  m_angularImpulse *= data.step.dtRatio;
122 
124  vA -= mA * P;
125  wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
126  vB += mB * P;
127  wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
128  }
129  else
130  {
132  m_angularImpulse = 0.0f;
133  }
134 
135  data.velocities[m_indexA].v = vA;
136  data.velocities[m_indexA].w = wA;
137  data.velocities[m_indexB].v = vB;
138  data.velocities[m_indexB].w = wB;
139 }
140 
142 {
143  b2Vec2 vA = data.velocities[m_indexA].v;
144  float32 wA = data.velocities[m_indexA].w;
145  b2Vec2 vB = data.velocities[m_indexB].v;
146  float32 wB = data.velocities[m_indexB].w;
147 
148  float32 mA = m_invMassA, mB = m_invMassB;
149  float32 iA = m_invIA, iB = m_invIB;
150 
151  float32 h = data.step.dt;
152  float32 inv_h = data.step.inv_dt;
153 
154  // Solve angular friction
155  {
156  float32 Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError;
157  float32 impulse = -m_angularMass * Cdot;
158 
159  float32 oldImpulse = m_angularImpulse;
160  float32 maxImpulse = h * m_maxTorque;
161  m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
162  impulse = m_angularImpulse - oldImpulse;
163 
164  wA -= iA * impulse;
165  wB += iB * impulse;
166  }
167 
168  // Solve linear friction
169  {
170  b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor * m_linearError;
171 
172  b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
173  b2Vec2 oldImpulse = m_linearImpulse;
174  m_linearImpulse += impulse;
175 
176  float32 maxImpulse = h * m_maxForce;
177 
178  if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
179  {
181  m_linearImpulse *= maxImpulse;
182  }
183 
184  impulse = m_linearImpulse - oldImpulse;
185 
186  vA -= mA * impulse;
187  wA -= iA * b2Cross(m_rA, impulse);
188 
189  vB += mB * impulse;
190  wB += iB * b2Cross(m_rB, impulse);
191  }
192 
193  data.velocities[m_indexA].v = vA;
194  data.velocities[m_indexA].w = wA;
195  data.velocities[m_indexB].v = vB;
196  data.velocities[m_indexB].w = wB;
197 }
198 
200 {
201  B2_NOT_USED(data);
202 
203  return true;
204 }
205 
207 {
208  return m_bodyA->GetPosition();
209 }
210 
212 {
213  return m_bodyB->GetPosition();
214 }
215 
217 {
218  return inv_dt * m_linearImpulse;
219 }
220 
222 {
223  return inv_dt * m_angularImpulse;
224 }
225 
227 {
228  b2Assert(b2IsValid(force) && force >= 0.0f);
229  m_maxForce = force;
230 }
231 
233 {
234  return m_maxForce;
235 }
236 
238 {
239  b2Assert(b2IsValid(torque) && torque >= 0.0f);
240  m_maxTorque = torque;
241 }
242 
244 {
245  return m_maxTorque;
246 }
247 
249 {
250  b2Assert(b2IsValid(factor) && 0.0f <= factor && factor <= 1.0f);
251  m_correctionFactor = factor;
252 }
253 
255 {
256  return m_correctionFactor;
257 }
258 
259 void b2MotorJoint::SetLinearOffset(const b2Vec2& linearOffset)
260 {
261  if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y)
262  {
263  m_bodyA->SetAwake(true);
264  m_bodyB->SetAwake(true);
265  m_linearOffset = linearOffset;
266  }
267 }
268 
270 {
271  return m_linearOffset;
272 }
273 
275 {
276  if (angularOffset != m_angularOffset)
277  {
278  m_bodyA->SetAwake(true);
279  m_bodyB->SetAwake(true);
280  m_angularOffset = angularOffset;
281  }
282 }
283 
285 {
286  return m_angularOffset;
287 }
288 
290 {
291  int32 indexA = m_bodyA->m_islandIndex;
292  int32 indexB = m_bodyB->m_islandIndex;
293 
294  b2Log(" b2MotorJointDef jd;\n");
295  b2Log(" jd.bodyA = bodies[%d];\n", indexA);
296  b2Log(" jd.bodyB = bodies[%d];\n", indexB);
297  b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
298  b2Log(" jd.linearOffset.Set(%.15lef, %.15lef);\n", m_linearOffset.x, m_linearOffset.y);
299  b2Log(" jd.angularOffset = %.15lef;\n", m_angularOffset);
300  b2Log(" jd.maxForce = %.15lef;\n", m_maxForce);
301  b2Log(" jd.maxTorque = %.15lef;\n", m_maxTorque);
302  b2Log(" jd.correctionFactor = %.15lef;\n", m_correctionFactor);
303  b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
304 }
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
void b2Log(const char *string,...)
Logging function.
Definition: b2Settings.cpp:38
float32 a
Definition: b2TimeStep.h:52
void SetMaxTorque(float32 torque)
Set the maximum friction torque in N*m.
float32 m_angularOffset
Definition: b2MotorJoint.h:109
b2TimeStep step
Definition: b2TimeStep.h:65
void Initialize(b2Body *bodyA, b2Body *bodyB)
Initialize the bodies and offsets using the current transforms.
float32 m_maxTorque
Definition: b2MotorJoint.h:113
float32 maxTorque
The maximum motor torque in N-m.
Definition: b2MotorJoint.h:50
float32 GetAngularOffset() const
b2Vec2 linearOffset
Position of bodyB minus the position of bodyA, in bodyA&#39;s frame, in meters.
Definition: b2MotorJoint.h:41
b2Vec2 GetAnchorB() const
Get the anchor point on bodyB in world coordinates.
b2Vec2 m_localCenterB
Definition: b2MotorJoint.h:122
b2Vec2 c
Definition: b2TimeStep.h:51
float32 GetReactionTorque(float32 inv_dt) const
Get the reaction torque on bodyB in N*m.
float32 w
Definition: b2TimeStep.h:59
bool m_collideConnected
Definition: b2Joint.h:181
#define B2_NOT_USED(x)
Definition: b2Settings.h:26
float32 dtRatio
Definition: b2TimeStep.h:42
float32 LengthSquared() const
Definition: b2Math.h:107
b2Vec2 GetReactionForce(float32 inv_dt) const
Get the reaction force on bodyB at the joint anchor in Newtons.
const b2Vec2 & GetLinearOffset() const
float32 m_invIA
Definition: b2MotorJoint.h:127
float32 GetAngle() const
Definition: b2Body.h:484
Solver Data.
Definition: b2TimeStep.h:63
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:61
float32 inv_dt
Definition: b2TimeStep.h:41
int32 m_index
Definition: b2Joint.h:178
A 2D column vector.
Definition: b2Math.h:52
b2Vec2 ey
Definition: b2Math.h:252
void SetAngularOffset(float32 angularOffset)
Set/get the target angular offset, in radians.
float32 m_invMassB
Definition: b2MotorJoint.h:126
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
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:126
b2Vec2 v
Definition: b2TimeStep.h:58
float32 m_maxForce
Definition: b2MotorJoint.h:112
bool SolvePositionConstraints(const b2SolverData &data)
bool b2IsValid(float32 x)
This function is used to ensure that a floating point number is not a NaN or infinity.
Definition: b2Math.h:26
b2Vec2 GetAnchorA() const
Get the anchor point on bodyA in world coordinates.
float32 GetCorrectionFactor() const
Get the position correction factor in the range [0,1].
b2MotorJoint(const b2MotorJointDef *def)
void SetLinearOffset(const b2Vec2 &linearOffset)
Set/get the target linear offset, in frame A, in meters.
float32 m_invI
Definition: b2Body.h:458
void SolveVelocityConstraints(const b2SolverData &data)
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
float32 correctionFactor
Position correction factor in the range [0,1].
Definition: b2MotorJoint.h:53
b2Mat22 m_linearMass
Definition: b2MotorJoint.h:129
void Dump()
Dump to b2Log.
float32 maxForce
The maximum motor force in N.
Definition: b2MotorJoint.h:47
b2Body * m_bodyA
Definition: b2Joint.h:175
float32 m_angularImpulse
Definition: b2MotorJoint.h:111
float32 m_correctionFactor
Definition: b2MotorJoint.h:114
void SetMaxForce(float32 force)
Set the maximum friction force in N.
b2Vec2 m_localCenterA
Definition: b2MotorJoint.h:121
float32 m_angularError
Definition: b2MotorJoint.h:124
float32 y
Definition: b2Math.h:139
b2Vec2 GetLocalPoint(const b2Vec2 &worldPoint) const
Definition: b2Body.h:566
float32 GetMaxTorque() const
Get the maximum friction torque in N*m.
b2Position * positions
Definition: b2TimeStep.h:66
b2Vec2 m_linearError
Definition: b2MotorJoint.h:123
#define b2Assert(A)
Definition: b2Settings.h:27
T b2Clamp(T a, T low, T high)
Definition: b2Math.h:653
float32 m_angularMass
Definition: b2MotorJoint.h:130
void InitVelocityConstraints(const b2SolverData &data)
b2Vec2 ex
Definition: b2Math.h:252
b2Vec2 m_linearImpulse
Definition: b2MotorJoint.h:110
A 2-by-2 matrix. Stored in column-major order.
Definition: b2Math.h:182
const b2Vec2 & GetPosition() const
Definition: b2Body.h:479
bool warmStarting
Definition: b2TimeStep.h:45
Rotation.
Definition: b2Math.h:298
float32 m_invIB
Definition: b2MotorJoint.h:128
float32 x
Definition: b2Math.h:139
float32 m_invMassA
Definition: b2MotorJoint.h:125
void SetCorrectionFactor(float32 factor)
Set the position correction factor in the range [0,1].
b2Body * bodyA
The first attached body.
Definition: b2Joint.h:92
float32 Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2Math.h:113
float32 GetMaxForce() const
Get the maximum friction force in N.
float32 dt
Definition: b2TimeStep.h:40
b2Vec2 m_linearOffset
Definition: b2MotorJoint.h:108
b2Mat22 GetInverse() const
Definition: b2Math.h:222
void SetAwake(bool flag)
Definition: b2Body.h:633
Motor joint definition.
Definition: b2MotorJoint.h:25
float32 angularOffset
The bodyB angle minus bodyA angle in radians.
Definition: b2MotorJoint.h:44
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
GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble f


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