File indexing completed on 2024-12-29 03:29:28

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/b2RevoluteJoint.h>
0020 #include <Box2D/Dynamics/b2Body.h>
0021 #include <Box2D/Dynamics/b2TimeStep.h>
0022 
0023 // Point-to-point constraint
0024 // C = p2 - p1
0025 // Cdot = v2 - v1
0026 //      = v2 + cross(w2, r2) - v1 - cross(w1, r1)
0027 // J = [-I -r1_skew I r2_skew ]
0028 // Identity used:
0029 // w k % (rx i + ry j) = w * (-ry i + rx j)
0030 
0031 // Motor constraint
0032 // Cdot = w2 - w1
0033 // J = [0 0 -1 0 0 1]
0034 // K = invI1 + invI2
0035 
0036 void b2RevoluteJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
0037 {
0038     bodyA = bA;
0039     bodyB = bB;
0040     localAnchorA = bodyA->GetLocalPoint(anchor);
0041     localAnchorB = bodyB->GetLocalPoint(anchor);
0042     referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
0043 }
0044 
0045 b2RevoluteJoint::b2RevoluteJoint(const b2RevoluteJointDef* def)
0046 : b2Joint(def)
0047 {
0048     m_localAnchorA = def->localAnchorA;
0049     m_localAnchorB = def->localAnchorB;
0050     m_referenceAngle = def->referenceAngle;
0051 
0052     m_impulse.SetZero();
0053     m_motorImpulse = 0.0f;
0054 
0055     m_lowerAngle = def->lowerAngle;
0056     m_upperAngle = def->upperAngle;
0057     m_maxMotorTorque = def->maxMotorTorque;
0058     m_motorSpeed = def->motorSpeed;
0059     m_enableLimit = def->enableLimit;
0060     m_enableMotor = def->enableMotor;
0061     m_limitState = e_inactiveLimit;
0062 }
0063 
0064 void b2RevoluteJoint::InitVelocityConstraints(const b2SolverData& data)
0065 {
0066     m_indexA = m_bodyA->m_islandIndex;
0067     m_indexB = m_bodyB->m_islandIndex;
0068     m_localCenterA = m_bodyA->m_sweep.localCenter;
0069     m_localCenterB = m_bodyB->m_sweep.localCenter;
0070     m_invMassA = m_bodyA->m_invMass;
0071     m_invMassB = m_bodyB->m_invMass;
0072     m_invIA = m_bodyA->m_invI;
0073     m_invIB = m_bodyB->m_invI;
0074 
0075     float32 aA = data.positions[m_indexA].a;
0076     b2Vec2 vA = data.velocities[m_indexA].v;
0077     float32 wA = data.velocities[m_indexA].w;
0078 
0079     float32 aB = data.positions[m_indexB].a;
0080     b2Vec2 vB = data.velocities[m_indexB].v;
0081     float32 wB = data.velocities[m_indexB].w;
0082 
0083     b2Rot qA(aA), qB(aB);
0084 
0085     m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
0086     m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
0087 
0088     // J = [-I -r1_skew I r2_skew]
0089     //     [ 0       -1 0       1]
0090     // r_skew = [-ry; rx]
0091 
0092     // Matlab
0093     // K = [ mA+r1y^2*iA+mB+r2y^2*iB,  -r1y*iA*r1x-r2y*iB*r2x,          -r1y*iA-r2y*iB]
0094     //     [  -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB,           r1x*iA+r2x*iB]
0095     //     [          -r1y*iA-r2y*iB,           r1x*iA+r2x*iB,                   iA+iB]
0096 
0097     float32 mA = m_invMassA, mB = m_invMassB;
0098     float32 iA = m_invIA, iB = m_invIB;
0099 
0100     bool fixedRotation = (iA + iB == 0.0f);
0101 
0102     m_mass.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
0103     m_mass.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
0104     m_mass.ez.x = -m_rA.y * iA - m_rB.y * iB;
0105     m_mass.ex.y = m_mass.ey.x;
0106     m_mass.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
0107     m_mass.ez.y = m_rA.x * iA + m_rB.x * iB;
0108     m_mass.ex.z = m_mass.ez.x;
0109     m_mass.ey.z = m_mass.ez.y;
0110     m_mass.ez.z = iA + iB;
0111 
0112     m_motorMass = iA + iB;
0113     if (m_motorMass > 0.0f)
0114     {
0115         m_motorMass = 1.0f / m_motorMass;
0116     }
0117 
0118     if (m_enableMotor == false || fixedRotation)
0119     {
0120         m_motorImpulse = 0.0f;
0121     }
0122 
0123     if (m_enableLimit && fixedRotation == false)
0124     {
0125         float32 jointAngle = aB - aA - m_referenceAngle;
0126         if (b2Abs(m_upperAngle - m_lowerAngle) < 2.0f * b2_angularSlop)
0127         {
0128             m_limitState = e_equalLimits;
0129         }
0130         else if (jointAngle <= m_lowerAngle)
0131         {
0132             if (m_limitState != e_atLowerLimit)
0133             {
0134                 m_impulse.z = 0.0f;
0135             }
0136             m_limitState = e_atLowerLimit;
0137         }
0138         else if (jointAngle >= m_upperAngle)
0139         {
0140             if (m_limitState != e_atUpperLimit)
0141             {
0142                 m_impulse.z = 0.0f;
0143             }
0144             m_limitState = e_atUpperLimit;
0145         }
0146         else
0147         {
0148             m_limitState = e_inactiveLimit;
0149             m_impulse.z = 0.0f;
0150         }
0151     }
0152     else
0153     {
0154         m_limitState = e_inactiveLimit;
0155     }
0156 
0157     if (data.step.warmStarting)
0158     {
0159         // Scale impulses to support a variable time step.
0160         m_impulse *= data.step.dtRatio;
0161         m_motorImpulse *= data.step.dtRatio;
0162 
0163         b2Vec2 P(m_impulse.x, m_impulse.y);
0164 
0165         vA -= mA * P;
0166         wA -= iA * (b2Cross(m_rA, P) + m_motorImpulse + m_impulse.z);
0167 
0168         vB += mB * P;
0169         wB += iB * (b2Cross(m_rB, P) + m_motorImpulse + m_impulse.z);
0170     }
0171     else
0172     {
0173         m_impulse.SetZero();
0174         m_motorImpulse = 0.0f;
0175     }
0176 
0177     data.velocities[m_indexA].v = vA;
0178     data.velocities[m_indexA].w = wA;
0179     data.velocities[m_indexB].v = vB;
0180     data.velocities[m_indexB].w = wB;
0181 }
0182 
0183 void b2RevoluteJoint::SolveVelocityConstraints(const b2SolverData& data)
0184 {
0185     b2Vec2 vA = data.velocities[m_indexA].v;
0186     float32 wA = data.velocities[m_indexA].w;
0187     b2Vec2 vB = data.velocities[m_indexB].v;
0188     float32 wB = data.velocities[m_indexB].w;
0189 
0190     float32 mA = m_invMassA, mB = m_invMassB;
0191     float32 iA = m_invIA, iB = m_invIB;
0192 
0193     bool fixedRotation = (iA + iB == 0.0f);
0194 
0195     // Solve motor constraint.
0196     if (m_enableMotor && m_limitState != e_equalLimits && fixedRotation == false)
0197     {
0198         float32 Cdot = wB - wA - m_motorSpeed;
0199         float32 impulse = -m_motorMass * Cdot;
0200         float32 oldImpulse = m_motorImpulse;
0201         float32 maxImpulse = data.step.dt * m_maxMotorTorque;
0202         m_motorImpulse = b2Clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
0203         impulse = m_motorImpulse - oldImpulse;
0204 
0205         wA -= iA * impulse;
0206         wB += iB * impulse;
0207     }
0208 
0209     // Solve limit constraint.
0210     if (m_enableLimit && m_limitState != e_inactiveLimit && fixedRotation == false)
0211     {
0212         b2Vec2 Cdot1 = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
0213         float32 Cdot2 = wB - wA;
0214         b2Vec3 Cdot(Cdot1.x, Cdot1.y, Cdot2);
0215 
0216         b2Vec3 impulse = -m_mass.Solve33(Cdot);
0217 
0218         if (m_limitState == e_equalLimits)
0219         {
0220             m_impulse += impulse;
0221         }
0222         else if (m_limitState == e_atLowerLimit)
0223         {
0224             float32 newImpulse = m_impulse.z + impulse.z;
0225             if (newImpulse < 0.0f)
0226             {
0227                 b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y);
0228                 b2Vec2 reduced = m_mass.Solve22(rhs);
0229                 impulse.x = reduced.x;
0230                 impulse.y = reduced.y;
0231                 impulse.z = -m_impulse.z;
0232                 m_impulse.x += reduced.x;
0233                 m_impulse.y += reduced.y;
0234                 m_impulse.z = 0.0f;
0235             }
0236             else
0237             {
0238                 m_impulse += impulse;
0239             }
0240         }
0241         else if (m_limitState == e_atUpperLimit)
0242         {
0243             float32 newImpulse = m_impulse.z + impulse.z;
0244             if (newImpulse > 0.0f)
0245             {
0246                 b2Vec2 rhs = -Cdot1 + m_impulse.z * b2Vec2(m_mass.ez.x, m_mass.ez.y);
0247                 b2Vec2 reduced = m_mass.Solve22(rhs);
0248                 impulse.x = reduced.x;
0249                 impulse.y = reduced.y;
0250                 impulse.z = -m_impulse.z;
0251                 m_impulse.x += reduced.x;
0252                 m_impulse.y += reduced.y;
0253                 m_impulse.z = 0.0f;
0254             }
0255             else
0256             {
0257                 m_impulse += impulse;
0258             }
0259         }
0260 
0261         b2Vec2 P(impulse.x, impulse.y);
0262 
0263         vA -= mA * P;
0264         wA -= iA * (b2Cross(m_rA, P) + impulse.z);
0265 
0266         vB += mB * P;
0267         wB += iB * (b2Cross(m_rB, P) + impulse.z);
0268     }
0269     else
0270     {
0271         // Solve point-to-point constraint
0272         b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
0273         b2Vec2 impulse = m_mass.Solve22(-Cdot);
0274 
0275         m_impulse.x += impulse.x;
0276         m_impulse.y += impulse.y;
0277 
0278         vA -= mA * impulse;
0279         wA -= iA * b2Cross(m_rA, impulse);
0280 
0281         vB += mB * impulse;
0282         wB += iB * b2Cross(m_rB, impulse);
0283     }
0284 
0285     data.velocities[m_indexA].v = vA;
0286     data.velocities[m_indexA].w = wA;
0287     data.velocities[m_indexB].v = vB;
0288     data.velocities[m_indexB].w = wB;
0289 }
0290 
0291 bool b2RevoluteJoint::SolvePositionConstraints(const b2SolverData& data)
0292 {
0293     b2Vec2 cA = data.positions[m_indexA].c;
0294     float32 aA = data.positions[m_indexA].a;
0295     b2Vec2 cB = data.positions[m_indexB].c;
0296     float32 aB = data.positions[m_indexB].a;
0297 
0298     b2Rot qA(aA), qB(aB);
0299 
0300     float32 angularError = 0.0f;
0301     float32 positionError = 0.0f;
0302 
0303     bool fixedRotation = (m_invIA + m_invIB == 0.0f);
0304 
0305     // Solve angular limit constraint.
0306     if (m_enableLimit && m_limitState != e_inactiveLimit && fixedRotation == false)
0307     {
0308         float32 angle = aB - aA - m_referenceAngle;
0309         float32 limitImpulse = 0.0f;
0310 
0311         if (m_limitState == e_equalLimits)
0312         {
0313             // Prevent large angular corrections
0314             float32 C = b2Clamp(angle - m_lowerAngle, -b2_maxAngularCorrection, b2_maxAngularCorrection);
0315             limitImpulse = -m_motorMass * C;
0316             angularError = b2Abs(C);
0317         }
0318         else if (m_limitState == e_atLowerLimit)
0319         {
0320             float32 C = angle - m_lowerAngle;
0321             angularError = -C;
0322 
0323             // Prevent large angular corrections and allow some slop.
0324             C = b2Clamp(C + b2_angularSlop, -b2_maxAngularCorrection, 0.0f);
0325             limitImpulse = -m_motorMass * C;
0326         }
0327         else if (m_limitState == e_atUpperLimit)
0328         {
0329             float32 C = angle - m_upperAngle;
0330             angularError = C;
0331 
0332             // Prevent large angular corrections and allow some slop.
0333             C = b2Clamp(C - b2_angularSlop, 0.0f, b2_maxAngularCorrection);
0334             limitImpulse = -m_motorMass * C;
0335         }
0336 
0337         aA -= m_invIA * limitImpulse;
0338         aB += m_invIB * limitImpulse;
0339     }
0340 
0341     // Solve point-to-point constraint.
0342     {
0343         qA.Set(aA);
0344         qB.Set(aB);
0345         b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
0346         b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
0347 
0348         b2Vec2 C = cB + rB - cA - rA;
0349         positionError = C.Length();
0350 
0351         float32 mA = m_invMassA, mB = m_invMassB;
0352         float32 iA = m_invIA, iB = m_invIB;
0353 
0354         b2Mat22 K;
0355         K.ex.x = mA + mB + iA * rA.y * rA.y + iB * rB.y * rB.y;
0356         K.ex.y = -iA * rA.x * rA.y - iB * rB.x * rB.y;
0357         K.ey.x = K.ex.y;
0358         K.ey.y = mA + mB + iA * rA.x * rA.x + iB * rB.x * rB.x;
0359 
0360         b2Vec2 impulse = -K.Solve(C);
0361 
0362         cA -= mA * impulse;
0363         aA -= iA * b2Cross(rA, impulse);
0364 
0365         cB += mB * impulse;
0366         aB += iB * b2Cross(rB, impulse);
0367     }
0368 
0369     data.positions[m_indexA].c = cA;
0370     data.positions[m_indexA].a = aA;
0371     data.positions[m_indexB].c = cB;
0372     data.positions[m_indexB].a = aB;
0373     
0374     return positionError <= b2_linearSlop && angularError <= b2_angularSlop;
0375 }
0376 
0377 b2Vec2 b2RevoluteJoint::GetAnchorA() const
0378 {
0379     return m_bodyA->GetWorldPoint(m_localAnchorA);
0380 }
0381 
0382 b2Vec2 b2RevoluteJoint::GetAnchorB() const
0383 {
0384     return m_bodyB->GetWorldPoint(m_localAnchorB);
0385 }
0386 
0387 b2Vec2 b2RevoluteJoint::GetReactionForce(float32 inv_dt) const
0388 {
0389     b2Vec2 P(m_impulse.x, m_impulse.y);
0390     return inv_dt * P;
0391 }
0392 
0393 float32 b2RevoluteJoint::GetReactionTorque(float32 inv_dt) const
0394 {
0395     return inv_dt * m_impulse.z;
0396 }
0397 
0398 float32 b2RevoluteJoint::GetJointAngle() const
0399 {
0400     b2Body* bA = m_bodyA;
0401     b2Body* bB = m_bodyB;
0402     return bB->m_sweep.a - bA->m_sweep.a - m_referenceAngle;
0403 }
0404 
0405 float32 b2RevoluteJoint::GetJointSpeed() const
0406 {
0407     b2Body* bA = m_bodyA;
0408     b2Body* bB = m_bodyB;
0409     return bB->m_angularVelocity - bA->m_angularVelocity;
0410 }
0411 
0412 bool b2RevoluteJoint::IsMotorEnabled() const
0413 {
0414     return m_enableMotor;
0415 }
0416 
0417 void b2RevoluteJoint::EnableMotor(bool flag)
0418 {
0419     m_bodyA->SetAwake(true);
0420     m_bodyB->SetAwake(true);
0421     m_enableMotor = flag;
0422 }
0423 
0424 float32 b2RevoluteJoint::GetMotorTorque(float32 inv_dt) const
0425 {
0426     return inv_dt * m_motorImpulse;
0427 }
0428 
0429 void b2RevoluteJoint::SetMotorSpeed(float32 speed)
0430 {
0431     m_bodyA->SetAwake(true);
0432     m_bodyB->SetAwake(true);
0433     m_motorSpeed = speed;
0434 }
0435 
0436 void b2RevoluteJoint::SetMaxMotorTorque(float32 torque)
0437 {
0438     m_bodyA->SetAwake(true);
0439     m_bodyB->SetAwake(true);
0440     m_maxMotorTorque = torque;
0441 }
0442 
0443 bool b2RevoluteJoint::IsLimitEnabled() const
0444 {
0445     return m_enableLimit;
0446 }
0447 
0448 void b2RevoluteJoint::EnableLimit(bool flag)
0449 {
0450     if (flag != m_enableLimit)
0451     {
0452         m_bodyA->SetAwake(true);
0453         m_bodyB->SetAwake(true);
0454         m_enableLimit = flag;
0455         m_impulse.z = 0.0f;
0456     }
0457 }
0458 
0459 float32 b2RevoluteJoint::GetLowerLimit() const
0460 {
0461     return m_lowerAngle;
0462 }
0463 
0464 float32 b2RevoluteJoint::GetUpperLimit() const
0465 {
0466     return m_upperAngle;
0467 }
0468 
0469 void b2RevoluteJoint::SetLimits(float32 lower, float32 upper)
0470 {
0471     b2Assert(lower <= upper);
0472     
0473     if (lower != m_lowerAngle || upper != m_upperAngle)
0474     {
0475         m_bodyA->SetAwake(true);
0476         m_bodyB->SetAwake(true);
0477         m_impulse.z = 0.0f;
0478         m_lowerAngle = lower;
0479         m_upperAngle = upper;
0480     }
0481 }
0482 
0483 void b2RevoluteJoint::Dump()
0484 {
0485     int32 indexA = m_bodyA->m_islandIndex;
0486     int32 indexB = m_bodyB->m_islandIndex;
0487 
0488     b2Log("  b2RevoluteJointDef jd;\n");
0489     b2Log("  jd.bodyA = bodies[%d];\n", indexA);
0490     b2Log("  jd.bodyB = bodies[%d];\n", indexB);
0491     b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
0492     b2Log("  jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
0493     b2Log("  jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
0494     b2Log("  jd.referenceAngle = %.15lef;\n", m_referenceAngle);
0495     b2Log("  jd.enableLimit = bool(%d);\n", m_enableLimit);
0496     b2Log("  jd.lowerAngle = %.15lef;\n", m_lowerAngle);
0497     b2Log("  jd.upperAngle = %.15lef;\n", m_upperAngle);
0498     b2Log("  jd.enableMotor = bool(%d);\n", m_enableMotor);
0499     b2Log("  jd.motorSpeed = %.15lef;\n", m_motorSpeed);
0500     b2Log("  jd.maxMotorTorque = %.15lef;\n", m_maxMotorTorque);
0501     b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
0502 }