Search in sources :

Example 36 with Vec2

use of org.jbox2d.common.Vec2 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 37 with Vec2

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

the class RopeJoint 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 u = pool.popVec2();
    final Vec2 rA = pool.popVec2();
    final Vec2 rB = pool.popVec2();
    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), rA);
    Rot.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    u.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
    float length = u.normalize();
    float C = length - m_maxLength;
    C = MathUtils.clamp(C, 0.0f, Settings.maxLinearCorrection);
    float impulse = -m_mass * C;
    float Px = impulse * u.x;
    float Py = impulse * u.y;
    cA.x -= m_invMassA * Px;
    cA.y -= m_invMassA * Py;
    aA -= m_invIA * (rA.x * Py - rA.y * Px);
    cB.x += m_invMassB * Px;
    cB.y += m_invMassB * Py;
    aB += m_invIB * (rB.x * Py - rB.y * Px);
    pool.pushRot(2);
    pool.pushVec2(4);
    // data.positions[m_indexA].c = cA;
    data.positions[m_indexA].a = aA;
    // data.positions[m_indexB].c = cB;
    data.positions[m_indexB].a = aB;
    return length - m_maxLength < Settings.linearSlop;
}
Also used : Vec2(org.jbox2d.common.Vec2) Rot(org.jbox2d.common.Rot)

Example 38 with Vec2

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

the class RopeJoint 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_u.set(cB).addLocal(m_rB).subLocal(cA).subLocal(m_rA);
    m_length = m_u.length();
    float C = m_length - m_maxLength;
    if (C > 0.0f) {
        m_state = LimitState.AT_UPPER;
    } else {
        m_state = LimitState.INACTIVE;
    }
    if (m_length > Settings.linearSlop) {
        m_u.mulLocal(1.0f / m_length);
    } else {
        m_u.setZero();
        m_mass = 0.0f;
        m_impulse = 0.0f;
        return;
    }
    // Compute effective mass.
    float crA = Vec2.cross(m_rA, m_u);
    float crB = Vec2.cross(m_rB, m_u);
    float invMass = m_invMassA + m_invIA * crA * crA + m_invMassB + m_invIB * crB * crB;
    m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
    if (data.step.warmStarting) {
        // Scale the impulse to support a variable time step.
        m_impulse *= data.step.dtRatio;
        float Px = m_impulse * m_u.x;
        float Py = m_impulse * m_u.y;
        vA.x -= m_invMassA * Px;
        vA.y -= m_invMassA * Py;
        wA -= m_invIA * (m_rA.x * Py - m_rA.y * Px);
        vB.x += m_invMassB * Px;
        vB.y += m_invMassB * Py;
        wB += m_invIB * (m_rB.x * Py - m_rB.y * Px);
    } else {
        m_impulse = 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 : Vec2(org.jbox2d.common.Vec2) Rot(org.jbox2d.common.Rot)

Example 39 with Vec2

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

the class RopeJoint 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;
    // Cdot = dot(u, v + cross(w, r))
    Vec2 vpA = pool.popVec2();
    Vec2 vpB = pool.popVec2();
    Vec2 temp = pool.popVec2();
    Vec2.crossToOutUnsafe(wA, m_rA, vpA);
    vpA.addLocal(vA);
    Vec2.crossToOutUnsafe(wB, m_rB, vpB);
    vpB.addLocal(vB);
    float C = m_length - m_maxLength;
    float Cdot = Vec2.dot(m_u, temp.set(vpB).subLocal(vpA));
    // Predictive constraint.
    if (C < 0.0f) {
        Cdot += data.step.inv_dt * C;
    }
    float impulse = -m_mass * Cdot;
    float oldImpulse = m_impulse;
    m_impulse = MathUtils.min(0.0f, m_impulse + impulse);
    impulse = m_impulse - oldImpulse;
    float Px = impulse * m_u.x;
    float Py = impulse * m_u.y;
    vA.x -= m_invMassA * Px;
    vA.y -= m_invMassA * Py;
    wA -= m_invIA * (m_rA.x * Py - m_rA.y * Px);
    vB.x += m_invMassB * Px;
    vB.y += m_invMassB * Py;
    wB += m_invIB * (m_rB.x * Py - m_rB.y * Px);
    pool.pushVec2(3);
    // 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 : Vec2(org.jbox2d.common.Vec2)

Example 40 with Vec2

use of org.jbox2d.common.Vec2 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)

Aggregations

Vec2 (org.jbox2d.common.Vec2)185 Rot (org.jbox2d.common.Rot)36 Body (org.jbox2d.dynamics.Body)11 World (org.jbox2d.dynamics.World)9 AABB (org.jbox2d.collision.AABB)8 Mat22 (org.jbox2d.common.Mat22)7 Joint (org.jbox2d.dynamics.joints.Joint)7 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)7 ManifoldPoint (org.jbox2d.collision.ManifoldPoint)6 Test (org.junit.jupiter.api.Test)6 PolygonShape (org.jbox2d.collision.shapes.PolygonShape)5 Transform (org.jbox2d.common.Transform)5 Vec3 (org.jbox2d.common.Vec3)5 BodyDef (org.jbox2d.dynamics.BodyDef)5 VelocityConstraintPoint (org.jbox2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 CircleShape (org.jbox2d.collision.shapes.CircleShape)4 Test (org.junit.Test)4 AffineTransform (java.awt.geom.AffineTransform)3 Shape (org.jbox2d.collision.shapes.Shape)3 Mat33 (org.jbox2d.common.Mat33)3