b2DistanceJoint.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-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/b2DistanceJoint.h>
00020 #include <Box2D/Dynamics/b2Body.h>
00021 #include <Box2D/Dynamics/b2TimeStep.h>
00022 
00023 // 1-D constrained system
00024 // m (v2 - v1) = lambda
00025 // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
00026 // x2 = x1 + h * v2
00027 
00028 // 1-D mass-damper-spring system
00029 // m (v2 - v1) + h * d * v2 + h * k * 
00030 
00031 // C = norm(p2 - p1) - L
00032 // u = (p2 - p1) / norm(p2 - p1)
00033 // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
00034 // J = [-u -cross(r1, u) u cross(r2, u)]
00035 // K = J * invM * JT
00036 //   = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
00037 
00038 void b2DistanceJointDef::Initialize(b2Body* b1, b2Body* b2,
00039                                                                         const b2Vec2& anchor1, const b2Vec2& anchor2)
00040 {
00041         bodyA = b1;
00042         bodyB = b2;
00043         localAnchorA = bodyA->GetLocalPoint(anchor1);
00044         localAnchorB = bodyB->GetLocalPoint(anchor2);
00045         b2Vec2 d = anchor2 - anchor1;
00046         length = d.Length();
00047 }
00048 
00049 b2DistanceJoint::b2DistanceJoint(const b2DistanceJointDef* def)
00050 : b2Joint(def)
00051 {
00052         m_localAnchorA = def->localAnchorA;
00053         m_localAnchorB = def->localAnchorB;
00054         m_length = def->length;
00055         m_frequencyHz = def->frequencyHz;
00056         m_dampingRatio = def->dampingRatio;
00057         m_impulse = 0.0f;
00058         m_gamma = 0.0f;
00059         m_bias = 0.0f;
00060 }
00061 
00062 void b2DistanceJoint::InitVelocityConstraints(const b2SolverData& data)
00063 {
00064         m_indexA = m_bodyA->m_islandIndex;
00065         m_indexB = m_bodyB->m_islandIndex;
00066         m_localCenterA = m_bodyA->m_sweep.localCenter;
00067         m_localCenterB = m_bodyB->m_sweep.localCenter;
00068         m_invMassA = m_bodyA->m_invMass;
00069         m_invMassB = m_bodyB->m_invMass;
00070         m_invIA = m_bodyA->m_invI;
00071         m_invIB = m_bodyB->m_invI;
00072 
00073         b2Vec2 cA = data.positions[m_indexA].c;
00074         float32 aA = data.positions[m_indexA].a;
00075         b2Vec2 vA = data.velocities[m_indexA].v;
00076         float32 wA = data.velocities[m_indexA].w;
00077 
00078         b2Vec2 cB = data.positions[m_indexB].c;
00079         float32 aB = data.positions[m_indexB].a;
00080         b2Vec2 vB = data.velocities[m_indexB].v;
00081         float32 wB = data.velocities[m_indexB].w;
00082 
00083         b2Rot qA(aA), qB(aB);
00084 
00085         m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
00086         m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
00087         m_u = cB + m_rB - cA - m_rA;
00088 
00089         // Handle singularity.
00090         float32 length = m_u.Length();
00091         if (length > b2_linearSlop)
00092         {
00093                 m_u *= 1.0f / length;
00094         }
00095         else
00096         {
00097                 m_u.Set(0.0f, 0.0f);
00098         }
00099 
00100         float32 crAu = b2Cross(m_rA, m_u);
00101         float32 crBu = b2Cross(m_rB, m_u);
00102         float32 invMass = m_invMassA + m_invIA * crAu * crAu + m_invMassB + m_invIB * crBu * crBu;
00103 
00104         // Compute the effective mass matrix.
00105         m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
00106 
00107         if (m_frequencyHz > 0.0f)
00108         {
00109                 float32 C = length - m_length;
00110 
00111                 // Frequency
00112                 float32 omega = 2.0f * b2_pi * m_frequencyHz;
00113 
00114                 // Damping coefficient
00115                 float32 d = 2.0f * m_mass * m_dampingRatio * omega;
00116 
00117                 // Spring stiffness
00118                 float32 k = m_mass * omega * omega;
00119 
00120                 // magic formulas
00121                 float32 h = data.step.dt;
00122                 m_gamma = h * (d + h * k);
00123                 m_gamma = m_gamma != 0.0f ? 1.0f / m_gamma : 0.0f;
00124                 m_bias = C * h * k * m_gamma;
00125 
00126                 invMass += m_gamma;
00127                 m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
00128         }
00129         else
00130         {
00131                 m_gamma = 0.0f;
00132                 m_bias = 0.0f;
00133         }
00134 
00135         if (data.step.warmStarting)
00136         {
00137                 // Scale the impulse to support a variable time step.
00138                 m_impulse *= data.step.dtRatio;
00139 
00140                 b2Vec2 P = m_impulse * m_u;
00141                 vA -= m_invMassA * P;
00142                 wA -= m_invIA * b2Cross(m_rA, P);
00143                 vB += m_invMassB * P;
00144                 wB += m_invIB * b2Cross(m_rB, P);
00145         }
00146         else
00147         {
00148                 m_impulse = 0.0f;
00149         }
00150 
00151         data.velocities[m_indexA].v = vA;
00152         data.velocities[m_indexA].w = wA;
00153         data.velocities[m_indexB].v = vB;
00154         data.velocities[m_indexB].w = wB;
00155 }
00156 
00157 void b2DistanceJoint::SolveVelocityConstraints(const b2SolverData& data)
00158 {
00159         b2Vec2 vA = data.velocities[m_indexA].v;
00160         float32 wA = data.velocities[m_indexA].w;
00161         b2Vec2 vB = data.velocities[m_indexB].v;
00162         float32 wB = data.velocities[m_indexB].w;
00163 
00164         // Cdot = dot(u, v + cross(w, r))
00165         b2Vec2 vpA = vA + b2Cross(wA, m_rA);
00166         b2Vec2 vpB = vB + b2Cross(wB, m_rB);
00167         float32 Cdot = b2Dot(m_u, vpB - vpA);
00168 
00169         float32 impulse = -m_mass * (Cdot + m_bias + m_gamma * m_impulse);
00170         m_impulse += impulse;
00171 
00172         b2Vec2 P = impulse * m_u;
00173         vA -= m_invMassA * P;
00174         wA -= m_invIA * b2Cross(m_rA, P);
00175         vB += m_invMassB * P;
00176         wB += m_invIB * b2Cross(m_rB, P);
00177 
00178         data.velocities[m_indexA].v = vA;
00179         data.velocities[m_indexA].w = wA;
00180         data.velocities[m_indexB].v = vB;
00181         data.velocities[m_indexB].w = wB;
00182 }
00183 
00184 bool b2DistanceJoint::SolvePositionConstraints(const b2SolverData& data)
00185 {
00186         if (m_frequencyHz > 0.0f)
00187         {
00188                 // There is no position correction for soft distance constraints.
00189                 return true;
00190         }
00191 
00192         b2Vec2 cA = data.positions[m_indexA].c;
00193         float32 aA = data.positions[m_indexA].a;
00194         b2Vec2 cB = data.positions[m_indexB].c;
00195         float32 aB = data.positions[m_indexB].a;
00196 
00197         b2Rot qA(aA), qB(aB);
00198 
00199         b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
00200         b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
00201         b2Vec2 u = cB + rB - cA - rA;
00202 
00203         float32 length = u.Normalize();
00204         float32 C = length - m_length;
00205         C = b2Clamp(C, -b2_maxLinearCorrection, b2_maxLinearCorrection);
00206 
00207         float32 impulse = -m_mass * C;
00208         b2Vec2 P = impulse * u;
00209 
00210         cA -= m_invMassA * P;
00211         aA -= m_invIA * b2Cross(rA, P);
00212         cB += m_invMassB * P;
00213         aB += m_invIB * b2Cross(rB, P);
00214 
00215         data.positions[m_indexA].c = cA;
00216         data.positions[m_indexA].a = aA;
00217         data.positions[m_indexB].c = cB;
00218         data.positions[m_indexB].a = aB;
00219 
00220         return b2Abs(C) < b2_linearSlop;
00221 }
00222 
00223 b2Vec2 b2DistanceJoint::GetAnchorA() const
00224 {
00225         return m_bodyA->GetWorldPoint(m_localAnchorA);
00226 }
00227 
00228 b2Vec2 b2DistanceJoint::GetAnchorB() const
00229 {
00230         return m_bodyB->GetWorldPoint(m_localAnchorB);
00231 }
00232 
00233 b2Vec2 b2DistanceJoint::GetReactionForce(float32 inv_dt) const
00234 {
00235         b2Vec2 F = (inv_dt * m_impulse) * m_u;
00236         return F;
00237 }
00238 
00239 float32 b2DistanceJoint::GetReactionTorque(float32 inv_dt) const
00240 {
00241         B2_NOT_USED(inv_dt);
00242         return 0.0f;
00243 }
00244 
00245 void b2DistanceJoint::Dump()
00246 {
00247         int32 indexA = m_bodyA->m_islandIndex;
00248         int32 indexB = m_bodyB->m_islandIndex;
00249 
00250         b2Log("  b2DistanceJointDef jd;\n");
00251         b2Log("  jd.bodyA = bodies[%d];\n", indexA);
00252         b2Log("  jd.bodyB = bodies[%d];\n", indexB);
00253         b2Log("  jd.collideConnected = bool(%d);\n", m_collideConnected);
00254         b2Log("  jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
00255         b2Log("  jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
00256         b2Log("  jd.length = %.15lef;\n", m_length);
00257         b2Log("  jd.frequencyHz = %.15lef;\n", m_frequencyHz);
00258         b2Log("  jd.dampingRatio = %.15lef;\n", m_dampingRatio);
00259         b2Log("  joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
00260 }


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