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

0001 /*
0002 * Copyright (c) 2007-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/b2RopeJoint.h>
0020 #include <Box2D/Dynamics/b2Body.h>
0021 #include <Box2D/Dynamics/b2TimeStep.h>
0022 
0023 
0024 // Limit:
0025 // C = norm(pB - pA) - L
0026 // u = (pB - pA) / norm(pB - pA)
0027 // Cdot = dot(u, vB + cross(wB, rB) - vA - cross(wA, rA))
0028 // J = [-u -cross(rA, u) u cross(rB, u)]
0029 // K = J * invM * JT
0030 //   = invMassA + invIA * cross(rA, u)^2 + invMassB + invIB * cross(rB, u)^2
0031 
0032 b2RopeJoint::b2RopeJoint(const b2RopeJointDef* def)
0033 : b2Joint(def)
0034 {
0035     m_localAnchorA = def->localAnchorA;
0036     m_localAnchorB = def->localAnchorB;
0037 
0038     m_maxLength = def->maxLength;
0039 
0040     m_mass = 0.0f;
0041     m_impulse = 0.0f;
0042     m_state = e_inactiveLimit;
0043     m_length = 0.0f;
0044 }
0045 
0046 void b2RopeJoint::InitVelocityConstraints(const b2SolverData& data)
0047 {
0048     m_indexA = m_bodyA->m_islandIndex;
0049     m_indexB = m_bodyB->m_islandIndex;
0050     m_localCenterA = m_bodyA->m_sweep.localCenter;
0051     m_localCenterB = m_bodyB->m_sweep.localCenter;
0052     m_invMassA = m_bodyA->m_invMass;
0053     m_invMassB = m_bodyB->m_invMass;
0054     m_invIA = m_bodyA->m_invI;
0055     m_invIB = m_bodyB->m_invI;
0056 
0057     b2Vec2 cA = data.positions[m_indexA].c;
0058     float32 aA = data.positions[m_indexA].a;
0059     b2Vec2 vA = data.velocities[m_indexA].v;
0060     float32 wA = data.velocities[m_indexA].w;
0061 
0062     b2Vec2 cB = data.positions[m_indexB].c;
0063     float32 aB = data.positions[m_indexB].a;
0064     b2Vec2 vB = data.velocities[m_indexB].v;
0065     float32 wB = data.velocities[m_indexB].w;
0066 
0067     b2Rot qA(aA), qB(aB);
0068 
0069     m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
0070     m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
0071     m_u = cB + m_rB - cA - m_rA;
0072 
0073     m_length = m_u.Length();
0074 
0075     float32 C = m_length - m_maxLength;
0076     if (C > 0.0f)
0077     {
0078         m_state = e_atUpperLimit;
0079     }
0080     else
0081     {
0082         m_state = e_inactiveLimit;
0083     }
0084 
0085     if (m_length > b2_linearSlop)
0086     {
0087         m_u *= 1.0f / m_length;
0088     }
0089     else
0090     {
0091         m_u.SetZero();
0092         m_mass = 0.0f;
0093         m_impulse = 0.0f;
0094         return;
0095     }
0096 
0097     // Compute effective mass.
0098     float32 crA = b2Cross(m_rA, m_u);
0099     float32 crB = b2Cross(m_rB, m_u);
0100     float32 invMass = m_invMassA + m_invIA * crA * crA + m_invMassB + m_invIB * crB * crB;
0101 
0102     m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
0103 
0104     if (data.step.warmStarting)
0105     {
0106         // Scale the impulse to support a variable time step.
0107         m_impulse *= data.step.dtRatio;
0108 
0109         b2Vec2 P = m_impulse * m_u;
0110         vA -= m_invMassA * P;
0111         wA -= m_invIA * b2Cross(m_rA, P);
0112         vB += m_invMassB * P;
0113         wB += m_invIB * b2Cross(m_rB, P);
0114     }
0115     else
0116     {
0117         m_impulse = 0.0f;
0118     }
0119 
0120     data.velocities[m_indexA].v = vA;
0121     data.velocities[m_indexA].w = wA;
0122     data.velocities[m_indexB].v = vB;
0123     data.velocities[m_indexB].w = wB;
0124 }
0125 
0126 void b2RopeJoint::SolveVelocityConstraints(const b2SolverData& data)
0127 {
0128     b2Vec2 vA = data.velocities[m_indexA].v;
0129     float32 wA = data.velocities[m_indexA].w;
0130     b2Vec2 vB = data.velocities[m_indexB].v;
0131     float32 wB = data.velocities[m_indexB].w;
0132 
0133     // Cdot = dot(u, v + cross(w, r))
0134     b2Vec2 vpA = vA + b2Cross(wA, m_rA);
0135     b2Vec2 vpB = vB + b2Cross(wB, m_rB);
0136     float32 C = m_length - m_maxLength;
0137     float32 Cdot = b2Dot(m_u, vpB - vpA);
0138 
0139     // Predictive constraint.
0140     if (C < 0.0f)
0141     {
0142         Cdot += data.step.inv_dt * C;
0143     }
0144 
0145     float32 impulse = -m_mass * Cdot;
0146     float32 oldImpulse = m_impulse;
0147     m_impulse = b2Min(0.0f, m_impulse + impulse);
0148     impulse = m_impulse - oldImpulse;
0149 
0150     b2Vec2 P = impulse * m_u;
0151     vA -= m_invMassA * P;
0152     wA -= m_invIA * b2Cross(m_rA, P);
0153     vB += m_invMassB * P;
0154     wB += m_invIB * b2Cross(m_rB, P);
0155 
0156     data.velocities[m_indexA].v = vA;
0157     data.velocities[m_indexA].w = wA;
0158     data.velocities[m_indexB].v = vB;
0159     data.velocities[m_indexB].w = wB;
0160 }
0161 
0162 bool b2RopeJoint::SolvePositionConstraints(const b2SolverData& data)
0163 {
0164     b2Vec2 cA = data.positions[m_indexA].c;
0165     float32 aA = data.positions[m_indexA].a;
0166     b2Vec2 cB = data.positions[m_indexB].c;
0167     float32 aB = data.positions[m_indexB].a;
0168 
0169     b2Rot qA(aA), qB(aB);
0170 
0171     b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
0172     b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
0173     b2Vec2 u = cB + rB - cA - rA;
0174 
0175     float32 length = u.Normalize();
0176     float32 C = length - m_maxLength;
0177 
0178     C = b2Clamp(C, 0.0f, b2_maxLinearCorrection);
0179 
0180     float32 impulse = -m_mass * C;
0181     b2Vec2 P = impulse * u;
0182 
0183     cA -= m_invMassA * P;
0184     aA -= m_invIA * b2Cross(rA, P);
0185     cB += m_invMassB * P;
0186     aB += m_invIB * b2Cross(rB, P);
0187 
0188     data.positions[m_indexA].c = cA;
0189     data.positions[m_indexA].a = aA;
0190     data.positions[m_indexB].c = cB;
0191     data.positions[m_indexB].a = aB;
0192 
0193     return length - m_maxLength < b2_linearSlop;
0194 }
0195 
0196 b2Vec2 b2RopeJoint::GetAnchorA() const
0197 {
0198     return m_bodyA->GetWorldPoint(m_localAnchorA);
0199 }
0200 
0201 b2Vec2 b2RopeJoint::GetAnchorB() const
0202 {
0203     return m_bodyB->GetWorldPoint(m_localAnchorB);
0204 }
0205 
0206 b2Vec2 b2RopeJoint::GetReactionForce(float32 inv_dt) const
0207 {
0208     b2Vec2 F = (inv_dt * m_impulse) * m_u;
0209     return F;
0210 }
0211 
0212 float32 b2RopeJoint::GetReactionTorque(float32 inv_dt) const
0213 {
0214     B2_NOT_USED(inv_dt);
0215     return 0.0f;
0216 }
0217 
0218 float32 b2RopeJoint::GetMaxLength() const
0219 {
0220     return m_maxLength;
0221 }
0222 
0223 b2LimitState b2RopeJoint::GetLimitState() const
0224 {
0225     return m_state;
0226 }
0227 
0228 void b2RopeJoint::Dump()
0229 {
0230     int32 indexA = m_bodyA->m_islandIndex;
0231     int32 indexB = m_bodyB->m_islandIndex;
0232 
0233     b2Log("  b2RopeJointDef jd;\n");
0234     b2Log("  jd.bodyA = bodies[%d];\n", indexA);
0235     b2Log("  jd.bodyB = bodies[%d];\n", indexB);
0236     b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
0237     b2Log("  jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
0238     b2Log("  jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
0239     b2Log("  jd.maxLength = %.15lef;\n", m_maxLength);
0240     b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
0241 }