b2_motor_joint.cpp
Go to the documentation of this file.
1 // MIT License
2 
3 // Copyright (c) 2019 Erin Catto
4 
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #include "box2d/b2_body.h"
24 #include "box2d/b2_motor_joint.h"
25 #include "box2d/b2_time_step.h"
26 
27 // Point-to-point constraint
28 // Cdot = v2 - v1
29 // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
30 // J = [-I -r1_skew I r2_skew ]
31 // Identity used:
32 // w k % (rx i + ry j) = w * (-ry i + rx j)
33 //
34 // r1 = offset - c1
35 // r2 = -c2
36 
37 // Angle constraint
38 // Cdot = w2 - w1
39 // J = [0 0 -1 0 0 1]
40 // K = invI1 + invI2
41 
43 {
44  bodyA = bA;
45  bodyB = bB;
46  b2Vec2 xB = bodyB->GetPosition();
48 
49  float angleA = bodyA->GetAngle();
50  float angleB = bodyB->GetAngle();
51  angularOffset = angleB - angleA;
52 }
53 
55 : b2Joint(def)
56 {
59 
61  m_angularImpulse = 0.0f;
62 
63  m_maxForce = def->maxForce;
64  m_maxTorque = def->maxTorque;
66 }
67 
69 {
78 
79  b2Vec2 cA = data.positions[m_indexA].c;
80  float aA = data.positions[m_indexA].a;
81  b2Vec2 vA = data.velocities[m_indexA].v;
82  float wA = data.velocities[m_indexA].w;
83 
84  b2Vec2 cB = data.positions[m_indexB].c;
85  float aB = data.positions[m_indexB].a;
86  b2Vec2 vB = data.velocities[m_indexB].v;
87  float wB = data.velocities[m_indexB].w;
88 
89  b2Rot qA(aA), qB(aB);
90 
91  // Compute the effective mass matrix.
93  m_rB = b2Mul(qB, -m_localCenterB);
94 
95  // J = [-I -r1_skew I r2_skew]
96  // r_skew = [-ry; rx]
97 
98  // Matlab
99  // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
100  // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
101  // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
102 
103  float mA = m_invMassA, mB = m_invMassB;
104  float iA = m_invIA, iB = m_invIB;
105 
106  // Upper 2 by 2 of K for point to point
107  b2Mat22 K;
108  K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
109  K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
110  K.ey.x = K.ex.y;
111  K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
112 
113  m_linearMass = K.GetInverse();
114 
115  m_angularMass = iA + iB;
116  if (m_angularMass > 0.0f)
117  {
118  m_angularMass = 1.0f / m_angularMass;
119  }
120 
121  m_linearError = cB + m_rB - cA - m_rA;
122  m_angularError = aB - aA - m_angularOffset;
123 
124  if (data.step.warmStarting)
125  {
126  // Scale impulses to support a variable time step.
127  m_linearImpulse *= data.step.dtRatio;
128  m_angularImpulse *= data.step.dtRatio;
129 
131  vA -= mA * P;
132  wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
133  vB += mB * P;
134  wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
135  }
136  else
137  {
139  m_angularImpulse = 0.0f;
140  }
141 
142  data.velocities[m_indexA].v = vA;
143  data.velocities[m_indexA].w = wA;
144  data.velocities[m_indexB].v = vB;
145  data.velocities[m_indexB].w = wB;
146 }
147 
149 {
150  b2Vec2 vA = data.velocities[m_indexA].v;
151  float wA = data.velocities[m_indexA].w;
152  b2Vec2 vB = data.velocities[m_indexB].v;
153  float wB = data.velocities[m_indexB].w;
154 
155  float mA = m_invMassA, mB = m_invMassB;
156  float iA = m_invIA, iB = m_invIB;
157 
158  float h = data.step.dt;
159  float inv_h = data.step.inv_dt;
160 
161  // Solve angular friction
162  {
163  float Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError;
164  float impulse = -m_angularMass * Cdot;
165 
166  float oldImpulse = m_angularImpulse;
167  float maxImpulse = h * m_maxTorque;
168  m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
169  impulse = m_angularImpulse - oldImpulse;
170 
171  wA -= iA * impulse;
172  wB += iB * impulse;
173  }
174 
175  // Solve linear friction
176  {
177  b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor * m_linearError;
178 
179  b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
180  b2Vec2 oldImpulse = m_linearImpulse;
181  m_linearImpulse += impulse;
182 
183  float maxImpulse = h * m_maxForce;
184 
185  if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
186  {
188  m_linearImpulse *= maxImpulse;
189  }
190 
191  impulse = m_linearImpulse - oldImpulse;
192 
193  vA -= mA * impulse;
194  wA -= iA * b2Cross(m_rA, impulse);
195 
196  vB += mB * impulse;
197  wB += iB * b2Cross(m_rB, impulse);
198  }
199 
200  data.velocities[m_indexA].v = vA;
201  data.velocities[m_indexA].w = wA;
202  data.velocities[m_indexB].v = vB;
203  data.velocities[m_indexB].w = wB;
204 }
205 
207 {
208  B2_NOT_USED(data);
209 
210  return true;
211 }
212 
214 {
215  return m_bodyA->GetPosition();
216 }
217 
219 {
220  return m_bodyB->GetPosition();
221 }
222 
224 {
225  return inv_dt * m_linearImpulse;
226 }
227 
228 float b2MotorJoint::GetReactionTorque(float inv_dt) const
229 {
230  return inv_dt * m_angularImpulse;
231 }
232 
233 void b2MotorJoint::SetMaxForce(float force)
234 {
235  b2Assert(b2IsValid(force) && force >= 0.0f);
236  m_maxForce = force;
237 }
238 
240 {
241  return m_maxForce;
242 }
243 
244 void b2MotorJoint::SetMaxTorque(float torque)
245 {
246  b2Assert(b2IsValid(torque) && torque >= 0.0f);
247  m_maxTorque = torque;
248 }
249 
251 {
252  return m_maxTorque;
253 }
254 
256 {
257  b2Assert(b2IsValid(factor) && 0.0f <= factor && factor <= 1.0f);
258  m_correctionFactor = factor;
259 }
260 
262 {
263  return m_correctionFactor;
264 }
265 
266 void b2MotorJoint::SetLinearOffset(const b2Vec2& linearOffset)
267 {
268  if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y)
269  {
270  m_bodyA->SetAwake(true);
271  m_bodyB->SetAwake(true);
272  m_linearOffset = linearOffset;
273  }
274 }
275 
277 {
278  return m_linearOffset;
279 }
280 
281 void b2MotorJoint::SetAngularOffset(float angularOffset)
282 {
283  if (angularOffset != m_angularOffset)
284  {
285  m_bodyA->SetAwake(true);
286  m_bodyB->SetAwake(true);
287  m_angularOffset = angularOffset;
288  }
289 }
290 
292 {
293  return m_angularOffset;
294 }
295 
297 {
298  int32 indexA = m_bodyA->m_islandIndex;
299  int32 indexB = m_bodyB->m_islandIndex;
300 
301  b2Dump(" b2MotorJointDef jd;\n");
302  b2Dump(" jd.bodyA = bodies[%d];\n", indexA);
303  b2Dump(" jd.bodyB = bodies[%d];\n", indexB);
304  b2Dump(" jd.collideConnected = bool(%d);\n", m_collideConnected);
305  b2Dump(" jd.linearOffset.Set(%.9g, %.9g);\n", m_linearOffset.x, m_linearOffset.y);
306  b2Dump(" jd.angularOffset = %.9g;\n", m_angularOffset);
307  b2Dump(" jd.maxForce = %.9g;\n", m_maxForce);
308  b2Dump(" jd.maxTorque = %.9g;\n", m_maxTorque);
309  b2Dump(" jd.correctionFactor = %.9g;\n", m_correctionFactor);
310  b2Dump(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
311 }
void Dump() override
Dump to b2Log.
b2Velocity * velocities
Definition: b2_time_step.h:71
int32 m_islandIndex
Definition: b2_body.h:439
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
void SetAngularOffset(float angularOffset)
Set/get the target angular offset, in radians.
void SetMaxTorque(float torque)
Set the maximum friction torque in N*m.
float m_correctionFactor
f
b2TimeStep step
Definition: b2_time_step.h:69
b2Vec2 GetAnchorB() const override
Get the anchor point on bodyB in world coordinates.
void Initialize(b2Body *bodyA, b2Body *bodyB)
Initialize the bodies and offsets using the current transforms.
b2Vec2 linearOffset
Position of bodyB minus the position of bodyA, in bodyA&#39;s frame, in meters.
float x
Definition: b2_math.h:128
void InitVelocityConstraints(const b2SolverData &data) override
float y
Definition: b2_math.h:128
float angularOffset
The bodyB angle minus bodyA angle in radians.
b2Vec2 m_localCenterB
b2Vec2 c
Definition: b2_time_step.h:55
bool m_collideConnected
Definition: b2_joint.h:188
float m_angularError
void SetCorrectionFactor(float factor)
Set the position correction factor in the range [0,1].
float maxForce
The maximum motor force in N.
const b2Vec2 & GetPosition() const
Definition: b2_body.h:484
Solver Data.
Definition: b2_time_step.h:67
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
void SetMaxForce(float force)
Set the maximum friction force in N.
int32 m_index
Definition: b2_joint.h:185
A 2D column vector.
Definition: b2_math.h:41
const b2Vec2 & GetLinearOffset() const
float GetCorrectionFactor() const
Get the position correction factor in the range [0,1].
b2Vec2 GetAnchorA() const override
Get the anchor point on bodyA in world coordinates.
b2Vec2 ey
Definition: b2_math.h:241
signed int int32
Definition: b2_types.h:28
float m_invI
Definition: b2_body.h:463
b2Vec2 localCenter
local center of mass position
Definition: b2_math.h:382
float m_angularOffset
A rigid body. These are created via b2World::CreateBody.
Definition: b2_body.h:128
b2Mat22 GetInverse() const
Definition: b2_math.h:211
b2Vec2 v
Definition: b2_time_step.h:62
float GetAngularOffset() const
float LengthSquared() const
Definition: b2_math.h:96
b2MotorJoint(const b2MotorJointDef *def)
#define B2_NOT_USED(x)
Definition: b2_common.h:36
void SetLinearOffset(const b2Vec2 &linearOffset)
Set/get the target linear offset, in frame A, in meters.
float maxTorque
The maximum motor torque in N-m.
float b2Cross(const b2Vec2 &a, const b2Vec2 &b)
Perform the cross product on two vectors. In 2D this produces a scalar.
Definition: b2_math.h:401
b2Vec2 GetReactionForce(float inv_dt) const override
Get the reaction force on bodyB at the joint anchor in Newtons.
b2Mat22 m_linearMass
bool SolvePositionConstraints(const b2SolverData &data) override
bool b2IsValid(float x)
This function is used to ensure that a floating point number is not a NaN or infinity.
Definition: b2_math.h:32
float GetAngle() const
Definition: b2_body.h:489
void SolveVelocityConstraints(const b2SolverData &data) override
b2Body * m_bodyA
Definition: b2_joint.h:182
T b2Clamp(T a, T low, T high)
Definition: b2_math.h:648
b2Vec2 m_localCenterA
float GetMaxTorque() const
Get the maximum friction torque in N*m.
float GetReactionTorque(float inv_dt) const override
Get the reaction torque on bodyB in N*m.
float m_invMass
Definition: b2_body.h:460
b2Position * positions
Definition: b2_time_step.h:70
b2Vec2 GetLocalPoint(const b2Vec2 &worldPoint) const
Definition: b2_body.h:571
b2Vec2 m_linearError
float inv_dt
Definition: b2_time_step.h:45
void b2Dump(const char *string,...)
Definition: b2_settings.cpp:57
float GetMaxForce() const
Get the maximum friction force in N.
float Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2_math.h:102
b2Vec2 ex
Definition: b2_math.h:241
b2Vec2 m_linearImpulse
A 2-by-2 matrix. Stored in column-major order.
Definition: b2_math.h:171
bool warmStarting
Definition: b2_time_step.h:49
Rotation.
Definition: b2_math.h:287
float dtRatio
Definition: b2_time_step.h:46
b2Body * bodyA
The first attached body.
Definition: b2_joint.h:89
float correctionFactor
Position correction factor in the range [0,1].
b2Vec2 m_linearOffset
#define b2Assert(A)
Definition: b2_common.h:37
void SetAwake(bool flag)
Definition: b2_body.h:638
Motor joint definition.
b2Body * bodyB
The second attached body.
Definition: b2_joint.h:92
b2Body * m_bodyB
Definition: b2_joint.h:183
b2Sweep m_sweep
Definition: b2_body.h:442
float m_angularImpulse


mvsim
Author(s):
autogenerated on Tue Jul 4 2023 03:08:19