Search in sources :

Example 1 with Vec3

use of org.jbox2d.common.Vec3 in project libgdx by libgdx.

the class RevoluteJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(final SolverData data) {
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    boolean fixedRotation = (iA + iB == 0.0f);
    // Solve motor constraint.
    if (m_enableMotor && m_limitState != LimitState.EQUAL && fixedRotation == false) {
        float Cdot = wB - wA - m_motorSpeed;
        float impulse = -m_motorMass * Cdot;
        float oldImpulse = m_motorImpulse;
        float maxImpulse = data.step.dt * m_maxMotorTorque;
        m_motorImpulse = MathUtils.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
        impulse = m_motorImpulse - oldImpulse;
        wA -= iA * impulse;
        wB += iB * impulse;
    }
    final Vec2 temp = pool.popVec2();
    // Solve limit constraint.
    if (m_enableLimit && m_limitState != LimitState.INACTIVE && fixedRotation == false) {
        final Vec2 Cdot1 = pool.popVec2();
        final Vec3 Cdot = pool.popVec3();
        // Solve point-to-point constraint
        Vec2.crossToOutUnsafe(wA, m_rA, temp);
        Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
        Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
        float Cdot2 = wB - wA;
        Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
        Vec3 impulse = pool.popVec3();
        m_mass.solve33ToOut(Cdot, impulse);
        impulse.negateLocal();
        if (m_limitState == LimitState.EQUAL) {
            m_impulse.addLocal(impulse);
        } else if (m_limitState == LimitState.AT_LOWER) {
            float newImpulse = m_impulse.z + impulse.z;
            if (newImpulse < 0.0f) {
                final Vec2 rhs = pool.popVec2();
                rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
                m_mass.solve22ToOut(rhs, temp);
                impulse.x = temp.x;
                impulse.y = temp.y;
                impulse.z = -m_impulse.z;
                m_impulse.x += temp.x;
                m_impulse.y += temp.y;
                m_impulse.z = 0.0f;
                pool.pushVec2(1);
            } else {
                m_impulse.addLocal(impulse);
            }
        } else if (m_limitState == LimitState.AT_UPPER) {
            float newImpulse = m_impulse.z + impulse.z;
            if (newImpulse > 0.0f) {
                final Vec2 rhs = pool.popVec2();
                rhs.set(m_mass.ez.x, m_mass.ez.y).mulLocal(m_impulse.z).subLocal(Cdot1);
                m_mass.solve22ToOut(rhs, temp);
                impulse.x = temp.x;
                impulse.y = temp.y;
                impulse.z = -m_impulse.z;
                m_impulse.x += temp.x;
                m_impulse.y += temp.y;
                m_impulse.z = 0.0f;
                pool.pushVec2(1);
            } else {
                m_impulse.addLocal(impulse);
            }
        }
        final Vec2 P = pool.popVec2();
        P.set(impulse.x, impulse.y);
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * (Vec2.cross(m_rA, P) + impulse.z);
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * (Vec2.cross(m_rB, P) + impulse.z);
        pool.pushVec2(2);
        pool.pushVec3(2);
    } else {
        // Solve point-to-point constraint
        Vec2 Cdot = pool.popVec2();
        Vec2 impulse = pool.popVec2();
        Vec2.crossToOutUnsafe(wA, m_rA, temp);
        Vec2.crossToOutUnsafe(wB, m_rB, Cdot);
        Cdot.addLocal(vB).subLocal(vA).subLocal(temp);
        // just leave negated
        m_mass.solve22ToOut(Cdot.negateLocal(), impulse);
        m_impulse.x += impulse.x;
        m_impulse.y += impulse.y;
        vA.x -= mA * impulse.x;
        vA.y -= mA * impulse.y;
        wA -= iA * Vec2.cross(m_rA, impulse);
        vB.x += mB * impulse.x;
        vB.y += mB * impulse.y;
        wB += iB * Vec2.cross(m_rB, impulse);
        pool.pushVec2(2);
    }
    // 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);
}
Also used : Vec2(org.jbox2d.common.Vec2) Vec3(org.jbox2d.common.Vec3)

Example 2 with Vec3

use of org.jbox2d.common.Vec3 in project libgdx by libgdx.

the class WeldJoint method solvePositionConstraints.

