Search in sources :

Example 31 with Rot

use of org.jbox2d.common.Rot 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 32 with Rot

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

the class PrismaticJoint method initVelocityConstraints.

@Override
public void initVelocityConstraints(final SolverData data) {
    m_indexA = m_bodyA.m_islandIndex;
    m_indexB = m_bodyB.m_islandIndex;
    m_localCenterA.set(m_bodyA.m_sweep.localCenter);
    m_localCenterB.set(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;
    Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Vec2 d = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective masses.
    Rot.mulToOutUnsafe(qA, d.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, d.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    d.set(cB).subLocal(cA).addLocal(rB).subLocal(rA);
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    // Compute motor Jacobian and effective mass.
    {
        Rot.mulToOutUnsafe(qA, m_localXAxisA, m_axis);
        temp.set(d).addLocal(rA);
        m_a1 = Vec2.cross(temp, m_axis);
        m_a2 = Vec2.cross(rB, m_axis);
        m_motorMass = mA + mB + iA * m_a1 * m_a1 + iB * m_a2 * m_a2;
        if (m_motorMass > 0.0f) {
            m_motorMass = 1.0f / m_motorMass;
        }
    }
    // Prismatic constraint.
    {
        Rot.mulToOutUnsafe(qA, m_localYAxisA, m_perp);
        temp.set(d).addLocal(rA);
        m_s1 = Vec2.cross(temp, m_perp);
        m_s2 = Vec2.cross(rB, m_perp);
        float k11 = mA + mB + iA * m_s1 * m_s1 + iB * m_s2 * m_s2;
        float k12 = iA * m_s1 + iB * m_s2;
        float k13 = iA * m_s1 * m_a1 + iB * m_s2 * m_a2;
        float k22 = iA + iB;
        if (k22 == 0.0f) {
            // For bodies with fixed rotation.
            k22 = 1.0f;
        }
        float k23 = iA * m_a1 + iB * m_a2;
        float k33 = mA + mB + iA * m_a1 * m_a1 + iB * m_a2 * m_a2;
        m_K.ex.set(k11, k12, k13);
        m_K.ey.set(k12, k22, k23);
        m_K.ez.set(k13, k23, k33);
    }
    // Compute motor and limit terms.
    if (m_enableLimit) {
        float jointTranslation = Vec2.dot(m_axis, d);
        if (MathUtils.abs(m_upperTranslation - m_lowerTranslation) < 2.0f * Settings.linearSlop) {
            m_limitState = LimitState.EQUAL;
        } else if (jointTranslation <= m_lowerTranslation) {
            if (m_limitState != LimitState.AT_LOWER) {
                m_limitState = LimitState.AT_LOWER;
                m_impulse.z = 0.0f;
            }
        } else if (jointTranslation >= m_upperTranslation) {
            if (m_limitState != LimitState.AT_UPPER) {
                m_limitState = LimitState.AT_UPPER;
                m_impulse.z = 0.0f;
            }
        } else {
            m_limitState = LimitState.INACTIVE;
            m_impulse.z = 0.0f;
        }
    } else {
        m_limitState = LimitState.INACTIVE;
        m_impulse.z = 0.0f;
    }
    if (m_enableMotor == false) {
        m_motorImpulse = 0.0f;
    }
    if (data.step.warmStarting) {
        // Account for variable time step.
        m_impulse.mulLocal(data.step.dtRatio);
        m_motorImpulse *= data.step.dtRatio;
        final Vec2 P = pool.popVec2();
        temp.set(m_axis).mulLocal(m_motorImpulse + m_impulse.z);
        P.set(m_perp).mulLocal(m_impulse.x).addLocal(temp);
        float LA = m_impulse.x * m_s1 + m_impulse.y + (m_motorImpulse + m_impulse.z) * m_a1;
        float LB = m_impulse.x * m_s2 + m_impulse.y + (m_motorImpulse + m_impulse.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(1);
    } else {
        m_impulse.setZero();
        m_motorImpulse = 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.pushRot(2);
    pool.pushVec2(4);
}
Also used : Vec2(org.jbox2d.common.Vec2) Rot(org.jbox2d.common.Rot)

Example 33 with Rot

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

the class PulleyJoint method initVelocityConstraints.

@Override
public void initVelocityConstraints(final SolverData data) {
    m_indexA = m_bodyA.m_islandIndex;
    m_indexB = m_bodyB.m_islandIndex;
    m_localCenterA.set(m_bodyA.m_sweep.localCenter);
    m_localCenterB.set(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;
    Vec2 cA = data.positions[m_indexA].c;
    float aA = data.positions[m_indexA].a;
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    Vec2 cB = data.positions[m_indexB].c;
    float aB = data.positions[m_indexB].a;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    final Vec2 temp = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective masses.
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
    m_uA.set(cA).addLocal(m_rA).subLocal(m_groundAnchorA);
    m_uB.set(cB).addLocal(m_rB).subLocal(m_groundAnchorB);
    float lengthA = m_uA.length();
    float lengthB = m_uB.length();
    if (lengthA > 10f * Settings.linearSlop) {
        m_uA.mulLocal(1.0f / lengthA);
    } else {
        m_uA.setZero();
    }
    if (lengthB > 10f * Settings.linearSlop) {
        m_uB.mulLocal(1.0f / lengthB);
    } else {
        m_uB.setZero();
    }
    // Compute effective mass.
    float ruA = Vec2.cross(m_rA, m_uA);
    float ruB = Vec2.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 Vec2 PA = pool.popVec2();
        final Vec2 PB = pool.popVec2();
        PA.set(m_uA).mulLocal(-m_impulse);
        PB.set(m_uB).mulLocal(-m_ratio * m_impulse);
        vA.x += m_invMassA * PA.x;
        vA.y += m_invMassA * PA.y;
        wA += m_invIA * Vec2.cross(m_rA, PA);
        vB.x += m_invMassB * PB.x;
        vB.y += m_invMassB * PB.y;
        wB += m_invIB * Vec2.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 : Vec2(org.jbox2d.common.Vec2) Rot(org.jbox2d.common.Rot)

Example 34 with Rot

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

the class PulleyJoint 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 uA = pool.popVec2();
    final Vec2 uB = pool.popVec2();
    final Vec2 temp = pool.popVec2();
    final Vec2 PA = pool.popVec2();
    final Vec2 PB = pool.popVec2();
    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);
    Rot.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    uA.set(cA).addLocal(rA).subLocal(m_groundAnchorA);
    uB.set(cB).addLocal(rB).subLocal(m_groundAnchorB);
    float lengthA = uA.length();
    float lengthB = uB.length();
    if (lengthA > 10.0f * Settings.linearSlop) {
        uA.mulLocal(1.0f / lengthA);
    } else {
        uA.setZero();
    }
    if (lengthB > 10.0f * Settings.linearSlop) {
        uB.mulLocal(1.0f / lengthB);
    } else {
        uB.setZero();
    }
    // Compute effective mass.
    float ruA = Vec2.cross(rA, uA);
    float ruB = Vec2.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 = MathUtils.abs(C);
    float impulse = -mass * C;
    PA.set(uA).mulLocal(-impulse);
    PB.set(uB).mulLocal(-m_ratio * impulse);
    cA.x += m_invMassA * PA.x;
    cA.y += m_invMassA * PA.y;
    aA += m_invIA * Vec2.cross(rA, PA);
    cB.x += m_invMassB * PB.x;
    cB.y += m_invMassB * PB.y;
    aB += m_invIB * Vec2.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 : Rot(org.jbox2d.common.Rot) Vec2(org.jbox2d.common.Vec2)

Example 35 with Rot

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

the class RevoluteJoint method solvePositionConstraints.

@Override
public boolean solvePositionConstraints(final SolverData data) {
    final Rot qA = pool.popRot();
    final Rot qB = pool.popRot();
    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 angularError = 0.0f;
    float positionError = 0.0f;
    boolean fixedRotation = (m_invIA + m_invIB == 0.0f);
    // Solve angular limit constraint.
    if (m_enableLimit && m_limitState != LimitState.INACTIVE && fixedRotation == false) {
        float angle = aB - aA - m_referenceAngle;
        float limitImpulse = 0.0f;
        if (m_limitState == LimitState.EQUAL) {
            // Prevent large angular corrections
            float C = MathUtils.clamp(angle - m_lowerAngle, -Settings.maxAngularCorrection, Settings.maxAngularCorrection);
            limitImpulse = -m_motorMass * C;
            angularError = MathUtils.abs(C);
        } else if (m_limitState == LimitState.AT_LOWER) {
            float C = angle - m_lowerAngle;
            angularError = -C;
            // Prevent large angular corrections and allow some slop.
            C = MathUtils.clamp(C + Settings.angularSlop, -Settings.maxAngularCorrection, 0.0f);
            limitImpulse = -m_motorMass * C;
        } else if (m_limitState == LimitState.AT_UPPER) {
            float C = angle - m_upperAngle;
            angularError = C;
            // Prevent large angular corrections and allow some slop.
            C = MathUtils.clamp(C - Settings.angularSlop, 0.0f, Settings.maxAngularCorrection);
            limitImpulse = -m_motorMass * C;
        }
        aA -= m_invIA * limitImpulse;
        aB += m_invIB * limitImpulse;
    }
    // Solve point-to-point constraint.
    {
        qA.set(aA);
        qB.set(aB);
        final Vec2 rA = pool.popVec2();
        final Vec2 rB = pool.popVec2();
        final Vec2 C = pool.popVec2();
        final Vec2 impulse = pool.popVec2();
        Rot.mulToOutUnsafe(qA, C.set(m_localAnchorA).subLocal(m_localCenterA), rA);
        Rot.mulToOutUnsafe(qB, C.set(m_localAnchorB).subLocal(m_localCenterB), rB);
        C.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
        positionError = C.length();
        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 * rA.y * rA.y + iB * rB.y * rB.y;
        K.ex.y = -iA * rA.x * rA.y - iB * rB.x * rB.y;
        K.ey.x = K.ex.y;
        K.ey.y = mA + mB + iA * rA.x * rA.x + iB * rB.x * rB.x;
        K.solveToOut(C, impulse);
        impulse.negateLocal();
        cA.x -= mA * impulse.x;
        cA.y -= mA * impulse.y;
        aA -= iA * Vec2.cross(rA, impulse);
        cB.x += mB * impulse.x;
        cB.y += mB * impulse.y;
        aB += iB * Vec2.cross(rB, impulse);
        pool.pushVec2(4);
        pool.pushMat22(1);
    }
    // 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);
    return positionError <= Settings.linearSlop && angularError <= Settings.angularSlop;
}
Also used : Rot(org.jbox2d.common.Rot) Vec2(org.jbox2d.common.Vec2) Mat22(org.jbox2d.common.Mat22)

Aggregations

Rot (org.jbox2d.common.Rot)37 Vec2 (org.jbox2d.common.Vec2)36 Mat22 (org.jbox2d.common.Mat22)5 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)3 Mat33 (org.jbox2d.common.Mat33)3 VelocityConstraintPoint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)3 Transform (org.jbox2d.common.Transform)2 Vec3 (org.jbox2d.common.Vec3)2 Manifold (org.jbox2d.collision.Manifold)1 WorldManifold (org.jbox2d.collision.WorldManifold)1 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)1