//################################################################################################//
//################################################################################################//
// //
// ██ ██████ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// █████ ██ ██ ██ ██ █████ ██ █████ ██ █████ ██ █████ █████ //
// ██ ██ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// █████ ██████ ██████████ █████ ██ █████ █████ █████ ██ ██ ██ ████ //
// //
//################################################################################################//
//################################################################################################//
// CLASS CONSTRUCTOR
/**
* A weld joint essentially glues two bodies together. A weld
* joint may distort somewhat because the island constraint
* solver is approximate.
*
* @class b2WeldJoint
* @constructor
* @param {b2WeldJointDef} weldJointDef
* @extends {b2Joint}
* @module Joints
*/
function b2WeldJoint( weldJointDef ) {
/**
* Invokes parent class constructor function reference.
*/
this.constructor( weldJointDef );
////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// ██████ ██ ██ //
// ██ ██ ██ //
// ██ ██ ████ █████ █████ █████ ████ █████ ██ █████ █████ //
// ██████ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ █████ █████ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ █████ █████ █████ ██ ████ ██ █████ █████ //
// ██ //
// ██ //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////
// property DECLARATIONS
/**
* @public
* @property m_localAnchorA
* @type {b2Vec2}
*/
this.m_localAnchorA = new b2Vec2();
/**
* @public
* @property m_localAnchorB
* @type {b2Vec2}
*/
this.m_localAnchorB = new b2Vec2();
/**
*
* @public
* @property m_impulse
* @type {b2Vec3}
*/
this.m_impulse = new b2Vec3();
/**
*
* @public
* @property m_mass
* @type {b2Mat33}
*/
this.m_mass = new b2Mat33();
/**
* @public
* @property m_rA
* @type {b2Vec2}
*/
this.m_rA = new b2Vec2();
/**
* @public
* @property m_rB
* @type {b2Vec2}
*/
this.m_rB = new b2Vec2();
/**
* @public
* @property m_localCenterA
* @type {b2Vec2}
*/
this.m_localCenterA = new b2Vec2();
/**
* @public
* @property m_localCenterB
* @type {b2Vec2}
*/
this.m_localCenterB = new b2Vec2();
/**
* @public
* @property m_qA
* @type {b2Rot}
*/
this.m_qA = new b2Rot();
/**
* @public
* @property m_qB
* @type {b2Rot}
*/
this.m_qB = new b2Rot();
/**
* @public
* @property m_lalcA
* @type {b2Vec2}
*/
this.m_lalcA = new b2Vec2();
/**
* @public
* @property m_lalcB
* @type {b2Vec2}
*/
this.m_lalcB = new b2Vec2();
/**
* @public
* @property m_K
* @type {b2Mat33}
*/
this.m_K = new b2Mat33();
// property INITIALISATIONS
this.m_localAnchorA.copy( weldJointDef.localAnchorA );
this.m_localAnchorB.copy( weldJointDef.localAnchorB );
this.m_impulse.setZero();
/**
* @public
* @property m_frequencyHz
* @type {float}
*/
this.m_frequencyHz = weldJointDef.frequencyHz;
/**
* @public
* @property m_dampingRatio
* @type {float}
*/
this.m_dampingRatio = weldJointDef.dampingRatio;
/**
* @public
* @property m_referenceAngle
* @type {float}
*/
this.m_referenceAngle = weldJointDef.referenceAngle;
/**
* @public
* @property m_bias
* @type {float}
* @default 0.0
*/
this.m_bias = 0.0;
/**
* @public
* @property m_gamma
* @type {float}
* @default 0.0
*/
this.m_gamma = 0.0;
// Solver temp
/**
* @public
* @property m_indexA
* @type {int}
* @default 0
*/
this.m_indexA = 0;
/**
* @public
* @property m_indexB
* @type {int}
* @default 0
*/
this.m_indexB = 0;
/**
* @public
* @property m_invMassA
* @type {float}
* @default 0.0
*/
this.m_invMassA = 0.0;
/**
* @public
* @property m_invMassB
* @type {float}
* @default 0.0
*/
this.m_invMassB = 0.0;
/**
* @public
* @property m_invMassA
* @type {float}
* @default 0.0
*/
this.m_invIA = 0.0;
/**
* @public
* @property m_invMassB
* @type {float}
* @default 0.0
*/
this.m_invIB = 0.0;
////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// ██ ██ ██ ██ //
// ██ ██ ██ //
// ██ █████ █████ █████ ████ ██ █████ █████ █████ █████ █████ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ █████ ██ ██ ██ █████ ██ ██ ██ █████ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ ██ ██ ██ █████ ██ ██ ████ █████ ██ ██ █████ █████ //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////
} b2WeldJoint.prototype = p = new b2Joint(); Box2D.b2WeldJoint = b2WeldJoint;
// STATIC CLASS PROPERTIES
/**
* Object pool for memory management.
*/
b2WeldJoint._B2VEC2_POOL0 = new b2Vec2;
b2WeldJoint._B2VEC2_POOL1 = new b2Vec2;
b2WeldJoint._B2VEC2_POOL2 = new b2Vec2;
b2WeldJoint._B2VEC3_POOL0 = new b2Vec3;
////////////////////////////////////////////////////////////////////////////////////////////////////
// //
// ██ ██ ██ ██ ██ //
// ███ ███ ██ ██ ██ //
// ███████ █████ █████ █████ █████ █████ █████ //
// ██ █ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ █████ ██ ██ ██ ██ ██ ██ ██ █████ //
// ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ //
// ██ ██ █████ ████ ██ ██ █████ █████ █████ //
// //
////////////////////////////////////////////////////////////////////////////////////////////////////
// INSTANCE METHODS
/**
* @public
* @method getAnchorB
* @param {b2Vec2|Object=} [out=b2Vec2] reusable object
* @return {b2Vec2|Object} out
*/
p.getAnchorA = function ( out ) {
out = out || b2MotorJoint._B2VEC2_POOL0;
return this.m_bodyA.getWorldPoint( this.m_localAnchorA, out );
};
/**
* @public
* @method getAnchorB
* @param {b2Vec2|Object=} [out=b2Vec2] reusable object
* @return {b2Vec2|Object} out
*/
p.getAnchorB = function ( out ) {
out = out || b2MotorJoint._B2VEC2_POOL0;
return this.m_bodyB.getWorldPoint( this.m_localAnchorB, out );
};
/**
* Get the reaction force given the inverse time step.
* Unit is N.
*
* @public
* @method getReactionForce
* @param {float} invDeltaTime
* @param {b2Vec2|Object=} [out=b2Vec2] reusable object
* @return {b2Vec2|Object} out
*/
p.getReactionForce = function ( invDeltaTime, out ) {
out = out || b2MotorJoint._B2VEC2_POOL0;
return out.set( invDeltaTime * this.m_impulse.x, invDeltaTime * this.m_impulse.y );
};
/**
* Get the reaction torque given the inverse time step.
* Unit is N*m. This is always zero for a distance joint.
*
* @public
* @method getReactionTorque
* @param {float} invDeltaTime
* @return {float}
*/
p.getReactionTorque = function ( invDeltaTime ) {
return invDeltaTime * this.m_impulse.z;
};
/**
* The local anchor point relative to bodyA's origin.
*
* @public
* @method getLocalAnchorA
* @param {b2Vec2|Object=} [out=b2Vec2] reusable object
* @return {b2Vec2|Object} out
*/
p.getLocalAnchorA = function ( out ) {
out = out || b2PrismaticJoint._B2VEC2_POOL0;
return out.copy( this.m_localAnchorA );
};
/**
* The local anchor point relative to bodyA's origin.
*
* @public
* @method getLocalAnchorB
* @param {b2Vec2|Object=} [out=b2Vec2] reusable object
* @return {b2Vec2|Object} out
*/
p.getLocalAnchorB = function ( out ) {
out = out || b2PrismaticJoint._B2VEC2_POOL0;
return out.copy( this.m_localAnchorB );
};
/**
* Get the reference angle.
*
* @public
* @method getReferenceAngle
* @return {float}
*/
p.getReferenceAngle = function () {
return this.m_referenceAngle;
};
/**
* Set frequency in Hz.
*
* @public
* @method setFrequency
* @param {float} hz
* @return {void}
*/
p.setFrequency = function ( hz ) {
this.m_frequencyHz = hz;
};
/**
* Get frequency in Hz.
*
* @public
* @method getReferenceAngle
* @return {float}
*/
p.getReferenceAngle = function () {
return this.m_frequencyHz;
};
/**
* Set damping ratio.
*
* @public
* @method setDampingRatio
* @param {float} ratio
* @return {void}
*/
p.setDampingRatio = function ( ratio ) {
this.m_dampingRatio = ratio;
};
/**
* Get damping ratio.
*
* @public
* @method getDampingRatio
* @return {float}
*/
p.getDampingRatio = function () {
return this.m_dampingRatio;
};
/**
* @public
* @override
* @method initVelocityConstraints
* @param {b2SolverData} data
* @return {void}
*/
// Implement b2Joint.initVelocityConstraints
p.initVelocityConstraints = function ( data ) {
this.m_indexA = this.m_bodyA.m_islandIndex;
this.m_indexB = this.m_bodyB.m_islandIndex;
this.m_localCenterA.copy( this.m_bodyA.m_sweep.localCenter );
this.m_localCenterB.copy( this.m_bodyB.m_sweep.localCenter );
this.m_invMassA = this.m_bodyA.m_invMass;
this.m_invMassB = this.m_bodyB.m_invMass;
this.m_invIA = this.m_bodyA.m_invI;
this.m_invIB = this.m_bodyB.m_invI;
/*float32*/var aA = data.positions[this.m_indexA].a;
/*b2Vec2&*/var vA = data.velocities[this.m_indexA].v;
/*float32*/var wA = data.velocities[this.m_indexA].w;
/*float32*/var aB = data.positions[this.m_indexB].a;
/*b2Vec2&*/var vB = data.velocities[this.m_indexB].v;
/*float32*/var wB = data.velocities[this.m_indexB].w;
/*b2Rot*/var qA = this.m_qA.setAngle( aA ), qB = this.m_qB.setAngle( aB );
// m_rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
b2Vec2.subtract( this.m_localAnchorA, this.m_localCenterA, this.m_lalcA );
b2Rot.timesV2( qA, this.m_lalcA, this.m_rA );
// m_rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
b2Vec2.subtract( this.m_localAnchorB, this.m_localCenterB, this.m_lalcB );
b2Rot.timesV2( qB, this.m_lalcB, this.m_rB );
// 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]
/*float32*/var mA = this.m_invMassA, mB = this.m_invMassB;
/*float32*/var iA = this.m_invIA, iB = this.m_invIB;
/*b2Mat33*/var K = this.m_K;
K.ex.x = mA + mB + this.m_rA.y * this.m_rA.y * iA + this.m_rB.y * this.m_rB.y * iB;
K.ey.x = -this.m_rA.y * this.m_rA.x * iA - this.m_rB.y * this.m_rB.x * iB;
K.ez.x = -this.m_rA.y * iA - this.m_rB.y * iB;
K.ex.y = K.ey.x;
K.ey.y = mA + mB + this.m_rA.x * this.m_rA.x * iA + this.m_rB.x * this.m_rB.x * iB;
K.ez.y = this.m_rA.x * iA + this.m_rB.x * iB;
K.ex.z = K.ez.x;
K.ey.z = K.ez.y;
K.ez.z = iA + iB;
if ( this.m_frequencyHz > 0 ) {
K.getInverse22( this.m_mass );
/*float32*/var invM = iA + iB;
/*float32*/var m = invM > 0 ? 1 / invM : 0;
/*float32*/var C = aB - aA - this.m_referenceAngle;
// Frequency
/*float32*/var omega = 2 * Math.PI * this.m_frequencyHz;
// Damping coefficient
/*float32*/var d = 2 * m * this.m_dampingRatio * omega;
// Spring stiffness
/*float32*/var k = m * omega * omega;
// magic formulas
/*float32*/var h = data.step.dt;
this.m_gamma = h * (d + h * k);
this.m_gamma = this.m_gamma !== 0 ? 1 / this.m_gamma : 0;
this.m_bias = C * h * k * this.m_gamma;
invM += this.m_gamma;
this.m_mass.ez.z = invM !== 0 ? 1 / invM : 0;
}
else if ( K.ez.z === 0 ) {
K.getInverse22( this.m_mass );
this.m_gamma = 0;
this.m_bias = 0;
}
else {
K.getSymInverse33( this.m_mass );
this.m_gamma = 0;
this.m_bias = 0;
}
if ( data.step.warmStarting ) {
// Scale impulses to support a variable time step.
this.m_impulse.timesScalar( data.step.dtRatio );
// b2Vec2 P(m_impulse.x, m_impulse.y);
var P = b2WeldJoint._B2VEC2_POOL0.set( this.m_impulse.x, this.m_impulse.y );
// vA -= mA * P;
vA.minusEqualsMul( mA, P );
wA -= iA * (b2Vec2.cross( this.m_rA, P ) + this.m_impulse.z);
// vB += mB * P;
vB.plusEqualsMul( mB, P );
wB += iB * (b2Vec2.cross( this.m_rB, P ) + this.m_impulse.z);
}
else {
this.m_impulse.setZero();
}
// data.velocities[this.m_indexA].v = vA;
data.velocities[this.m_indexA].w = wA;
// data.velocities[this.m_indexB].v = vB;
data.velocities[this.m_indexB].w = wB;
};
/**
*
* @public
* @override
* @method solveVelocityConstraints
* @param {b2SolverData} data
* @return {void}
*/
// Implement b2Joint.solveVelocityConstraints
p.solveVelocityConstraints = function ( data ) {
/*b2Vec2&*/var vA = data.velocities[this.m_indexA].v;
/*float32*/var wA = data.velocities[this.m_indexA].w;
/*b2Vec2&*/var vB = data.velocities[this.m_indexB].v;
/*float32*/var wB = data.velocities[this.m_indexB].w;
/*float32*/var mA = this.m_invMassA, mB = this.m_invMassB;
/*float32*/var iA = this.m_invIA, iB = this.m_invIB;
if ( this.m_frequencyHz > 0 ) {
/*float32*/var Cdot2 = wB - wA;
/*float32*/var impulse2 = -this.m_mass.ez.z * (Cdot2 + this.m_bias + this.m_gamma * this.m_impulse.z);
this.m_impulse.z += impulse2;
wA -= iA * impulse2;
wB += iB * impulse2;
// b2Vec2 Cdot1 = vB + b2CrossSV(wB, this.m_rB) - vA - b2CrossSV(wA, this.m_rA);
var Cdot1 = b2Vec2.subtract(
b2Vec2.vPlusCrossFV( vB, wB, this.m_rB, b2Vec2.POOL0 ),
b2Vec2.vPlusCrossFV( vA, wA, this.m_rA, b2Vec2.POOL1 ),
b2WeldJoint._B2VEC2_POOL0 );
// b2Vec2 impulse1 = -b2Mul22(m_mass, Cdot1);
var impulse1 = b2Mat33.multiplyXY( this.m_mass, Cdot1.x, Cdot1.y, b2WeldJoint._B2VEC2_POOL1 ).negative();
this.m_impulse.x += impulse1.x;
this.m_impulse.y += impulse1.y;
// b2Vec2 P = impulse1;
var P = impulse1;
// vA -= mA * P;
vA.minusEqualsMul( mA, P );
// wA -= iA * b2Cross(m_rA, P);
wA -= iA * b2Vec2.cross( this.m_rA, P );
// vB += mB * P;
vB.plusEqualsMul( mB, P );
// wB += iB * b2Cross(m_rB, P);
wB += iB * b2Vec2.cross( this.m_rB, P );
}
else {
// b2Vec2 Cdot1 = vB + b2Cross(wB, this.m_rB) - vA - b2Cross(wA, this.m_rA);
var Cdot1 = b2Vec2.subtract(
b2Vec2.vPlusCrossFV( vB, wB, this.m_rB, b2Vec2.POOL0 ),
b2Vec2.vPlusCrossFV( vA, wA, this.m_rA, b2Vec2.POOL1 ),
b2WeldJoint._B2VEC2_POOL0 );
/*float32*/var Cdot2 = wB - wA;
// b2Vec3 var Cdot(Cdot1.x, Cdot1.y, Cdot2);
// b2Vec3 impulse = -b2Mul(m_mass, Cdot);
var impulse = b2Mat33.multiplyXYZ( this.m_mass, Cdot1.x, Cdot1.y, Cdot2, b2WeldJoint._B2VEC3_POOL0 ).negative();
this.m_impulse.plus( impulse );
// b2Vec2 P(impulse.x, impulse.y);
var P = b2WeldJoint._B2VEC2_POOL2.set( impulse.x, impulse.y );
// vA -= mA * P;
vA.minusEqualsMul( mA, P );
wA -= iA * (b2Vec2.cross( this.m_rA, P ) + impulse.z);
// vB += mB * P;
vB.plusEqualsMul( mB, P );
wB += iB * (b2Vec2.cross( this.m_rB, P ) + impulse.z);
}
// data.velocities[this.m_indexA].v = vA;
data.velocities[this.m_indexA].w = wA;
// data.velocities[this.m_indexB].v = vB;
data.velocities[this.m_indexB].w = wB;
};
/**
* @public
* @override
* @method solvePositionConstraints
* @param {b2SolverData} data
* @return {void}
*/
// Implement b2Joint.solvePositionConstraints
p.solvePositionConstraints = function ( data ) {
/*b2Vec2&*/var cA = data.positions[this.m_indexA].c;
/*float32*/var aA = data.positions[this.m_indexA].a;
/*b2Vec2&*/var cB = data.positions[this.m_indexB].c;
/*float32*/var aB = data.positions[this.m_indexB].a;
/*b2Rot*/var qA = this.m_qA.setAngle( aA ), qB = this.m_qB.setAngle( aB );
/*float32*/var mA = this.m_invMassA, mB = this.m_invMassB;
/*float32*/var iA = this.m_invIA, iB = this.m_invIB;
// b2Vec2 rA = b2Mul(qA, m_localAnchorA - m_localCenterA);
b2Vec2.subtract( this.m_localAnchorA, this.m_localCenterA, this.m_lalcA );
var rA = b2Rot.timesV2( qA, this.m_lalcA, this.m_rA );
// b2Vec2 rB = b2Mul(qB, m_localAnchorB - m_localCenterB);
b2Vec2.subtract( this.m_localAnchorB, this.m_localCenterB, this.m_lalcB );
var rB = b2Rot.timesV2( qB, this.m_lalcB, this.m_rB );
/*float32*/var positionError, angularError;
/*b2Mat33*/var K = this.m_K;
K.ex.x = mA + mB + rA.y * rA.y * iA + rB.y * rB.y * iB;
K.ey.x = -rA.y * rA.x * iA - rB.y * rB.x * iB;
K.ez.x = -rA.y * iA - rB.y * iB;
K.ex.y = K.ey.x;
K.ey.y = mA + mB + rA.x * rA.x * iA + rB.x * rB.x * iB;
K.ez.y = rA.x * iA + rB.x * iB;
K.ex.z = K.ez.x;
K.ey.z = K.ez.y;
K.ez.z = iA + iB;
if ( this.m_frequencyHz > 0 ) {
// b2Vec2 C1 = cB + rB - cA - rA;
var C1 =
b2Vec2.subtract(
b2Vec2.add( cB, rB, b2Vec2.POOL0 ),
b2Vec2.add( cA, rA, b2Vec2.POOL1 ),
b2WeldJoint._B2VEC2_POOL0 );
positionError = C1.length();
angularError = 0;
// b2Vec2 P = -K.solve22(C1);
var P = K.solve22( C1.x, C1.y, b2WeldJoint._B2VEC2_POOL1 ).negative();
// cA -= mA * P;
cA.minusEqualsMul( mA, P );
aA -= iA * b2Vec2.cross( rA, P );
// cB += mB * P;
cB.plusEqualsMul( mB, P );
aB += iB * b2Vec2.cross( rB, P );
}
else {
// b2Vec2 C1 = cB + rB - cA - rA;
var C1 =
b2Vec2.subtract(
b2Vec2.add( cB, rB, b2Vec2.POOL0 ),
b2Vec2.add( cA, rA, b2Vec2.POOL1 ),
b2WeldJoint._B2VEC2_POOL0 );
/*float32*/var C2 = aB - aA - this.m_referenceAngle;
positionError = C1.length();
angularError = Math.abs( C2 );
// b2Vec3 C(C1.x, C1.y, C2);
// b2Vec3 impulse;
/*b2Vec3*/var impulse = b2WeldJoint._B2VEC3_POOL0;
if ( K.ez.z > 0 ) {
// impulse = -K.solve33(C);
K.solve33( C1.x, C1.y, C2, impulse ).negative();
}
else {
// b2Vec2 impulse2 = -K.solve22(C1);
var impulse2 = K.solve22( C1.x, C1.y, b2WeldJoint._B2VEC2_POOL2 ).negative();
// impulse.set(impulse2.x, impulse2.y, 0.0f);
impulse.x = impulse2.x;
impulse.y = impulse2.y;
impulse.z = 0;
}
// b2Vec2 P(impulse.x, impulse.y);
var P = b2WeldJoint._B2VEC2_POOL1.set( impulse.x, impulse.y );
// cA -= mA * P;
cA.minusEqualsMul( mA, P );
aA -= iA * (b2Vec2.cross( this.m_rA, P ) + impulse.z);
// cB += mB * P;
cB.plusEqualsMul( mB, P );
aB += iB * (b2Vec2.cross( this.m_rB, P ) + impulse.z);
}
// data.positions[this.m_indexA].c = cA;
data.positions[this.m_indexA].a = aA;
// data.positions[this.m_indexB].c = cB;
data.positions[this.m_indexB].a = aB;
return positionError <= b2Settings.b2_linearSlop && angularError <= b2Settings.b2_angularSlop;
};