Search in sources :

Example 86 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class WheelJoint method getJointTranslation.

public float getJointTranslation() {
    Body b1 = m_bodyA;
    Body b2 = m_bodyB;
    Vec2 p1 = pool.popVec2();
    Vec2 p2 = pool.popVec2();
    Vec2 axis = pool.popVec2();
    b1.getWorldPointToOut(m_localAnchorA, p1);
    b2.getWorldPointToOut(m_localAnchorA, p2);
    p2.subLocal(p1);
    b1.getWorldVectorToOut(m_localXAxisA, axis);
    float translation = Vec2.dot(p2, axis);
    pool.pushVec2(3);
    return translation;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Body(com.almasb.fxgl.physics.box2d.dynamics.Body)

Example 87 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class WheelJoint method getReactionForce.

@Override
public void getReactionForce(float inv_dt, Vec2 argOut) {
    final Vec2 temp = pool.popVec2();
    temp.set(m_ay).mulLocal(m_impulse);
    argOut.set(m_ax).mulLocal(m_springImpulse).addLocal(temp).mulLocal(inv_dt);
    pool.pushVec2(1);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 88 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class WheelJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(SolverData data) {
    float mA = m_invMassA, mB = m_invMassB;
    float iA = m_invIA, iB = m_invIB;
    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;
    final Vec2 temp = pool.popVec2();
    final Vec2 P = pool.popVec2();
    // Solve spring constraint
    {
        float Cdot = Vec2.dot(m_ax, temp.set(vB).subLocal(vA)) + m_sBx * wB - m_sAx * wA;
        float impulse = -m_springMass * (Cdot + m_bias + m_gamma * m_springImpulse);
        m_springImpulse += impulse;
        P.x = impulse * m_ax.x;
        P.y = impulse * m_ax.y;
        float LA = impulse * m_sAx;
        float LB = impulse * m_sBx;
        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;
    }
    // Solve rotational motor constraint
    {
        float Cdot = wB - wA - m_motorSpeed;
        float impulse = -m_motorMass * Cdot;
        float oldImpulse = m_motorImpulse;
        float maxImpulse = data.step.dt * m_maxMotorTorque;
        m_motorImpulse = FXGLMath.clamp(m_motorImpulse + impulse, -maxImpulse, maxImpulse);
        impulse = m_motorImpulse - oldImpulse;
        wA -= iA * impulse;
        wB += iB * impulse;
    }
    // Solve point to line constraint
    {
        float Cdot = Vec2.dot(m_ay, temp.set(vB).subLocal(vA)) + m_sBy * wB - m_sAy * wA;
        float impulse = -m_mass * Cdot;
        m_impulse += impulse;
        P.x = impulse * m_ay.x;
        P.y = impulse * m_ay.y;
        float LA = impulse * m_sAy;
        float LB = impulse * m_sBy;
        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 = vA;
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v = vB;
    data.velocities[m_indexB].w = wB;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 89 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class FrictionJoint 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;
    float aA = data.positions[m_indexA].a;
    Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    float aB = data.positions[m_indexB].a;
    Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    final Vec2 temp = pool.popVec2();
    final Rotation qA = pool.popRot();
    final Rotation qB = pool.popRot();
    qA.set(aA);
    qB.set(aB);
    // Compute the effective mass matrix.
    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 Mat22 K = pool.popMat22();
    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;
    }
    if (data.step.warmStarting) {
        // Scale impulses to support a variable time step.
        m_linearImpulse.mulLocal(data.step.dtRatio);
        m_angularImpulse *= data.step.dtRatio;
        final Vec2 P = pool.popVec2();
        P.set(m_linearImpulse);
        temp.set(P).mulLocal(mA);
        vA.subLocal(temp);
        wA -= iA * (Vec2.cross(m_rA, P) + m_angularImpulse);
        temp.set(P).mulLocal(mB);
        vB.addLocal(temp);
        wB += iB * (Vec2.cross(m_rB, P) + m_angularImpulse);
        pool.pushVec2(1);
    } else {
        m_linearImpulse.setZero();
        m_angularImpulse = 0.0f;
    }
    // data.velocities[m_indexA].v.set(vA);
    if (data.velocities[m_indexA].w != wA) {
        assert data.velocities[m_indexA].w != wA;
    }
    data.velocities[m_indexA].w = wA;
    // data.velocities[m_indexB].v.set(vB);
    data.velocities[m_indexB].w = wB;
    pool.pushRot(2);
    pool.pushVec2(1);
    pool.pushMat22(1);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Mat22(com.almasb.fxgl.physics.box2d.common.Mat22) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Example 90 with Vec2

use of com.almasb.fxgl.core.math.Vec2 in project FXGL by AlmasB.

the class GearJoint method initVelocityConstraints.

@Override
public void initVelocityConstraints(SolverData data) {
    m_indexA = m_bodyA.m_islandIndex;
    m_indexB = m_bodyB.m_islandIndex;
    m_indexC = m_bodyC.m_islandIndex;
    m_indexD = m_bodyD.m_islandIndex;
    m_lcA.set(m_bodyA.m_sweep.localCenter);
    m_lcB.set(m_bodyB.m_sweep.localCenter);
    m_lcC.set(m_bodyC.m_sweep.localCenter);
    m_lcD.set(m_bodyD.m_sweep.localCenter);
    m_mA = m_bodyA.m_invMass;
    m_mB = m_bodyB.m_invMass;
    m_mC = m_bodyC.m_invMass;
    m_mD = m_bodyD.m_invMass;
    m_iA = m_bodyA.m_invI;
    m_iB = m_bodyB.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;
    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;
    // Vec2 cC = data.positions[m_indexC].c;
    float aC = data.positions[m_indexC].a;
    Vec2 vC = data.velocities[m_indexC].v;
    float wC = data.velocities[m_indexC].w;
    // Vec2 cD = data.positions[m_indexD].c;
    float aD = data.positions[m_indexD].a;
    Vec2 vD = data.velocities[m_indexD].v;
    float wD = data.velocities[m_indexD].w;
    Rotation 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;
    Vec2 temp = pool.popVec2();
    if (m_typeA.equals(RevoluteJoint.class)) {
        m_JvAC.setZero();
        m_JwA = 1.0f;
        m_JwC = 1.0f;
        m_mass += m_iA + m_iC;
    } else {
        Vec2 rC = pool.popVec2();
        Vec2 rA = pool.popVec2();
        Rotation.mulToOutUnsafe(qC, m_localAxisC, m_JvAC);
        Rotation.mulToOutUnsafe(qC, temp.set(m_localAnchorC).subLocal(m_lcC), rC);
        Rotation.mulToOutUnsafe(qA, temp.set(m_localAnchorA).subLocal(m_lcA), rA);
        m_JwC = Vec2.cross(rC, m_JvAC);
        m_JwA = Vec2.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.equals(RevoluteJoint.class)) {
        m_JvBD.setZero();
        m_JwB = m_ratio;
        m_JwD = m_ratio;
        m_mass += m_ratio * m_ratio * (m_iB + m_iD);
    } else {
        Vec2 u = pool.popVec2();
        Vec2 rD = pool.popVec2();
        Vec2 rB = pool.popVec2();
        Rotation.mulToOutUnsafe(qD, m_localAxisD, u);
        Rotation.mulToOutUnsafe(qD, temp.set(m_localAnchorD).subLocal(m_lcD), rD);
        Rotation.mulToOutUnsafe(qB, temp.set(m_localAnchorB).subLocal(m_lcB), rB);
        m_JvBD.set(u).mulLocal(m_ratio);
        m_JwD = m_ratio * Vec2.cross(rD, u);
        m_JwB = m_ratio * Vec2.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 : Vec2(com.almasb.fxgl.core.math.Vec2) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Aggregations

Vec2 (com.almasb.fxgl.core.math.Vec2)138 Rotation (com.almasb.fxgl.physics.box2d.common.Rotation)36 Point2D (javafx.geometry.Point2D)7 Mat22 (com.almasb.fxgl.physics.box2d.common.Mat22)6 Body (com.almasb.fxgl.physics.box2d.dynamics.Body)6 Rectangle (javafx.scene.shape.Rectangle)6 GameApplication (com.almasb.fxgl.app.GameApplication)5 Vec3 (com.almasb.fxgl.core.math.Vec3)5 AABB (com.almasb.fxgl.physics.box2d.collision.AABB)5 ManifoldPoint (com.almasb.fxgl.physics.box2d.collision.ManifoldPoint)5 VelocityConstraintPoint (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)5 Rectangle2D (javafx.geometry.Rectangle2D)5 Color (javafx.scene.paint.Color)5 Interpolators (com.almasb.fxgl.animation.Interpolators)4 GameSettings (com.almasb.fxgl.app.GameSettings)4 FXGL (com.almasb.fxgl.dsl.FXGL)4 ImagesKt (com.almasb.fxgl.texture.ImagesKt)4 Comparator (java.util.Comparator)4 List (java.util.List)4 Collectors (java.util.stream.Collectors)4