b2_friction_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 
24 #include "box2d/b2_body.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 // Angle constraint
35 // Cdot = w2 - w1
36 // J = [0 0 -1 0 0 1]
37 // K = invI1 + invI2
38 
39 void b2FrictionJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
40 {
41  bodyA = bA;
42  bodyB = bB;
43  localAnchorA = bodyA->GetLocalPoint(anchor);
44  localAnchorB = bodyB->GetLocalPoint(anchor);
45 }
46 
48 : b2Joint(def)
49 {
52 
54  m_angularImpulse = 0.0f;
55 
56  m_maxForce = def->maxForce;
57  m_maxTorque = def->maxTorque;
58 }
59 
61 {
70 
71  float aA = data.positions[m_indexA].a;
72  b2Vec2 vA = data.velocities[m_indexA].v;
73  float wA = data.velocities[m_indexA].w;
74 
75  float aB = data.positions[m_indexB].a;
76  b2Vec2 vB = data.velocities[m_indexB].v;
77  float wB = data.velocities[m_indexB].w;
78 
79  b2Rot qA(aA), qB(aB);
80 
81  // Compute the effective mass matrix.
84 
85  // J = [-I -r1_skew I r2_skew]
86  // [ 0 -1 0 1]
87  // r_skew = [-ry; rx]
88 
89  // Matlab
90  // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
91  // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
92  // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
93 
94  float mA = m_invMassA, mB = m_invMassB;
95  float iA = m_invIA, iB = m_invIB;
96 
97  b2Mat22 K;
98  K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
99  K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
100  K.ey.x = K.ex.y;
101  K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
102 
103  m_linearMass = K.GetInverse();
104 
105  m_angularMass = iA + iB;
106  if (m_angularMass > 0.0f)
107  {
108  m_angularMass = 1.0f / m_angularMass;
109  }
110 
111  if (data.step.warmStarting)
112  {
113  // Scale impulses to support a variable time step.
114  m_linearImpulse *= data.step.dtRatio;
115  m_angularImpulse *= data.step.dtRatio;
116 
118  vA -= mA * P;
119  wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
120  vB += mB * P;
121  wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
122  }
123  else
124  {
126  m_angularImpulse = 0.0f;
127  }
128 
129  data.velocities[m_indexA].v = vA;
130  data.velocities[m_indexA].w = wA;
131  data.velocities[m_indexB].v = vB;
132  data.velocities[m_indexB].w = wB;
133 }
134 
136 {
137  b2Vec2 vA = data.velocities[m_indexA].v;
138  float wA = data.velocities[m_indexA].w;
139  b2Vec2 vB = data.velocities[m_indexB].v;
140  float wB = data.velocities[m_indexB].w;
141 
142  float mA = m_invMassA, mB = m_invMassB;
143  float iA = m_invIA, iB = m_invIB;
144 
145  float h = data.step.dt;
146 
147  // Solve angular friction
148  {
149  float Cdot = wB - wA;
150  float impulse = -m_angularMass * Cdot;
151 
152  float oldImpulse = m_angularImpulse;
153  float maxImpulse = h * m_maxTorque;
154  m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
155  impulse = m_angularImpulse - oldImpulse;
156 
157  wA -= iA * impulse;
158  wB += iB * impulse;
159  }
160 
161  // Solve linear friction
162  {
163  b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
164 
165  b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
166  b2Vec2 oldImpulse = m_linearImpulse;
167  m_linearImpulse += impulse;
168 
169  float maxImpulse = h * m_maxForce;
170 
171  if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
172  {
174  m_linearImpulse *= maxImpulse;
175  }
176 
177  impulse = m_linearImpulse - oldImpulse;
178 
179  vA -= mA * impulse;
180  wA -= iA * b2Cross(m_rA, impulse);
181 
182  vB += mB * impulse;
183  wB += iB * b2Cross(m_rB, impulse);
184  }
185 
186  data.velocities[m_indexA].v = vA;
187  data.velocities[m_indexA].w = wA;
188  data.velocities[m_indexB].v = vB;
189  data.velocities[m_indexB].w = wB;
190 }
191 
193 {
194  B2_NOT_USED(data);
195 
196  return true;
197 }
198 
200 {
202 }
203 
205 {
207 }
208 
210 {
211  return inv_dt * m_linearImpulse;
212 }
213 
214 float b2FrictionJoint::GetReactionTorque(float inv_dt) const
215 {
216  return inv_dt * m_angularImpulse;
217 }
218 
220 {
221  b2Assert(b2IsValid(force) && force >= 0.0f);
222  m_maxForce = force;
223 }
224 
226 {
227  return m_maxForce;
228 }
229 
231 {
232  b2Assert(b2IsValid(torque) && torque >= 0.0f);
233  m_maxTorque = torque;
234 }
235 
237 {
238  return m_maxTorque;
239 }
240 
242 {
243  int32 indexA = m_bodyA->m_islandIndex;
244  int32 indexB = m_bodyB->m_islandIndex;
245 
246  b2Dump(" b2FrictionJointDef jd;\n");
247  b2Dump(" jd.bodyA = bodies[%d];\n", indexA);
248  b2Dump(" jd.bodyB = bodies[%d];\n", indexB);
249  b2Dump(" jd.collideConnected = bool(%d);\n", m_collideConnected);
250  b2Dump(" jd.localAnchorA.Set(%.9g, %.9g);\n", m_localAnchorA.x, m_localAnchorA.y);
251  b2Dump(" jd.localAnchorB.Set(%.9g, %.9g);\n", m_localAnchorB.x, m_localAnchorB.y);
252  b2Dump(" jd.maxForce = %.9g;\n", m_maxForce);
253  b2Dump(" jd.maxTorque = %.9g;\n", m_maxTorque);
254  b2Dump(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
255 }
float maxForce
The maximum friction force in N.
b2Velocity * velocities
Definition: b2_time_step.h:71
int32 m_islandIndex
Definition: b2_body.h:439
b2Vec2 localAnchorA
The local anchor point relative to bodyA's origin.
b2Vec2 b2Mul(const b2Mat22 &A, const b2Vec2 &v)
Definition: b2_math.h:422
void Dump() override
Dump joint to dmLog.
f
b2TimeStep step
Definition: b2_time_step.h:69
float maxTorque
The maximum friction torque in N-m.
float x
Definition: b2_math.h:128
b2Vec2 localAnchorB
The local anchor point relative to bodyB's origin.
float y
Definition: b2_math.h:128
float GetMaxTorque() const
Get the maximum friction torque in N*m.
bool m_collideConnected
Definition: b2_joint.h:188
bool SolvePositionConstraints(const b2SolverData &data) override
void SolveVelocityConstraints(const b2SolverData &data) override
Solver Data.
Definition: b2_time_step.h:67
void SetZero()
Set this vector to all zeros.
Definition: b2_math.h:50
int32 m_index
Definition: b2_joint.h:185
A 2D column vector.
Definition: b2_math.h:41
b2Vec2 ey
Definition: b2_math.h:241
signed int int32
Definition: b2_types.h:28
Friction joint definition.
float GetReactionTorque(float inv_dt) const override
Get the reaction torque on bodyB in N*m.
float m_invI
Definition: b2_body.h:463
b2Vec2 localCenter
local center of mass position
Definition: b2_math.h:382
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
void SetMaxForce(float force)
Set the maximum friction force in N.
float LengthSquared() const
Definition: b2_math.h:96
#define B2_NOT_USED(x)
Definition: b2_common.h:36
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
void SetMaxTorque(float torque)
Set the maximum friction torque in N*m.
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
b2Body * m_bodyA
Definition: b2_joint.h:182
T b2Clamp(T a, T low, T high)
Definition: b2_math.h:648
b2Vec2 GetReactionForce(float inv_dt) const override
Get the reaction force on bodyB at the joint anchor in Newtons.
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
void b2Dump(const char *string,...)
Definition: b2_settings.cpp:57
void Initialize(b2Body *bodyA, b2Body *bodyB, const b2Vec2 &anchor)
float Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2_math.h:102
b2Vec2 GetWorldPoint(const b2Vec2 &localPoint) const
Definition: b2_body.h:561
b2Vec2 GetAnchorB() const override
Get the anchor point on bodyB in world coordinates.
b2Vec2 ex
Definition: b2_math.h:241
float GetMaxForce() const
Get the maximum friction force in N.
A 2-by-2 matrix. Stored in column-major order.
Definition: b2_math.h:171
bool warmStarting
Definition: b2_time_step.h:49
void InitVelocityConstraints(const b2SolverData &data) override
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
#define b2Assert(A)
Definition: b2_common.h:37
b2Vec2 GetAnchorA() const override
Get the anchor point on bodyA in world coordinates.
b2FrictionJoint(const b2FrictionJointDef *def)
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


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