File indexing completed on 2024-12-29 03:29:26
0001 /* 0002 * Copyright (c) 2006-2011 Erin Catto http://www.box2d.org 0003 * 0004 * This software is provided 'as-is', without any express or implied 0005 * warranty. In no event will the authors be held liable for any damages 0006 * arising from the use of this software. 0007 * Permission is granted to anyone to use this software for any purpose, 0008 * including commercial applications, and to alter it and redistribute it 0009 * freely, subject to the following restrictions: 0010 * 1. The origin of this software must not be misrepresented; you must not 0011 * claim that you wrote the original software. If you use this software 0012 * in a product, an acknowledgment in the product documentation would be 0013 * appreciated but is not required. 0014 * 2. Altered source versions must be plainly marked as such, and must not be 0015 * misrepresented as being the original software. 0016 * 3. This notice may not be removed or altered from any source distribution. 0017 */ 0018 0019 #include <Box2D/Dynamics/Joints/b2FrictionJoint.h> 0020 #include <Box2D/Dynamics/b2Body.h> 0021 #include <Box2D/Dynamics/b2TimeStep.h> 0022 0023 // Point-to-point constraint 0024 // Cdot = v2 - v1 0025 // = v2 + cross(w2, r2) - v1 - cross(w1, r1) 0026 // J = [-I -r1_skew I r2_skew ] 0027 // Identity used: 0028 // w k % (rx i + ry j) = w * (-ry i + rx j) 0029 0030 // Angle constraint 0031 // Cdot = w2 - w1 0032 // J = [0 0 -1 0 0 1] 0033 // K = invI1 + invI2 0034 0035 void b2FrictionJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor) 0036 { 0037 bodyA = bA; 0038 bodyB = bB; 0039 localAnchorA = bodyA->GetLocalPoint(anchor); 0040 localAnchorB = bodyB->GetLocalPoint(anchor); 0041 } 0042 0043 b2FrictionJoint::b2FrictionJoint(const b2FrictionJointDef* def) 0044 : b2Joint(def) 0045 { 0046 m_localAnchorA = def->localAnchorA; 0047 m_localAnchorB = def->localAnchorB; 0048 0049 m_linearImpulse.SetZero(); 0050 m_angularImpulse = 0.0f; 0051 0052 m_maxForce = def->maxForce; 0053 m_maxTorque = def->maxTorque; 0054 } 0055 0056 void b2FrictionJoint::InitVelocityConstraints(const b2SolverData& data) 0057 { 0058 m_indexA = m_bodyA->m_islandIndex; 0059 m_indexB = m_bodyB->m_islandIndex; 0060 m_localCenterA = m_bodyA->m_sweep.localCenter; 0061 m_localCenterB = m_bodyB->m_sweep.localCenter; 0062 m_invMassA = m_bodyA->m_invMass; 0063 m_invMassB = m_bodyB->m_invMass; 0064 m_invIA = m_bodyA->m_invI; 0065 m_invIB = m_bodyB->m_invI; 0066 0067 float32 aA = data.positions[m_indexA].a; 0068 b2Vec2 vA = data.velocities[m_indexA].v; 0069 float32 wA = data.velocities[m_indexA].w; 0070 0071 float32 aB = data.positions[m_indexB].a; 0072 b2Vec2 vB = data.velocities[m_indexB].v; 0073 float32 wB = data.velocities[m_indexB].w; 0074 0075 b2Rot qA(aA), qB(aB); 0076 0077 // Compute the effective mass matrix. 0078 m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA); 0079 m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB); 0080 0081 // J = [-I -r1_skew I r2_skew] 0082 // [ 0 -1 0 1] 0083 // r_skew = [-ry; rx] 0084 0085 // Matlab 0086 // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB] 0087 // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB] 0088 // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB] 0089 0090 float32 mA = m_invMassA, mB = m_invMassB; 0091 float32 iA = m_invIA, iB = m_invIB; 0092 0093 b2Mat22 K; 0094 K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y; 0095 K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y; 0096 K.ey.x = K.ex.y; 0097 K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x; 0098 0099 m_linearMass = K.GetInverse(); 0100 0101 m_angularMass = iA + iB; 0102 if (m_angularMass > 0.0f) 0103 { 0104 m_angularMass = 1.0f / m_angularMass; 0105 } 0106 0107 if (data.step.warmStarting) 0108 { 0109 // Scale impulses to support a variable time step. 0110 m_linearImpulse *= data.step.dtRatio; 0111 m_angularImpulse *= data.step.dtRatio; 0112 0113 b2Vec2 P(m_linearImpulse.x, m_linearImpulse.y); 0114 vA -= mA * P; 0115 wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse); 0116 vB += mB * P; 0117 wB += iB * (b2Cross(m_rB, P) + m_angularImpulse); 0118 } 0119 else 0120 { 0121 m_linearImpulse.SetZero(); 0122 m_angularImpulse = 0.0f; 0123 } 0124 0125 data.velocities[m_indexA].v = vA; 0126 data.velocities[m_indexA].w = wA; 0127 data.velocities[m_indexB].v = vB; 0128 data.velocities[m_indexB].w = wB; 0129 } 0130 0131 void b2FrictionJoint::SolveVelocityConstraints(const b2SolverData& data) 0132 { 0133 b2Vec2 vA = data.velocities[m_indexA].v; 0134 float32 wA = data.velocities[m_indexA].w; 0135 b2Vec2 vB = data.velocities[m_indexB].v; 0136 float32 wB = data.velocities[m_indexB].w; 0137 0138 float32 mA = m_invMassA, mB = m_invMassB; 0139 float32 iA = m_invIA, iB = m_invIB; 0140 0141 float32 h = data.step.dt; 0142 0143 // Solve angular friction 0144 { 0145 float32 Cdot = wB - wA; 0146 float32 impulse = -m_angularMass * Cdot; 0147 0148 float32 oldImpulse = m_angularImpulse; 0149 float32 maxImpulse = h * m_maxTorque; 0150 m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse); 0151 impulse = m_angularImpulse - oldImpulse; 0152 0153 wA -= iA * impulse; 0154 wB += iB * impulse; 0155 } 0156 0157 // Solve linear friction 0158 { 0159 b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA); 0160 0161 b2Vec2 impulse = -b2Mul(m_linearMass, Cdot); 0162 b2Vec2 oldImpulse = m_linearImpulse; 0163 m_linearImpulse += impulse; 0164 0165 float32 maxImpulse = h * m_maxForce; 0166 0167 if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse) 0168 { 0169 m_linearImpulse.Normalize(); 0170 m_linearImpulse *= maxImpulse; 0171 } 0172 0173 impulse = m_linearImpulse - oldImpulse; 0174 0175 vA -= mA * impulse; 0176 wA -= iA * b2Cross(m_rA, impulse); 0177 0178 vB += mB * impulse; 0179 wB += iB * b2Cross(m_rB, impulse); 0180 } 0181 0182 data.velocities[m_indexA].v = vA; 0183 data.velocities[m_indexA].w = wA; 0184 data.velocities[m_indexB].v = vB; 0185 data.velocities[m_indexB].w = wB; 0186 } 0187 0188 bool b2FrictionJoint::SolvePositionConstraints(const b2SolverData& data) 0189 { 0190 B2_NOT_USED(data); 0191 0192 return true; 0193 } 0194 0195 b2Vec2 b2FrictionJoint::GetAnchorA() const 0196 { 0197 return m_bodyA->GetWorldPoint(m_localAnchorA); 0198 } 0199 0200 b2Vec2 b2FrictionJoint::GetAnchorB() const 0201 { 0202 return m_bodyB->GetWorldPoint(m_localAnchorB); 0203 } 0204 0205 b2Vec2 b2FrictionJoint::GetReactionForce(float32 inv_dt) const 0206 { 0207 return inv_dt * m_linearImpulse; 0208 } 0209 0210 float32 b2FrictionJoint::GetReactionTorque(float32 inv_dt) const 0211 { 0212 return inv_dt * m_angularImpulse; 0213 } 0214 0215 void b2FrictionJoint::SetMaxForce(float32 force) 0216 { 0217 b2Assert(b2IsValid(force) && force >= 0.0f); 0218 m_maxForce = force; 0219 } 0220 0221 float32 b2FrictionJoint::GetMaxForce() const 0222 { 0223 return m_maxForce; 0224 } 0225 0226 void b2FrictionJoint::SetMaxTorque(float32 torque) 0227 { 0228 b2Assert(b2IsValid(torque) && torque >= 0.0f); 0229 m_maxTorque = torque; 0230 } 0231 0232 float32 b2FrictionJoint::GetMaxTorque() const 0233 { 0234 return m_maxTorque; 0235 } 0236 0237 void b2FrictionJoint::Dump() 0238 { 0239 int32 indexA = m_bodyA->m_islandIndex; 0240 int32 indexB = m_bodyB->m_islandIndex; 0241 0242 b2Log(" b2FrictionJointDef jd;\n"); 0243 b2Log(" jd.bodyA = bodies[%d];\n", indexA); 0244 b2Log(" jd.bodyB = bodies[%d];\n", indexB); 0245 b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected); 0246 b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y); 0247 b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y); 0248 b2Log(" jd.maxForce = %.15lef;\n", m_maxForce); 0249 b2Log(" jd.maxTorque = %.15lef;\n", m_maxTorque); 0250 b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index); 0251 }