Search in sources :

Example 51 with Vec2

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

the class DistanceJoint 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();
    qA.set(aA);
    qB.set(aB);
    // use m_u as temporary variable
    Rotation.mulToOutUnsafe(qA, m_u.set(m_localAnchorA).subLocal(m_localCenterA), m_rA);
    Rotation.mulToOutUnsafe(qB, m_u.set(m_localAnchorB).subLocal(m_localCenterB), m_rB);
    m_u.set(cB).addLocal(m_rB).subLocal(cA).subLocal(m_rA);
    pool.pushRot(2);
    // Handle singularity.
    float length = m_u.length();
    if (length > JBoxSettings.linearSlop) {
        m_u.x *= 1.0f / length;
        m_u.y *= 1.0f / length;
    } else {
        m_u.set(0.0f, 0.0f);
    }
    float crAu = Vec2.cross(m_rA, m_u);
    float crBu = Vec2.cross(m_rB, m_u);
    float invMass = m_invMassA + m_invIA * crAu * crAu + m_invMassB + m_invIB * crBu * crBu;
    // Compute the effective mass matrix.
    m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
    if (m_frequencyHz > 0.0f) {
        float C = length - m_length;
        // Frequency
        float omega = 2.0f * (float) FXGLMath.PI * m_frequencyHz;
        // Damping coefficient
        float d = 2.0f * m_mass * m_dampingRatio * omega;
        // Spring stiffness
        float k = m_mass * 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;
        invMass += m_gamma;
        m_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
    } else {
        m_gamma = 0.0f;
        m_bias = 0.0f;
    }
    if (data.step.warmStarting) {
        // Scale the impulse to support a variable time step.
        m_impulse *= data.step.dtRatio;
        Vec2 P = pool.popVec2();
        P.set(m_u).mulLocal(m_impulse);
        vA.x -= m_invMassA * P.x;
        vA.y -= m_invMassA * P.y;
        wA -= m_invIA * Vec2.cross(m_rA, P);
        vB.x += m_invMassB * P.x;
        vB.y += m_invMassB * P.y;
        wB += m_invIB * Vec2.cross(m_rB, P);
        pool.pushVec2(1);
    } 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;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Rotation(com.almasb.fxgl.physics.box2d.common.Rotation)

Example 52 with Vec2

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

the class MotorJoint method solveVelocityConstraints.

@Override
public void solveVelocityConstraints(SolverData data) {
    final Vec2 vA = data.velocities[m_indexA].v;
    float wA = data.velocities[m_indexA].w;
    final Vec2 vB = data.velocities[m_indexB].v;
    float wB = data.velocities[m_indexB].w;
    float mA = m_invMassA;
    float mB = m_invMassB;
    float iA = m_invIA;
    float iB = m_invIB;
    float h = data.step.dt;
    float inv_h = data.step.inv_dt;
    final Vec2 temp = pool.popVec2();
    // Solve angular friction
    {
        float Cdot = wB - wA + inv_h * correctionFactor * m_angularError;
        float impulse = -m_angularMass * Cdot;
        float oldImpulse = angularImpulse;
        float maxImpulse = h * maxTorque;
        angularImpulse = FXGLMath.clamp(angularImpulse + impulse, -maxImpulse, maxImpulse);
        impulse = angularImpulse - oldImpulse;
        wA -= iA * impulse;
        wB += iB * impulse;
    }
    final Vec2 Cdot = pool.popVec2();
    // Solve linear friction
    {
        // Cdot = vB + b2Cross(wB, m_rB) - vA - b2Cross(wA, m_rA) + inv_h * m_correctionFactor *
        // m_linearError;
        Cdot.x = vB.x + -wB * m_rB.y - vA.x - -wA * m_rA.y + inv_h * correctionFactor * m_linearError.x;
        Cdot.y = vB.y + wB * m_rB.x - vA.y - wA * m_rA.x + inv_h * correctionFactor * m_linearError.y;
        final Vec2 impulse = temp;
        Mat22.mulToOutUnsafe(m_linearMass, Cdot, impulse);
        impulse.negateLocal();
        final Vec2 oldImpulse = pool.popVec2();
        oldImpulse.set(linearImpulse);
        linearImpulse.addLocal(impulse);
        float maxImpulse = h * maxForce;
        if (linearImpulse.lengthSquared() > maxImpulse * maxImpulse) {
            linearImpulse.getLengthAndNormalize();
            linearImpulse.mulLocal(maxImpulse);
        }
        impulse.x = linearImpulse.x - oldImpulse.x;
        impulse.y = linearImpulse.y - oldImpulse.y;
        vA.x -= mA * impulse.x;
        vA.y -= mA * impulse.y;
        wA -= iA * (m_rA.x * impulse.y - m_rA.y * impulse.x);
        vB.x += mB * impulse.x;
        vB.y += mB * impulse.y;
        wB += iB * (m_rB.x * impulse.y - m_rB.y * impulse.x);
    }
    pool.pushVec2(3);
    // 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;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 53 with Vec2

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

the class Body method getLinearVelocityFromLocalPoint.

/**
 * Get the world velocity of a local point.
 *
 * @param localPoint point in local coordinates.
 * @return the world velocity of a point.
 */
public Vec2 getLinearVelocityFromLocalPoint(Vec2 localPoint) {
    Vec2 out = new Vec2();
    getLinearVelocityFromLocalPointToOut(localPoint, out);
    return out;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 54 with Vec2

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

the class PulleyJointDef method initialize.

/**
 * Initialize the bodies, anchors, lengths, max lengths, and ratio using the world anchors.
 */
public void initialize(Body b1, Body b2, Vec2 ga1, Vec2 ga2, Vec2 anchor1, Vec2 anchor2, float r) {
    setBodyA(b1);
    setBodyB(b2);
    groundAnchorA = ga1;
    groundAnchorB = ga2;
    localAnchorA = b1.getLocalPoint(anchor1);
    localAnchorB = b2.getLocalPoint(anchor2);
    Vec2 d1 = anchor1.sub(ga1);
    lengthA = d1.length();
    Vec2 d2 = anchor2.sub(ga2);
    lengthB = d2.length();
    ratio = r;
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2)

Example 55 with Vec2

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

the class EdgeShape method computeAABB.

@Override
@SuppressWarnings("PMD.UselessParentheses")
public void computeAABB(AABB aabb, Transform xf, int childIndex) {
    final Vec2 lowerBound = aabb.lowerBound;
    final Vec2 upperBound = aabb.upperBound;
    final Rotation xfq = xf.q;
    final float v1x = (xfq.c * m_vertex1.x - xfq.s * m_vertex1.y) + xf.p.x;
    final float v1y = (xfq.s * m_vertex1.x + xfq.c * m_vertex1.y) + xf.p.y;
    final float v2x = (xfq.c * m_vertex2.x - xfq.s * m_vertex2.y) + xf.p.x;
    final float v2y = (xfq.s * m_vertex2.x + xfq.c * m_vertex2.y) + xf.p.y;
    lowerBound.x = v1x < v2x ? v1x : v2x;
    lowerBound.y = v1y < v2y ? v1y : v2y;
    upperBound.x = v1x > v2x ? v1x : v2x;
    upperBound.y = v1y > v2y ? v1y : v2y;
    lowerBound.x -= getRadius();
    lowerBound.y -= getRadius();
    upperBound.x += getRadius();
    upperBound.y += getRadius();
}
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