Search in sources :

Example 1 with Mat22

use of spacegraph.space2d.phys.common.Mat22 in project narchy by automenta.

the class FrictionJoint method initVelocityConstraints.

/**
 * @see Joint#initVelocityConstraints(org.jbox2d.dynamics.TimeStep)
 */
@Override
public void initVelocityConstraints(final SolverData data) {
    m_indexA = A.island;
    m_indexB = B.island;
    m_localCenterA.set(A.sweep.localCenter);
    m_localCenterB.set(B.sweep.localCenter);
    m_invMassA = A.m_invMass;
    m_invMassB = B.m_invMass;
    m_invIA = A.m_invI;
    m_invIB = B.m_invI;
    float aA = data.positions[m_indexA].a;
    v2 vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    float aB = data.positions[m_indexB].a;
    v2 vB = data.velocities[m_indexB];
    float wB = data.velocities[m_indexB].w;
    final Tuple2f temp = pool.popVec2();
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective mass matrix.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_localCenterA), m_rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_localCenterB), 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]
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    final Mat22 K = pool.popMat22();
    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;
    K.invertToOut(m_linearMass);
    m_angularMass = iA + iB;
    if (m_angularMass > 0.0f) {
        m_angularMass = 1.0f / m_angularMass;
    }
    if (data.step.warmStarting) {
        // Scale impulses to support a variable time step.
        m_linearImpulse.scaled(data.step.dtRatio);
        m_angularImpulse *= data.step.dtRatio;
        final Tuple2f P = pool.popVec2();
        P.set(m_linearImpulse);
        temp.set(P).scaled(mA);
        vA.subbed(temp);
        wA -= iA * (Tuple2f.cross(m_rA, P) + m_angularImpulse);
        temp.set(P).scaled(mB);
        vB.added(temp);
        wB += iB * (Tuple2f.cross(m_rB, P) + m_angularImpulse);
        pool.pushVec2(1);
    } else {
        m_linearImpulse.setZero();
        m_angularImpulse = 0.0f;
    }
    // data.velocities[m_indexA].v.set(vA);
    assert !(data.velocities[m_indexA].w != wA) || (data.velocities[m_indexA].w != wA);
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    pool.pushRot(2);
    pool.pushVec2(1);
    pool.pushMat22(1);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot) Mat22(spacegraph.space2d.phys.common.Mat22) spacegraph.util.math.v2(spacegraph.util.math.v2)

Example 2 with Mat22

use of spacegraph.space2d.phys.common.Mat22 in project narchy by automenta.

the class MotorJoint method initVelocityConstraints.

@Override
public void initVelocityConstraints(SolverData data) {
    m_indexA = A.island;
    m_indexB = B.island;
    m_localCenterA.set(A.sweep.localCenter);
    m_localCenterB.set(B.sweep.localCenter);
    m_invMassA = A.m_invMass;
    m_invMassB = B.m_invMass;
    m_invIA = A.m_invI;
    m_invIB = B.m_invI;
    final Tuple2f cA = data.positions[m_indexA];
    float aA = data.positions[m_indexA].a;
    final Tuple2f vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    final Tuple2f cB = data.positions[m_indexB];
    float aB = data.positions[m_indexB].a;
    final Tuple2f vB = data.velocities[m_indexB];
    float wB = data.velocities[m_indexB].w;
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Tuple2f temp = new v2();
    Mat22 K = pool.popMat22();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective mass matrix.
    // m_rA = b2Mul(qA, -m_localCenterA);
    // m_rB = b2Mul(qB, -m_localCenterB);
    m_rA.x = qA.c * -m_localCenterA.x - qA.s * -m_localCenterA.y;
    m_rA.y = qA.s * -m_localCenterA.x + qA.c * -m_localCenterA.y;
    m_rB.x = qB.c * -m_localCenterB.x - qB.s * -m_localCenterB.y;
    m_rB.y = qB.s * -m_localCenterB.x + qB.c * -m_localCenterB.y;
    // 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]
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    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;
    K.invertToOut(m_linearMass);
    m_angularMass = iA + iB;
    if (m_angularMass > 0.0f) {
        m_angularMass = 1.0f / m_angularMass;
    }
    // m_linearError = cB + m_rB - cA - m_rA - b2Mul(qA, m_linearOffset);
    Rot.mulToOutUnsafe(qA, m_linearOffset, temp);
    m_linearError.x = cB.x + m_rB.x - cA.x - m_rA.x - temp.x;
    m_linearError.y = cB.y + m_rB.y - cA.y - m_rA.y - temp.y;
    m_angularError = aB - aA - m_angularOffset;
    if (data.step.warmStarting) {
        // Scale impulses to support a variable time step.
        m_linearImpulse.x *= data.step.dtRatio;
        m_linearImpulse.y *= data.step.dtRatio;
        m_angularImpulse *= data.step.dtRatio;
        final Tuple2f P = m_linearImpulse;
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * (m_rA.x * P.y - m_rA.y * P.x + m_angularImpulse);
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * (m_rB.x * P.y - m_rB.y * P.x + m_angularImpulse);
    } else {
        m_linearImpulse.setZero();
        m_angularImpulse = 0.0f;
    }
    pool.pushMat22(1);
    pool.pushRot(2);
    // data.velocities[m_indexA].v = vA;
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v = vB;
    data.velocities[m_indexB].w = wB;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot) Mat22(spacegraph.space2d.phys.common.Mat22) spacegraph.util.math.v2(spacegraph.util.math.v2)

Aggregations

Mat22 (spacegraph.space2d.phys.common.Mat22)2 Rot (spacegraph.space2d.phys.common.Rot)2 Tuple2f (spacegraph.util.math.Tuple2f)2 spacegraph.util.math.v2 (spacegraph.util.math.v2)2