b2RopeJoint.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2007-2011 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Dynamics/Joints/b2RopeJoint.h>
00020 #include <Box2D/Dynamics/b2Body.h>
00021 #include <Box2D/Dynamics/b2TimeStep.h>
00022 
00023 
00024 // Limit:
00025 // C = norm(pB - pA) - L
00026 // u = (pB - pA) / norm(pB - pA)
00027 // Cdot = dot(u, vB + cross(wB, rB) - vA - cross(wA, rA))
00028 // J = [-u -cross(rA, u) u cross(rB, u)]
00029 // K = J * invM * JT
00030 //   = invMassA + invIA * cross(rA, u)^2 + invMassB + invIB * cross(rB, u)^2
00031 
00032 b2RopeJoint::b2RopeJoint(const b2RopeJointDef* def)
00033 : b2Joint(def)
00034 {
00035         m_localAnchorA = def->localAnchorA;
00036         m_localAnchorB = def->localAnchorB;
00037 
00038         m_maxLength = def->maxLength;
00039 
00040         m_mass = 0.0f;
00041         m_impulse = 0.0f;
00042         m_state = e_inactiveLimit;
00043         m_length = 0.0f;
00044 }
00045 
00046 void b2RopeJoint::InitVelocityConstraints(const b2SolverData& data)
00047 {
00048         m_indexA = m_bodyA->m_islandIndex;
00049         m_indexB = m_bodyB->m_islandIndex;
00050         m_localCenterA = m_bodyA->m_sweep.localCenter;
00051         m_localCenterB = m_bodyB->m_sweep.localCenter;
00052         m_invMassA = m_bodyA->m_invMass;
00053         m_invMassB = m_bodyB->m_invMass;
00054         m_invIA = m_bodyA->m_invI;
00055         m_invIB = m_bodyB->m_invI;
00056 
00057         b2Vec2 cA = data.positions[m_indexA].c;
00058         float32 aA = data.positions[m_indexA].a;
00059         b2Vec2 vA = data.velocities[m_indexA].v;
00060         float32 wA = data.velocities[m_indexA].w;
00061 
00062         b2Vec2 cB = data.positions[m_indexB].c;
00063         float32 aB = data.positions[m_indexB].a;
00064         b2Vec2 vB = data.velocities[m_indexB].v;
00065         float32 wB = data.velocities[m_indexB].w;
00066 
00067         b2Rot qA(aA), qB(aB);
00068 
00069         m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
00070         m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
00071         m_u = cB + m_rB - cA - m_rA;
00072 
00073         m_length = m_u.Length();
00074 
00075         float32 C = m_length - m_maxLength;
00076         if (C > 0.0f)
00077         {
00078                 m_state = e_atUpperLimit;
00079         }
00080         else
00081         {
00082                 m_state = e_inactiveLimit;
00083         }
00084 
00085         if (m_length > b2_linearSlop)
00086         {
00087                 m_u *= 1.0f / m_length;
00088         }
00089         else
00090         {
00091                 m_u.SetZero();
00092                 m_mass = 0.0f;
00093                 m_impulse = 0.0f;
00094                 return;
00095         }
00096 
00097         // Compute effective mass.
00098         float32 crA = b2Cross(m_rA, m_u);
00099         float32 crB = b2Cross(m_rB, m_u);
00100         float32 invMass = m_invMassA + m_invIA * crA * crA + m_invMassB + m_invIB * crB * crB;
00101 
00102         m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
00103 
00104         if (data.step.warmStarting)
00105         {
00106                 // Scale the impulse to support a variable time step.
00107                 m_impulse *= data.step.dtRatio;
00108 
00109                 b2Vec2 P = m_impulse * m_u;
00110                 vA -= m_invMassA * P;
00111                 wA -= m_invIA * b2Cross(m_rA, P);
00112                 vB += m_invMassB * P;
00113                 wB += m_invIB * b2Cross(m_rB, P);
00114         }
00115         else
00116         {
00117                 m_impulse = 0.0f;
00118         }
00119 
00120         data.velocities[m_indexA].v = vA;
00121         data.velocities[m_indexA].w = wA;
00122         data.velocities[m_indexB].v = vB;
00123         data.velocities[m_indexB].w = wB;
00124 }
00125 
00126 void b2RopeJoint::SolveVelocityConstraints(const b2SolverData& data)
00127 {
00128         b2Vec2 vA = data.velocities[m_indexA].v;
00129         float32 wA = data.velocities[m_indexA].w;
00130         b2Vec2 vB = data.velocities[m_indexB].v;
00131         float32 wB = data.velocities[m_indexB].w;
00132 
00133         // Cdot = dot(u, v + cross(w, r))
00134         b2Vec2 vpA = vA + b2Cross(wA, m_rA);
00135         b2Vec2 vpB = vB + b2Cross(wB, m_rB);
00136         float32 C = m_length - m_maxLength;
00137         float32 Cdot = b2Dot(m_u, vpB - vpA);
00138 
00139         // Predictive constraint.
00140         if (C < 0.0f)
00141         {
00142                 Cdot += data.step.inv_dt * C;
00143         }
00144 
00145         float32 impulse = -m_mass * Cdot;
00146         float32 oldImpulse = m_impulse;
00147         m_impulse = b2Min(0.0f, m_impulse + impulse);
00148         impulse = m_impulse - oldImpulse;
00149 
00150         b2Vec2 P = impulse * m_u;
00151         vA -= m_invMassA * P;
00152         wA -= m_invIA * b2Cross(m_rA, P);
00153         vB += m_invMassB * P;
00154         wB += m_invIB * b2Cross(m_rB, P);
00155 
00156         data.velocities[m_indexA].v = vA;
00157         data.velocities[m_indexA].w = wA;
00158         data.velocities[m_indexB].v = vB;
00159         data.velocities[m_indexB].w = wB;
00160 }
00161 
00162 bool b2RopeJoint::SolvePositionConstraints(const b2SolverData& data)
00163 {
00164         b2Vec2 cA = data.positions[m_indexA].c;
00165         float32 aA = data.positions[m_indexA].a;
00166         b2Vec2 cB = data.positions[m_indexB].c;
00167         float32 aB = data.positions[m_indexB].a;
00168 
00169         b2Rot qA(aA), qB(aB);
00170 
00171         b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
00172         b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
00173         b2Vec2 u = cB + rB - cA - rA;
00174 
00175         float32 length = u.Normalize();
00176         float32 C = length - m_maxLength;
00177 
00178         C = b2Clamp(C, 0.0f, b2_maxLinearCorrection);
00179 
00180         float32 impulse = -m_mass * C;
00181         b2Vec2 P = impulse * u;
00182 
00183         cA -= m_invMassA * P;
00184         aA -= m_invIA * b2Cross(rA, P);
00185         cB += m_invMassB * P;
00186         aB += m_invIB * b2Cross(rB, P);
00187 
00188         data.positions[m_indexA].c = cA;
00189         data.positions[m_indexA].a = aA;
00190         data.positions[m_indexB].c = cB;
00191         data.positions[m_indexB].a = aB;
00192 
00193         return length - m_maxLength < b2_linearSlop;
00194 }
00195 
00196 b2Vec2 b2RopeJoint::GetAnchorA() const
00197 {
00198         return m_bodyA->GetWorldPoint(m_localAnchorA);
00199 }
00200 
00201 b2Vec2 b2RopeJoint::GetAnchorB() const
00202 {
00203         return m_bodyB->GetWorldPoint(m_localAnchorB);
00204 }
00205 
00206 b2Vec2 b2RopeJoint::GetReactionForce(float32 inv_dt) const
00207 {
00208         b2Vec2 F = (inv_dt * m_impulse) * m_u;
00209         return F;
00210 }
00211 
00212 float32 b2RopeJoint::GetReactionTorque(float32 inv_dt) const
00213 {
00214         B2_NOT_USED(inv_dt);
00215         return 0.0f;
00216 }
00217 
00218 float32 b2RopeJoint::GetMaxLength() const
00219 {
00220         return m_maxLength;
00221 }
00222 
00223 b2LimitState b2RopeJoint::GetLimitState() const
00224 {
00225         return m_state;
00226 }
00227 
00228 void b2RopeJoint::Dump()
00229 {
00230         int32 indexA = m_bodyA->m_islandIndex;
00231         int32 indexB = m_bodyB->m_islandIndex;
00232 
00233         b2Log("  b2RopeJointDef jd;\n");
00234         b2Log("  jd.bodyA = bodies[%d];\n", indexA);
00235         b2Log("  jd.bodyB = bodies[%d];\n", indexB);
00236         b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
00237         b2Log("  jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
00238         b2Log("  jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
00239         b2Log("  jd.maxLength = %.15lef;\n", m_maxLength);
00240         b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
00241 }


mvsim
Author(s):
autogenerated on Thu Jun 6 2019 22:08:35