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

0001 /*
0002 * Copyright (c) 2006-2012 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/b2MotorJoint.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 b2MotorJointDef::Initialize(b2Body* bA, b2Body* bB)
0036 {
0037     bodyA = bA;
0038     bodyB = bB;
0039     b2Vec2 xB = bodyB->GetPosition();
0040     linearOffset = bodyA->GetLocalPoint(xB);
0041 
0042     float32 angleA = bodyA->GetAngle();
0043     float32 angleB = bodyB->GetAngle();
0044     angularOffset = angleB - angleA;
0045 }
0046 
0047 b2MotorJoint::b2MotorJoint(const b2MotorJointDef* def)
0048 : b2Joint(def)
0049 {
0050     m_linearOffset = def->linearOffset;
0051     m_angularOffset = def->angularOffset;
0052 
0053     m_linearImpulse.SetZero();
0054     m_angularImpulse = 0.0f;
0055 
0056     m_maxForce = def->maxForce;
0057     m_maxTorque = def->maxTorque;
0058     m_correctionFactor = def->correctionFactor;
0059 }
0060 
0061 void b2MotorJoint::InitVelocityConstraints(const b2SolverData& data)
0062 {
0063     m_indexA = m_bodyA->m_islandIndex;
0064     m_indexB = m_bodyB->m_islandIndex;
0065     m_localCenterA = m_bodyA->m_sweep.localCenter;
0066     m_localCenterB = m_bodyB->m_sweep.localCenter;
0067     m_invMassA = m_bodyA->m_invMass;
0068     m_invMassB = m_bodyB->m_invMass;
0069     m_invIA = m_bodyA->m_invI;
0070     m_invIB = m_bodyB->m_invI;
0071 
0072     b2Vec2 cA = data.positions[m_indexA].c;
0073     float32 aA = data.positions[m_indexA].a;
0074     b2Vec2 vA = data.velocities[m_indexA].v;
0075     float32 wA = data.velocities[m_indexA].w;
0076 
0077     b2Vec2 cB = data.positions[m_indexB].c;
0078     float32 aB = data.positions[m_indexB].a;
0079     b2Vec2 vB = data.velocities[m_indexB].v;
0080     float32 wB = data.velocities[m_indexB].w;
0081 
0082     b2Rot qA(aA), qB(aB);
0083 
0084     // Compute the effective mass matrix.
0085     m_rA = b2Mul(qA, -m_localCenterA);
0086     m_rB = b2Mul(qB, -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     b2Mat22 K;
0101     K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
0102     K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
0103     K.ey.x = K.ex.y;
0104     K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
0105 
0106     m_linearMass = K.GetInverse();
0107 
0108     m_angularMass = iA + iB;
0109     if (m_angularMass > 0.0f)
0110     {
0111         m_angularMass = 1.0f / m_angularMass;
0112     }
0113 
0114     m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset);
0115     m_angularError = aB - aA - m_angularOffset;
0116 
0117     if (data.step.warmStarting)
0118     {
0119         // Scale impulses to support a variable time step.
0120         m_linearImpulse *= data.step.dtRatio;
0121         m_angularImpulse *= data.step.dtRatio;
0122 
0123         b2Vec2 P(m_linearImpulse.x, m_linearImpulse.y);
0124         vA -= mA * P;
0125         wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
0126         vB += mB * P;
0127         wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
0128     }
0129     else
0130     {
0131         m_linearImpulse.SetZero();
0132         m_angularImpulse = 0.0f;
0133     }
0134 
0135     data.velocities[m_indexA].v = vA;
0136     data.velocities[m_indexA].w = wA;
0137     data.velocities[m_indexB].v = vB;
0138     data.velocities[m_indexB].w = wB;
0139 }
0140 
0141 void b2MotorJoint::SolveVelocityConstraints(const b2SolverData& data)
0142 {
0143     b2Vec2 vA = data.velocities[m_indexA].v;
0144     float32 wA = data.velocities[m_indexA].w;
0145     b2Vec2 vB = data.velocities[m_indexB].v;
0146     float32 wB = data.velocities[m_indexB].w;
0147 
0148     float32 mA = m_invMassA, mB = m_invMassB;
0149     float32 iA = m_invIA, iB = m_invIB;
0150 
0151     float32 h = data.step.dt;
0152     float32 inv_h = data.step.inv_dt;
0153 
0154     // Solve angular friction
0155     {
0156         float32 Cdot = wB - wA + inv_h * m_correctionFactor * m_angularError;
0157         float32 impulse = -m_angularMass * Cdot;
0158 
0159         float32 oldImpulse = m_angularImpulse;
0160         float32 maxImpulse = h * m_maxTorque;
0161         m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
0162         impulse = m_angularImpulse - oldImpulse;
0163 
0164         wA -= iA * impulse;
0165         wB += iB * impulse;
0166     }
0167 
0168     // Solve linear friction
0169     {
0170         b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor * m_linearError;
0171 
0172         b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
0173         b2Vec2 oldImpulse = m_linearImpulse;
0174         m_linearImpulse += impulse;
0175 
0176         float32 maxImpulse = h * m_maxForce;
0177 
0178         if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
0179         {
0180             m_linearImpulse.Normalize();
0181             m_linearImpulse *= maxImpulse;
0182         }
0183 
0184         impulse = m_linearImpulse - oldImpulse;
0185 
0186         vA -= mA * impulse;
0187         wA -= iA * b2Cross(m_rA, impulse);
0188 
0189         vB += mB * impulse;
0190         wB += iB * b2Cross(m_rB, impulse);
0191     }
0192 
0193     data.velocities[m_indexA].v = vA;
0194     data.velocities[m_indexA].w = wA;
0195     data.velocities[m_indexB].v = vB;
0196     data.velocities[m_indexB].w = wB;
0197 }
0198 
0199 bool b2MotorJoint::SolvePositionConstraints(const b2SolverData& data)
0200 {
0201     B2_NOT_USED(data);
0202 
0203     return true;
0204 }
0205 
0206 b2Vec2 b2MotorJoint::GetAnchorA() const
0207 {
0208     return m_bodyA->GetPosition();
0209 }
0210 
0211 b2Vec2 b2MotorJoint::GetAnchorB() const
0212 {
0213     return m_bodyB->GetPosition();
0214 }
0215 
0216 b2Vec2 b2MotorJoint::GetReactionForce(float32 inv_dt) const
0217 {
0218     return inv_dt * m_linearImpulse;
0219 }
0220 
0221 float32 b2MotorJoint::GetReactionTorque(float32 inv_dt) const
0222 {
0223     return inv_dt * m_angularImpulse;
0224 }
0225 
0226 void b2MotorJoint::SetMaxForce(float32 force)
0227 {
0228     b2Assert(b2IsValid(force) && force >= 0.0f);
0229     m_maxForce = force;
0230 }
0231 
0232 float32 b2MotorJoint::GetMaxForce() const
0233 {
0234     return m_maxForce;
0235 }
0236 
0237 void b2MotorJoint::SetMaxTorque(float32 torque)
0238 {
0239     b2Assert(b2IsValid(torque) && torque >= 0.0f);
0240     m_maxTorque = torque;
0241 }
0242 
0243 float32 b2MotorJoint::GetMaxTorque() const
0244 {
0245     return m_maxTorque;
0246 }
0247 
0248 void b2MotorJoint::SetCorrectionFactor(float32 factor)
0249 {
0250     b2Assert(b2IsValid(factor) && 0.0f <= factor && factor <= 1.0f);
0251     m_correctionFactor = factor;
0252 }
0253 
0254 float32 b2MotorJoint::GetCorrectionFactor() const
0255 {
0256     return m_correctionFactor;
0257 }
0258 
0259 void b2MotorJoint::SetLinearOffset(const b2Vec2& linearOffset)
0260 {
0261     if (linearOffset.x != m_linearOffset.x || linearOffset.y != m_linearOffset.y)
0262     {
0263         m_bodyA->SetAwake(true);
0264         m_bodyB->SetAwake(true);
0265         m_linearOffset = linearOffset;
0266     }
0267 }
0268 
0269 const b2Vec2& b2MotorJoint::GetLinearOffset() const
0270 {
0271     return m_linearOffset;
0272 }
0273 
0274 void b2MotorJoint::SetAngularOffset(float32 angularOffset)
0275 {
0276     if (angularOffset != m_angularOffset)
0277     {
0278         m_bodyA->SetAwake(true);
0279         m_bodyB->SetAwake(true);
0280         m_angularOffset = angularOffset;
0281     }
0282 }
0283 
0284 float32 b2MotorJoint::GetAngularOffset() const
0285 {
0286     return m_angularOffset;
0287 }
0288 
0289 void b2MotorJoint::Dump()
0290 {
0291     int32 indexA = m_bodyA->m_islandIndex;
0292     int32 indexB = m_bodyB->m_islandIndex;
0293 
0294     b2Log("  b2MotorJointDef jd;\n");
0295     b2Log("  jd.bodyA = bodies[%d];\n", indexA);
0296     b2Log("  jd.bodyB = bodies[%d];\n", indexB);
0297     b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
0298     b2Log("  jd.linearOffset.Set(%.15lef, %.15lef);\n", m_linearOffset.x, m_linearOffset.y);
0299     b2Log("  jd.angularOffset = %.15lef;\n", m_angularOffset);
0300     b2Log("  jd.maxForce = %.15lef;\n", m_maxForce);
0301     b2Log("  jd.maxTorque = %.15lef;\n", m_maxTorque);
0302     b2Log("  jd.correctionFactor = %.15lef;\n", m_correctionFactor);
0303     b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
0304 }