@Override
public boolean solvePositionConstraints(final SolverData data) {
    Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Vec2 temp = pool.popVec2();
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    float positionError, angularError;
    final Mat33 K = pool.popMat33();
    final Vec2 C1 = pool.popVec2();
    final Vec2 P = pool.popVec2();
    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 (m_frequencyHz > 0.0f) {
        C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
        positionError = C1.length();
        angularError = 0.0f;
        K.solve22ToOut(C1, P);
        P.negateLocal();
        cA.x -= mA * P.x;
        cA.y -= mA * P.y;
        aA -= iA * Vec2.cross(rA, P);
        cB.x += mB * P.x;
        cB.y += mB * P.y;
        aB += iB * Vec2.cross(rB, P);
    } else {
        C1.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
        float C2 = aB - aA - m_referenceAngle;
        positionError = C1.length();
        angularError = MathUtils.abs(C2);
        final Vec3 C = pool.popVec3();
        final Vec3 impulse = pool.popVec3();
        C.set(C1.x, C1.y, C2);
        K.solve33ToOut(C, impulse);
        impulse.negateLocal();
        P.set(impulse.x, impulse.y);
        cA.x -= mA * P.x;
        cA.y -= mA * P.y;
        aA -= iA * (Vec2.cross(rA, P) + impulse.z);
        cB.x += mB * P.x;
        cB.y += mB * P.y;
        aB += iB * (Vec2.cross(rB, P) + impulse.z);
        pool.pushVec3(2);
    }
    //    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.pushVec2(5);
    pool.pushRot(2);
    pool.pushMat33(1);
    return positionError <= Settings.linearSlop && angularError <= Settings.angularSlop;
}
Also used : Vec2(org.jbox2d.common.Vec2) Rot(org.jbox2d.common.Rot) Vec3(org.jbox2d.common.Vec3) Mat33(org.jbox2d.common.Mat33)

Example 3 with Vec3

use of org.jbox2d.common.Vec3 in project libgdx by libgdx.

the class PrismaticJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(final SolverData data) {
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    final Vec2 temp = pool.popVec2();
    // Solve linear motor constraint.
    if (m_enableMotor && m_limitState != LimitState.EQUAL) {
        temp.set(vB).subLocal(vA);
        float Cdot = Vec2.dot(m_axis, temp) + m_a2 * wB - m_a1 * wA;
        float impulse = m_motorMass * (m_motorSpeed - Cdot);
        float oldImpulse = m_motorImpulse;
        float maxImpulse = data.step.dt * m_maxMotorForce;
        m_motorImpulse = MathUtils.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
        impulse = m_motorImpulse - oldImpulse;
        final Vec2 P = pool.popVec2();
        P.set(m_axis).mulLocal(impulse);
        float LA = impulse * m_a1;
        float LB = impulse * m_a2;
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * LA;
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * LB;
        pool.pushVec2(1);
    }
    final Vec2 Cdot1 = pool.popVec2();
    temp.set(vB).subLocal(vA);
    Cdot1.x = Vec2.dot(m_perp, temp) + m_s2 * wB - m_s1 * wA;
    Cdot1.y = wB - wA;
    if (m_enableLimit && m_limitState != LimitState.INACTIVE) {
        // Solve prismatic and limit constraint in block form.
        float Cdot2;
        temp.set(vB).subLocal(vA);
        Cdot2 = Vec2.dot(m_axis, temp) + m_a2 * wB - m_a1 * wA;
        final Vec3 Cdot = pool.popVec3();
        Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
        final Vec3 f1 = pool.popVec3();
        final Vec3 df = pool.popVec3();
        f1.set(m_impulse);
        m_K.solve33ToOut(Cdot.negateLocal(), df);
        // Cdot.negateLocal(); not used anymore
        m_impulse.addLocal(df);
        if (m_limitState == LimitState.AT_LOWER) {
            m_impulse.z = MathUtils.max(m_impulse.z, 0.0f);
        } else if (m_limitState == LimitState.AT_UPPER) {
            m_impulse.z = MathUtils.min(m_impulse.z, 0.0f);
        }
        // f2(1:2) = invK(1:2,1:2) * (-Cdot(1:2) - K(1:2,3) * (f2(3) - f1(3))) +
        // f1(1:2)
        final Vec2 b = pool.popVec2();
        final Vec2 f2r = pool.popVec2();
        temp.set(m_K.ez.x, m_K.ez.y).mulLocal(m_impulse.z - f1.z);
        b.set(Cdot1).negateLocal().subLocal(temp);
        m_K.solve22ToOut(b, f2r);
        f2r.addLocal(f1.x, f1.y);
        m_impulse.x = f2r.x;
        m_impulse.y = f2r.y;
        df.set(m_impulse).subLocal(f1);
        final Vec2 P = pool.popVec2();
        temp.set(m_axis).mulLocal(df.z);
        P.set(m_perp).mulLocal(df.x).addLocal(temp);
        float LA = df.x * m_s1 + df.y + df.z * m_a1;
        float LB = df.x * m_s2 + df.y + df.z * m_a2;
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * LA;
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * LB;
        pool.pushVec2(3);
        pool.pushVec3(3);
    } else {
        // Limit is inactive, just solve the prismatic constraint in block form.
        final Vec2 df = pool.popVec2();
        m_K.solve22ToOut(Cdot1.negateLocal(), df);
        Cdot1.negateLocal();
        m_impulse.x += df.x;
        m_impulse.y += df.y;
        final Vec2 P = pool.popVec2();
        P.set(m_perp).mulLocal(df.x);
        float LA = df.x * m_s1 + df.y;
        float LB = df.x * m_s2 + df.y;
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * LA;
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * LB;
        pool.pushVec2(2);
    }
    // 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(2);
}
Also used : Vec2(org.jbox2d.common.Vec2) Vec3(org.jbox2d.common.Vec3)

