Search in sources :

Example 21 with Rot

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

the class GearJoint method initVelocityConstraints.

@Override
public void initVelocityConstraints(SolverData data) {
    m_indexA = A.island;
    m_indexB = B.island;
    m_indexC = m_bodyC.island;
    m_indexD = m_bodyD.island;
    m_lcA.set(A.sweep.localCenter);
    m_lcB.set(B.sweep.localCenter);
    m_lcC.set(m_bodyC.sweep.localCenter);
    m_lcD.set(m_bodyD.sweep.localCenter);
    m_mA = A.m_invMass;
    m_mB = B.m_invMass;
    m_mC = m_bodyC.m_invMass;
    m_mD = m_bodyD.m_invMass;
    m_iA = A.m_invI;
    m_iB = B.m_invI;
    m_iC = m_bodyC.m_invI;
    m_iD = m_bodyD.m_invI;
    // Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Tuple2f vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    // Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    Tuple2f vB = data.velocities[m_indexB];
    float wB = data.velocities[m_indexB].w;
    // Vec2 cC = data.positions[m_indexC].c;
    float aC = data.positions[m_indexC].a;
    Tuple2f vC = data.velocities[m_indexC];
    float wC = data.velocities[m_indexC].w;
    // Vec2 cD = data.positions[m_indexD].c;
    float aD = data.positions[m_indexD].a;
    Tuple2f vD = data.velocities[m_indexD];
    float wD = data.velocities[m_indexD].w;
    Rot qA = pool.popRot(), qB = pool.popRot(), qC = pool.popRot(), qD = pool.popRot();
    qA.set(aA);
    qB.set(aB);
    qC.set(aC);
    qD.set(aD);
    m_mass = 0.0f;
    Tuple2f temp = pool.popVec2();
    if (m_typeA == JointType.REVOLUTE) {
        m_JvAC.setZero();
        m_JwA = 1.0f;
        m_JwC = 1.0f;
        m_mass += m_iA + m_iC;
    } else {
        Tuple2f rC = pool.popVec2();
        Tuple2f rA = pool.popVec2();
        Rot.mulToOutUnsafe(qC, m_localAxisC, m_JvAC);
        Rot.mulToOutUnsafe(qC, temp.set(m_localAnchorC).subbed(m_lcC), rC);
        Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_lcA), rA);
        m_JwC = Tuple2f.cross(rC, m_JvAC);
        m_JwA = Tuple2f.cross(rA, m_JvAC);
        m_mass += m_mC + m_mA + m_iC * m_JwC * m_JwC + m_iA * m_JwA * m_JwA;
        pool.pushVec2(2);
    }
    if (m_typeB == JointType.REVOLUTE) {
        m_JvBD.setZero();
        m_JwB = m_ratio;
        m_JwD = m_ratio;
        m_mass += m_ratio * m_ratio * (m_iB + m_iD);
    } else {
        Tuple2f u = pool.popVec2();
        Tuple2f rD = pool.popVec2();
        Tuple2f rB = pool.popVec2();
        Rot.mulToOutUnsafe(qD, m_localAxisD, u);
        Rot.mulToOutUnsafe(qD, temp.set(m_localAnchorD).subbed(m_lcD), rD);
        Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_lcB), rB);
        m_JvBD.set(u).scaled(m_ratio);
        m_JwD = m_ratio * Tuple2f.cross(rD, u);
        m_JwB = m_ratio * Tuple2f.cross(rB, u);
        m_mass += m_ratio * m_ratio * (m_mD + m_mB) + m_iD * m_JwD * m_JwD + m_iB * m_JwB * m_JwB;
        pool.pushVec2(3);
    }
    // Compute effective mass.
    m_mass = m_mass > 0.0f ? 1.0f / m_mass : 0.0f;
    if (data.step.warmStarting) {
        vA.x += (m_mA * m_impulse) * m_JvAC.x;
        vA.y += (m_mA * m_impulse) * m_JvAC.y;
        wA += m_iA * m_impulse * m_JwA;
        vB.x += (m_mB * m_impulse) * m_JvBD.x;
        vB.y += (m_mB * m_impulse) * m_JvBD.y;
        wB += m_iB * m_impulse * m_JwB;
        vC.x -= (m_mC * m_impulse) * m_JvAC.x;
        vC.y -= (m_mC * m_impulse) * m_JvAC.y;
        wC -= m_iC * m_impulse * m_JwC;
        vD.x -= (m_mD * m_impulse) * m_JvBD.x;
        vD.y -= (m_mD * m_impulse) * m_JvBD.y;
        wD -= m_iD * m_impulse * m_JwD;
    } else {
        m_impulse = 0.0f;
    }
    pool.pushVec2(1);
    pool.pushRot(4);
    // data.velocities[m_indexA].v = vA;
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v = vB;
    data.velocities[m_indexB].w = wB;
    // data.velocities[m_indexC].v = vC;
    data.velocities[m_indexC].w = wC;
    // data.velocities[m_indexD].v = vD;
    data.velocities[m_indexD].w = wD;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 22 with Rot

