Search in sources :

Example 1 with Mat33

use of com.almasb.fxgl.physics.box2d.common.Mat33 in project FXGL by AlmasB.

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 Rotation qA = pool.popRot();
    final Rotation 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;
    Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rotation.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 = FXGLMath.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 <= JBoxSettings.linearSlop && angularError <= JBoxSettings.angularSlop;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Vec3(com.almasb.fxgl.core.math.Vec3) Mat33(com.almasb.fxgl.physics.box2d.common.Mat33) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Example 2 with Mat33

use of com.almasb.fxgl.physics.box2d.common.Mat33 in project FXGL by AlmasB.

the class WeldJoint 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 Rotation qA = pool.popRot();
    final Rotation qB = pool.popRot();
    final Vec2 temp = pool.popVec2();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective masses.
    Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
    Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(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 Mat33 K = pool.popMat33();
    K.ex.x = mA + mB + m_rA.y * m_rA.y * iA + m_rB.y * m_rB.y * iB;
    K.ey.x = -m_rA.y * m_rA.x * iA - m_rB.y * m_rB.x * iB;
    K.ez.x = -m_rA.y * iA - m_rB.y * iB;
    K.ex.y = K.ey.x;
    K.ey.y = mA + mB + m_rA.x * m_rA.x * iA + m_rB.x * m_rB.x * iB;
    K.ez.y = m_rA.x * iA + m_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) {
        K.getInverse22(m_mass);
        float invM = iA + iB;
        float m = invM > 0.0f ? 1.0f / invM : 0.0f;
        float C = aB - aA - m_referenceAngle;
        // Frequency
        float omega = 2.0f * (float) FXGLMath.PI * m_frequencyHz;
        // Damping coefficient
        float d = 2.0f * m * m_dampingRatio * omega;
        // Spring stiffness
        float k = m * omega * omega;
        // magic formulas
        float h = data.step.dt;
        m_gamma = h * (d + h * k);
        m_gamma = m_gamma != 0.0f ? 1.0f / m_gamma : 0.0f;
        m_bias = C * h * k * m_gamma;
        invM += m_gamma;
        m_mass.ez.z = invM != 0.0f ? 1.0f / invM : 0.0f;
    } else {
        K.getSymInverse33(m_mass);
        m_gamma = 0.0f;
        m_bias = 0.0f;
    }
    if (data.step.warmStarting) {
        final Vec2 P = pool.popVec2();
        // Scale impulses to support a variable time step.
        m_impulse.mulLocal(data.step.dtRatio);
        P.set(m_impulse.x, m_impulse.y);
        vA.x -= mA * P.x;
        vA.y -= mA * P.y;
        wA -= iA * (Vec2.cross(m_rA, P) + m_impulse.z);
        vB.x += mB * P.x;
        vB.y += mB * P.y;
        wB += iB * (Vec2.cross(m_rB, P) + m_impulse.z);
        pool.pushVec2(1);
    } else {
        m_impulse.setZero();
    }
    // 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);
    pool.pushMat33(1);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Mat33(com.almasb.fxgl.physics.box2d.common.Mat33) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Example 3 with Mat33

use of com.almasb.fxgl.physics.box2d.common.Mat33 in project FXGL by AlmasB.

the class PrismaticJoint method solvePositionConstraints.

@Override
public boolean solvePositionConstraints(final SolverData data) {
    final Rotation qA = pool.popRot();
    final Rotation 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
    Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_localCenterA), rA);
    Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_localCenterB), rB);
    d.set(cB).addLocal(rB).subLocal(cA).subLocal(rA);
    Rotation.mulToOutUnsafe(qA, m_localXAxisA, axis);
    float a1 = Vec2.cross(temp.set(d).addLocal(rA), axis);
    float a2 = Vec2.cross(rB, axis);
    Rotation.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 = FXGLMath.abs(C1.x);
    float angularError = FXGLMath.abs(C1.y);
    boolean active = false;
    float C2 = 0.0f;
    if (m_enableLimit) {
        float translation = Vec2.dot(axis, d);
        if (FXGLMath.abs(m_upperTranslation - m_lowerTranslation) < 2.0f * JBoxSettings.linearSlop) {
            // Prevent large angular corrections
            C2 = FXGLMath.clamp(translation, -JBoxSettings.maxLinearCorrection, JBoxSettings.maxLinearCorrection);
            linearError = Math.max(linearError, FXGLMath.abs(translation));
            active = true;
        } else if (translation <= m_lowerTranslation) {
            // Prevent large linear corrections and allow some slop.
            C2 = FXGLMath.clamp(translation - m_lowerTranslation + JBoxSettings.linearSlop, -JBoxSettings.maxLinearCorrection, 0.0f);
            linearError = Math.max(linearError, m_lowerTranslation - translation);
            active = true;
        } else if (translation >= m_upperTranslation) {
            // Prevent large linear corrections and allow some slop.
            C2 = FXGLMath.clamp(translation - m_upperTranslation - JBoxSettings.linearSlop, 0.0f, JBoxSettings.maxLinearCorrection);
            linearError = Math.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 <= JBoxSettings.linearSlop && angularError <= JBoxSettings.angularSlop;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Vec3(com.almasb.fxgl.core.math.Vec3) Mat33(com.almasb.fxgl.physics.box2d.common.Mat33) Mat22(com.almasb.fxgl.physics.box2d.common.Mat22) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Aggregations

Vec2 (com.almasb.fxgl.core.math.Vec2)3 Mat33 (com.almasb.fxgl.physics.box2d.common.Mat33)3 Rotation (com.almasb.fxgl.physics.box2d.common.Rotation)3 Vec3 (com.almasb.fxgl.core.math.Vec3)2 Mat22 (com.almasb.fxgl.physics.box2d.common.Mat22)1