Example 4 with Vec3

use of org.jbox2d.common.Vec3 in project libgdx by libgdx.

the class PrismaticJoint method solvePositionConstraints.

@Override
public boolean solvePositionConstraints(final SolverData data) {
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    final Vec2 d = pool.popVec2();
    final Vec2 axis = pool.popVec2();
    final Vec2 perp = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    final Vec2 C1 = pool.popVec2();
    final Vec3 impulse = pool.popVec3();
    Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    qA.set(aA);
    qB.set(aB);
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    // Compute fresh Jacobians
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    d.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
    Rot.mulToOutUnsafe(qA, m_localXAxisA, axis);
    float a1 = Vec2.cross(temp.set(d).addLocal(rA), axis);
    float a2 = Vec2.cross(rB, axis);
    Rot.mulToOutUnsafe(qA, m_localYAxisA, perp);
    float s1 = Vec2.cross(temp.set(d).addLocal(rA), perp);
    float s2 = Vec2.cross(rB, perp);
    C1.x = Vec2.dot(perp, d);
    C1.y = aB - aA - m_referenceAngle;
    float linearError = MathUtils.abs(C1.x);
    float angularError = MathUtils.abs(C1.y);
    boolean active = false;
    float C2 = 0.0f;
    if (m_enableLimit) {
        float translation = Vec2.dot(axis, d);
        if (MathUtils.abs(m_upperTranslation - m_lowerTranslation) < 2.0f * Settings.linearSlop) {
            // Prevent large angular corrections
            C2 = MathUtils.clamp(translation, -Settings.maxLinearCorrection, Settings.maxLinearCorrection);
            linearError = MathUtils.max(linearError, MathUtils.abs(translation));
            active = true;
        } else if (translation <= m_lowerTranslation) {
            // Prevent large linear corrections and allow some slop.
            C2 = MathUtils.clamp(translation - m_lowerTranslation + Settings.linearSlop, -Settings.maxLinearCorrection, 0.0f);
            linearError = MathUtils.max(linearError, m_lowerTranslation - translation);
            active = true;
        } else if (translation >= m_upperTranslation) {
            // Prevent large linear corrections and allow some slop.
            C2 = MathUtils.clamp(translation - m_upperTranslation - Settings.linearSlop, 0.0f, Settings.maxLinearCorrection);
            linearError = MathUtils.max(linearError, translation - m_upperTranslation);
            active = true;
        }
    }
    if (active) {
        float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
        float k12 = iA * s1 + iB * s2;
        float k13 = iA * s1 * a1 + iB * s2 * a2;
        float k22 = iA + iB;
        if (k22 == 0.0f) {
            // For fixed rotation
            k22 = 1.0f;
        }
        float k23 = iA * a1 + iB * a2;
        float k33 = mA + mB + iA * a1 * a1 + iB * a2 * a2;
        final Mat33 K = pool.popMat33();
        K.ex.set(k11, k12, k13);
        K.ey.set(k12, k22, k23);
        K.ez.set(k13, k23, k33);
        final Vec3 C = pool.popVec3();
        C.x = C1.x;
        C.y = C1.y;
        C.z = C2;
        K.solve33ToOut(C.negateLocal(), impulse);
        pool.pushVec3(1);
        pool.pushMat33(1);
    } else {
        float k11 = mA + mB + iA * s1 * s1 + iB * s2 * s2;
        float k12 = iA * s1 + iB * s2;
        float k22 = iA + iB;
        if (k22 == 0.0f) {
            k22 = 1.0f;
        }
        final Mat22 K = pool.popMat22();
        K.ex.set(k11, k12);
        K.ey.set(k12, k22);
        // temp is impulse1
        K.solveToOut(C1.negateLocal(), temp);
        C1.negateLocal();
        impulse.x = temp.x;
        impulse.y = temp.y;
        impulse.z = 0.0f;
        pool.pushMat22(1);
    }
    float Px = impulse.x * perp.x + impulse.z * axis.x;
    float Py = impulse.x * perp.y + impulse.z * axis.y;
    float LA = impulse.x * s1 + impulse.y + impulse.z * a1;
    float LB = impulse.x * s2 + impulse.y + impulse.z * a2;
    cA.x -= mA * Px;
    cA.y -= mA * Py;
    aA -= iA * LA;
    cB.x += mB * Px;
    cB.y += mB * Py;
    aB += iB * LB;
    // 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.pushVec2(7);
    pool.pushVec3(1);
    pool.pushRot(2);
    return linearError <= Settings.linearSlop && angularError <= Settings.angularSlop;
}
Also used : Rot(org.jbox2d.common.Rot) Vec2(org.jbox2d.common.Vec2) Vec3(org.jbox2d.common.Vec3) Mat33(org.jbox2d.common.Mat33) Mat22(org.jbox2d.common.Mat22)

