2010-09-14 14:47:12 +08:00
|
|
|
/*
|
2011-11-25 18:30:27 +08:00
|
|
|
* Copyright (c) 2006-2011 Erin Catto http://www.box2d.org
|
2010-09-14 14:47:12 +08:00
|
|
|
*
|
|
|
|
* This software is provided 'as-is', without any express or implied
|
|
|
|
* warranty. In no event will the authors be held liable for any damages
|
|
|
|
* arising from the use of this software.
|
|
|
|
* Permission is granted to anyone to use this software for any purpose,
|
|
|
|
* including commercial applications, and to alter it and redistribute it
|
|
|
|
* freely, subject to the following restrictions:
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
* claim that you wrote the original software. If you use this software
|
|
|
|
* in a product, an acknowledgment in the product documentation would be
|
|
|
|
* appreciated but is not required.
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
* misrepresented as being the original software.
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Box2D/Dynamics/Joints/b2FrictionJoint.h>
|
|
|
|
#include <Box2D/Dynamics/b2Body.h>
|
|
|
|
#include <Box2D/Dynamics/b2TimeStep.h>
|
|
|
|
|
|
|
|
// Point-to-point constraint
|
|
|
|
// Cdot = v2 - v1
|
|
|
|
// = v2 + cross(w2, r2) - v1 - cross(w1, r1)
|
|
|
|
// J = [-I -r1_skew I r2_skew ]
|
|
|
|
// Identity used:
|
|
|
|
// w k % (rx i + ry j) = w * (-ry i + rx j)
|
|
|
|
|
|
|
|
// Angle constraint
|
|
|
|
// Cdot = w2 - w1
|
|
|
|
// J = [0 0 -1 0 0 1]
|
|
|
|
// K = invI1 + invI2
|
|
|
|
|
|
|
|
void b2FrictionJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
|
|
|
|
{
|
|
|
|
bodyA = bA;
|
|
|
|
bodyB = bB;
|
|
|
|
localAnchorA = bodyA->GetLocalPoint(anchor);
|
|
|
|
localAnchorB = bodyB->GetLocalPoint(anchor);
|
|
|
|
}
|
|
|
|
|
|
|
|
b2FrictionJoint::b2FrictionJoint(const b2FrictionJointDef* def)
|
|
|
|
: b2Joint(def)
|
|
|
|
{
|
|
|
|
m_localAnchorA = def->localAnchorA;
|
|
|
|
m_localAnchorB = def->localAnchorB;
|
|
|
|
|
|
|
|
m_linearImpulse.SetZero();
|
|
|
|
m_angularImpulse = 0.0f;
|
|
|
|
|
|
|
|
m_maxForce = def->maxForce;
|
|
|
|
m_maxTorque = def->maxTorque;
|
|
|
|
}
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
void b2FrictionJoint::InitVelocityConstraints(const b2SolverData& data)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2011-11-25 18:30:27 +08:00
|
|
|
m_indexA = m_bodyA->m_islandIndex;
|
|
|
|
m_indexB = m_bodyB->m_islandIndex;
|
|
|
|
m_localCenterA = m_bodyA->m_sweep.localCenter;
|
|
|
|
m_localCenterB = m_bodyB->m_sweep.localCenter;
|
|
|
|
m_invMassA = m_bodyA->m_invMass;
|
|
|
|
m_invMassB = m_bodyB->m_invMass;
|
|
|
|
m_invIA = m_bodyA->m_invI;
|
|
|
|
m_invIB = m_bodyB->m_invI;
|
|
|
|
|
|
|
|
float32 aA = data.positions[m_indexA].a;
|
|
|
|
b2Vec2 vA = data.velocities[m_indexA].v;
|
|
|
|
float32 wA = data.velocities[m_indexA].w;
|
|
|
|
|
|
|
|
float32 aB = data.positions[m_indexB].a;
|
|
|
|
b2Vec2 vB = data.velocities[m_indexB].v;
|
|
|
|
float32 wB = data.velocities[m_indexB].w;
|
|
|
|
|
|
|
|
b2Rot qA(aA), qB(aB);
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
// Compute the effective mass matrix.
|
2011-11-25 18:30:27 +08:00
|
|
|
m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
|
|
|
|
m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
// J = [-I -r1_skew I r2_skew]
|
|
|
|
// [ 0 -1 0 1]
|
|
|
|
// r_skew = [-ry; rx]
|
|
|
|
|
|
|
|
// Matlab
|
|
|
|
// K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
|
|
|
|
// [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
|
|
|
|
// [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
float32 mA = m_invMassA, mB = m_invMassB;
|
|
|
|
float32 iA = m_invIA, iB = m_invIB;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
b2Mat22 K;
|
|
|
|
K.ex.x = mA + mB + iA * m_rA.y * m_rA.y + iB * m_rB.y * m_rB.y;
|
|
|
|
K.ex.y = -iA * m_rA.x * m_rA.y - iB * m_rB.x * m_rB.y;
|
|
|
|
K.ey.x = K.ex.y;
|
|
|
|
K.ey.y = mA + mB + iA * m_rA.x * m_rA.x + iB * m_rB.x * m_rB.x;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
m_linearMass = K.GetInverse();
|
|
|
|
|
|
|
|
m_angularMass = iA + iB;
|
|
|
|
if (m_angularMass > 0.0f)
|
|
|
|
{
|
|
|
|
m_angularMass = 1.0f / m_angularMass;
|
|
|
|
}
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
if (data.step.warmStarting)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
|
|
|
// Scale impulses to support a variable time step.
|
2011-11-25 18:30:27 +08:00
|
|
|
m_linearImpulse *= data.step.dtRatio;
|
|
|
|
m_angularImpulse *= data.step.dtRatio;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
b2Vec2 P(m_linearImpulse.x, m_linearImpulse.y);
|
2011-11-25 18:30:27 +08:00
|
|
|
vA -= mA * P;
|
|
|
|
wA -= iA * (b2Cross(m_rA, P) + m_angularImpulse);
|
|
|
|
vB += mB * P;
|
|
|
|
wB += iB * (b2Cross(m_rB, P) + m_angularImpulse);
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_linearImpulse.SetZero();
|
|
|
|
m_angularImpulse = 0.0f;
|
|
|
|
}
|
2011-11-25 18:30:27 +08:00
|
|
|
|
|
|
|
data.velocities[m_indexA].v = vA;
|
|
|
|
data.velocities[m_indexA].w = wA;
|
|
|
|
data.velocities[m_indexB].v = vB;
|
|
|
|
data.velocities[m_indexB].w = wB;
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
void b2FrictionJoint::SolveVelocityConstraints(const b2SolverData& data)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2011-11-25 18:30:27 +08:00
|
|
|
b2Vec2 vA = data.velocities[m_indexA].v;
|
|
|
|
float32 wA = data.velocities[m_indexA].w;
|
|
|
|
b2Vec2 vB = data.velocities[m_indexB].v;
|
|
|
|
float32 wB = data.velocities[m_indexB].w;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
float32 mA = m_invMassA, mB = m_invMassB;
|
|
|
|
float32 iA = m_invIA, iB = m_invIB;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
float32 h = data.step.dt;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
// Solve angular friction
|
|
|
|
{
|
|
|
|
float32 Cdot = wB - wA;
|
|
|
|
float32 impulse = -m_angularMass * Cdot;
|
|
|
|
|
|
|
|
float32 oldImpulse = m_angularImpulse;
|
2011-11-25 18:30:27 +08:00
|
|
|
float32 maxImpulse = h * m_maxTorque;
|
2010-09-14 14:47:12 +08:00
|
|
|
m_angularImpulse = b2Clamp(m_angularImpulse + impulse, -maxImpulse, maxImpulse);
|
|
|
|
impulse = m_angularImpulse - oldImpulse;
|
|
|
|
|
|
|
|
wA -= iA * impulse;
|
|
|
|
wB += iB * impulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Solve linear friction
|
|
|
|
{
|
2011-11-25 18:30:27 +08:00
|
|
|
b2Vec2 Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA);
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
b2Vec2 impulse = -b2Mul(m_linearMass, Cdot);
|
|
|
|
b2Vec2 oldImpulse = m_linearImpulse;
|
|
|
|
m_linearImpulse += impulse;
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
float32 maxImpulse = h * m_maxForce;
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
if (m_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
|
|
|
|
{
|
|
|
|
m_linearImpulse.Normalize();
|
|
|
|
m_linearImpulse *= maxImpulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
impulse = m_linearImpulse - oldImpulse;
|
|
|
|
|
|
|
|
vA -= mA * impulse;
|
2011-11-25 18:30:27 +08:00
|
|
|
wA -= iA * b2Cross(m_rA, impulse);
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
vB += mB * impulse;
|
2011-11-25 18:30:27 +08:00
|
|
|
wB += iB * b2Cross(m_rB, impulse);
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
data.velocities[m_indexA].v = vA;
|
|
|
|
data.velocities[m_indexA].w = wA;
|
|
|
|
data.velocities[m_indexB].v = vB;
|
|
|
|
data.velocities[m_indexB].w = wB;
|
2010-09-14 14:47:12 +08:00
|
|
|
}
|
|
|
|
|
2011-11-25 18:30:27 +08:00
|
|
|
bool b2FrictionJoint::SolvePositionConstraints(const b2SolverData& data)
|
2010-09-14 14:47:12 +08:00
|
|
|
{
|
2011-11-25 18:30:27 +08:00
|
|
|
B2_NOT_USED(data);
|
2010-09-14 14:47:12 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
b2Vec2 b2FrictionJoint::GetAnchorA() const
|
|
|
|
{
|
|
|
|
return m_bodyA->GetWorldPoint(m_localAnchorA);
|
|
|
|
}
|
|
|
|
|
|
|
|
b2Vec2 b2FrictionJoint::GetAnchorB() const
|
|
|
|
{
|
|
|
|
return m_bodyB->GetWorldPoint(m_localAnchorB);
|
|
|
|
}
|
|
|
|
|
|
|
|
b2Vec2 b2FrictionJoint::GetReactionForce(float32 inv_dt) const
|
|
|
|
{
|
|
|
|
return inv_dt * m_linearImpulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
float32 b2FrictionJoint::GetReactionTorque(float32 inv_dt) const
|
|
|
|
{
|
|
|
|
return inv_dt * m_angularImpulse;
|
|
|
|
}
|
|
|
|
|
|
|
|
void b2FrictionJoint::SetMaxForce(float32 force)
|
|
|
|
{
|
|
|
|
b2Assert(b2IsValid(force) && force >= 0.0f);
|
|
|
|
m_maxForce = force;
|
|
|
|
}
|
|
|
|
|
|
|
|
float32 b2FrictionJoint::GetMaxForce() const
|
|
|
|
{
|
|
|
|
return m_maxForce;
|
|
|
|
}
|
|
|
|
|
|
|
|
void b2FrictionJoint::SetMaxTorque(float32 torque)
|
|
|
|
{
|
|
|
|
b2Assert(b2IsValid(torque) && torque >= 0.0f);
|
|
|
|
m_maxTorque = torque;
|
|
|
|
}
|
|
|
|
|
|
|
|
float32 b2FrictionJoint::GetMaxTorque() const
|
|
|
|
{
|
|
|
|
return m_maxTorque;
|
|
|
|
}
|
2011-11-25 18:30:27 +08:00
|
|
|
|
|
|
|
void b2FrictionJoint::Dump()
|
|
|
|
{
|
|
|
|
int32 indexA = m_bodyA->m_islandIndex;
|
|
|
|
int32 indexB = m_bodyB->m_islandIndex;
|
|
|
|
|
|
|
|
b2Log(" b2FrictionJointDef jd;\n");
|
|
|
|
b2Log(" jd.bodyA = bodies[%d];\n", indexA);
|
|
|
|
b2Log(" jd.bodyB = bodies[%d];\n", indexB);
|
|
|
|
b2Log(" jd.collideConnected = bool(%d);\n", m_collideConnected);
|
|
|
|
b2Log(" jd.localAnchorA.Set(%.15lef, %.15lef);\n", m_localAnchorA.x, m_localAnchorA.y);
|
|
|
|
b2Log(" jd.localAnchorB.Set(%.15lef, %.15lef);\n", m_localAnchorB.x, m_localAnchorB.y);
|
|
|
|
b2Log(" jd.maxForce = %.15lef;\n", m_maxForce);
|
|
|
|
b2Log(" jd.maxTorque = %.15lef;\n", m_maxTorque);
|
|
|
|
b2Log(" joints[%d] = m_world->CreateJoint(&jd);\n", m_index);
|
|
|
|
}
|