use of spacegraph.space2d.phys.common.Rot 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)

Example 23 with Rot

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

the class PulleyJoint method solvePositionConstraints.

@Override
public boolean solvePositionConstraints(final SolverData data) {
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Tuple2f rA = pool.popVec2();
    final Tuple2f rB = pool.popVec2();
    final Tuple2f uA = pool.popVec2();
    final Tuple2f uB = pool.popVec2();
    final Tuple2f temp = pool.popVec2();
    final Tuple2f PA = pool.popVec2();
    final Tuple2f PB = pool.popVec2();
    Tuple2f cA = data.positions[m_indexA];
    float aA = data.positions[m_indexA].a;
    Tuple2f cB = data.positions[m_indexB];
    float aB = data.positions[m_indexB].a;
    qA.set(aA);
    qB.set(aB);
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_localCenterB), rB);
    uA.set(cA).added(rA).subbed(m_groundAnchorA);
    uB.set(cB).added(rB).subbed(m_groundAnchorB);
    float lengthA = uA.length();
    float lengthB = uB.length();
    if (lengthA > 10.0f * Settings.linearSlop) {
        uA.scaled(1.0f / lengthA);
    } else {
        uA.setZero();
    }
    if (lengthB > 10.0f * Settings.linearSlop) {
        uB.scaled(1.0f / lengthB);
    } else {
        uB.setZero();
    }
    // Compute effective mass.
    float ruA = Tuple2f.cross(rA, uA);
    float ruB = Tuple2f.cross(rB, uB);
    float mA = m_invMassA + m_invIA * ruA * ruA;
    float mB = m_invMassB + m_invIB * ruB * ruB;
    float mass = mA + m_ratio * m_ratio * mB;
    if (mass > 0.0f) {
        mass = 1.0f / mass;
    }
    float C = m_constant - lengthA - m_ratio * lengthB;
    float linearError = Math.abs(C);
    float impulse = -mass * C;
    PA.set(uA).scaled(-impulse);
    PB.set(uB).scaled(-m_ratio * impulse);
    cA.x += m_invMassA * PA.x;
    cA.y += m_invMassA * PA.y;
    aA += m_invIA * Tuple2f.cross(rA, PA);
    cB.x += m_invMassB * PB.x;
    cB.y += m_invMassB * PB.y;
    aB += m_invIB * Tuple2f.cross(rB, PB);
    // data.positions[m_indexA].c.set(cA);
    data.positions[m_indexA].a = aA;
    // data.positions[m_indexB].c.set(cB);
    data.positions[m_indexB].a = aB;
    pool.pushRot(2);
    pool.pushVec2(7);
    return linearError < Settings.linearSlop;
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 24 with Rot

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

the class PulleyJoint method initVelocityConstraints.

@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;
    Tuple2f cA = data.positions[m_indexA];
    float aA = data.positions[m_indexA].a;
    Tuple2f vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    Tuple2f cB = data.positions[m_indexB];
    float aB = data.positions[m_indexB].a;
    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 = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective masses.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_localCenterA), m_rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_localCenterB), m_rB);
    m_uA.set(cA).added(m_rA).subbed(m_groundAnchorA);
    m_uB.set(cB).added(m_rB).subbed(m_groundAnchorB);
    float lengthA = m_uA.length();
    float lengthB = m_uB.length();
    if (lengthA > 10f * Settings.linearSlop) {
        m_uA.scaled(1.0f / lengthA);
    } else {
        m_uA.setZero();
    }
    if (lengthB > 10f * Settings.linearSlop) {
        m_uB.scaled(1.0f / lengthB);
    } else {
        m_uB.setZero();
    }
    // Compute effective mass.
    float ruA = Tuple2f.cross(m_rA, m_uA);
    float ruB = Tuple2f.cross(m_rB, m_uB);
    float mA = m_invMassA + m_invIA * ruA * ruA;
    float mB = m_invMassB + m_invIB * ruB * ruB;
    m_mass = mA + m_ratio * m_ratio * mB;
    if (m_mass > 0.0f) {
        m_mass = 1.0f / m_mass;
    }
    if (data.step.warmStarting) {
        // Scale impulses to support variable time steps.
        m_impulse *= data.step.dtRatio;
        // Warm starting.
        final Tuple2f PA = pool.popVec2();
        final Tuple2f PB = pool.popVec2();
        PA.set(m_uA).scaled(-m_impulse);
        PB.set(m_uB).scaled(-m_ratio * m_impulse);
        vA.x += m_invMassA * PA.x;
        vA.y += m_invMassA * PA.y;
        wA += m_invIA * Tuple2f.cross(m_rA, PA);
        vB.x += m_invMassB * PB.x;
        vB.y += m_invMassB * PB.y;
        wB += m_invIB * Tuple2f.cross(m_rB, PB);
        pool.pushVec2(2);
    } else {
        m_impulse = 0.0f;
    }
    // data.velocities[m_indexA].v.set(vA);
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    pool.pushVec2(1);
    pool.pushRot(2);
}
Also used : Tuple2f(spacegraph.util.math.Tuple2f) Rot(spacegraph.space2d.phys.common.Rot)