Example 5 with Vec3

use of org.jbox2d.common.Vec3 in project libgdx by libgdx.

the class WeldJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(final SolverData data) {
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    final Vec2 Cdot1 = pool.popVec2();
    final Vec2 P = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    if (m_frequencyHz > 0.0f) {
        float Cdot2 = wB - wA;
        float impulse2 = -m_mass.ez.z * (Cdot2 + m_bias + m_gamma * m_impulse.z);
        m_impulse.z += impulse2;
        wA -= iA * impulse2;
        wB += iB * impulse2;
        Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
        Vec2.crossToOutUnsafe(wA, m_rA, temp);
        Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
        final Vec2 impulse1 = P;
        Mat33.mul22ToOutUnsafe(m_mass, Cdot1, impulse1);
        impulse1.negateLocal();
        m_impulse.x += impulse1.x;
        m_impulse.y += impulse1.y;
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * Vec2.cross(m_rA, P);
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * Vec2.cross(m_rB, P);
    } else {
        Vec2.crossToOutUnsafe(wA, m_rA, temp);
        Vec2.crossToOutUnsafe(wB, m_rB, Cdot1);
        Cdot1.addLocal(vB).subLocal(vA).subLocal(temp);
        float Cdot2 = wB - wA;
        final Vec3 Cdot = pool.popVec3();
        Cdot.set(Cdot1.x, Cdot1.y, Cdot2);
        final Vec3 impulse = pool.popVec3();
        Mat33.mulToOutUnsafe(m_mass, Cdot, impulse);
        impulse.negateLocal();
        m_impulse.addLocal(impulse);
        P.set(impulse.x, impulse.y);
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * (Vec2.cross(m_rA, P) + impulse.z);
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * (Vec2.cross(m_rB, P) + impulse.z);
        pool.pushVec3(2);
    }
    //    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(3);
}
Also used : Vec2(org.jbox2d.common.Vec2) Vec3(org.jbox2d.common.Vec3)

Aggregations

Vec2 (org.jbox2d.common.Vec2)5 Vec3 (org.jbox2d.common.Vec3)5 Mat33 (org.jbox2d.common.Mat33)2 Rot (org.jbox2d.common.Rot)2 Mat22 (org.jbox2d.common.Mat22)1