Example 25 with Rot

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

the class WheelJoint 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;
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    Tuple2f cA = data.positions[m_indexA];
    float aA = data.positions[m_indexA].a;
    Tuple2f vA = data.velocities[m_indexA];
    float wA = data.velocities[m_indexA].w;
    Tuple2f cB = data.positions[m_indexB];
    float aB = data.positions[m_indexB].a;
    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 = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective masses.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subbed(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subbed(m_localCenterB), rB);
    d.set(cB).added(rB).subbed(cA).subbed(rA);
    // Point to line constraint
    {
        Rot.mulToOut(qA, m_localYAxisA, m_ay);
        m_sAy = Tuple2f.cross(temp.set(d).added(rA), m_ay);
        m_sBy = Tuple2f.cross(rB, m_ay);
        m_mass = mA + mB + iA * m_sAy * m_sAy + iB * m_sBy * m_sBy;
        if (m_mass > 0.0f) {
            m_mass = 1.0f / m_mass;
        }
    }
    // Spring constraint
    m_springMass = 0.0f;
    m_bias = 0.0f;
    m_gamma = 0.0f;
    if (m_frequencyHz > 0.0f) {
        Rot.mulToOut(qA, m_localXAxisA, m_ax);
        m_sAx = Tuple2f.cross(temp.set(d).added(rA), m_ax);
        m_sBx = Tuple2f.cross(rB, m_ax);
        float invMass = mA + mB + iA * m_sAx * m_sAx + iB * m_sBx * m_sBx;
        if (invMass > 0.0f) {
            m_springMass = 1.0f / invMass;
            float C = Tuple2f.dot(d, m_ax);
            // Frequency
            float omega = 2.0f * MathUtils.PI * m_frequencyHz;
            // Damping coefficient
            float d = 2.0f * m_springMass * m_dampingRatio * omega;
            // Spring stiffness
            float k = m_springMass * omega * omega;
            // magic formulas
            float h = data.step.dt;
            m_gamma = h * (d + h * k);
            if (m_gamma > 0.0f) {
                m_gamma = 1.0f / m_gamma;
            }
            m_bias = C * h * k * m_gamma;
            m_springMass = invMass + m_gamma;
            if (m_springMass > 0.0f) {
                m_springMass = 1.0f / m_springMass;
            }
        }
    } else {
        m_springImpulse = 0.0f;
    }
    // Rotational motor
    if (m_enableMotor) {
        m_motorMass = iA + iB;
        if (m_motorMass > 0.0f) {
            m_motorMass = 1.0f / m_motorMass;
        }
    } else {
        m_motorMass = 0.0f;
        m_motorImpulse = 0.0f;
    }
    if (data.step.warmStarting) {
        final Tuple2f P = pool.popVec2();
        // Account for variable time step.
        m_impulse *= data.step.dtRatio;
        m_springImpulse *= data.step.dtRatio;
        m_motorImpulse *= data.step.dtRatio;
        P.x = m_impulse * m_ay.x + m_springImpulse * m_ax.x;
        P.y = m_impulse * m_ay.y + m_springImpulse * m_ax.y;
        float LA = m_impulse * m_sAy + m_springImpulse * m_sAx + m_motorImpulse;
        float LB = m_impulse * m_sBy + m_springImpulse * m_sBx + m_motorImpulse;
        vA.x -= m_invMassA * P.x;
        vA.y -= m_invMassA * P.y;
        wA -= m_invIA * LA;
        vB.x += m_invMassB * P.x;
        vB.y += m_invMassB * P.y;
        wB += m_invIB * LB;
        pool.pushVec2(1);
    } else {
        m_impulse = 0.0f;
        m_springImpulse = 0.0f;
        m_motorImpulse = 0.0f;
    }
    pool.pushRot(2);
    pool.pushVec2(1);
    // 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)

Aggregations

Rot (spacegraph.space2d.phys.common.Rot)25 Tuple2f (spacegraph.util.math.Tuple2f)23 spacegraph.util.math.v2 (spacegraph.util.math.v2)3 Mat22 (spacegraph.space2d.phys.common.Mat22)2 Transform (spacegraph.space2d.phys.common.Transform)2 PolygonShape (spacegraph.space2d.phys.collision.shapes.PolygonShape)1 PolygonFixture (spacegraph.space2d.phys.fracture.PolygonFixture)1