Search in sources :

Example 1 with Body

use of com.almasb.fxgl.physics.box2d.dynamics.Body in project FXGL by AlmasB.

the class ParticleSystem method solveViscous.

@SuppressWarnings("PMD.UnusedFormalParameter")
private void solveViscous(final TimeStep step) {
    float viscousStrength = m_viscousStrength;
    for (int k = 0; k < m_bodyContactCount; k++) {
        final ParticleBodyContact contact = m_bodyContactBuffer[k];
        int a = contact.index;
        if ((m_flagsBuffer.data[a] & ParticleTypeInternal.b2_viscousParticle) != 0) {
            Body b = contact.body;
            float w = contact.weight;
            float m = contact.mass;
            Vec2 p = m_positionBuffer.data[a];
            final Vec2 va = m_velocityBuffer.data[a];
            final float tempX = p.x - b.m_sweep.c.x;
            final float tempY = p.y - b.m_sweep.c.y;
            final float vx = -b.getAngularVelocity() * tempY + b.getLinearVelocity().x - va.x;
            final float vy = b.getAngularVelocity() * tempX + b.getLinearVelocity().y - va.y;
            final Vec2 f = tempVec;
            final float pInvMass = getParticleInvMass();
            f.x = viscousStrength * m * w * vx;
            f.y = viscousStrength * m * w * vy;
            va.x += pInvMass * f.x;
            va.y += pInvMass * f.y;
            f.x = -f.x;
            f.y = -f.y;
            b.applyLinearImpulse(f, p, true);
        }
    }
    for (int k = 0; k < m_contactCount; k++) {
        final ParticleContact contact = m_contactBuffer[k];
        if ((contact.flags & ParticleTypeInternal.b2_viscousParticle) != 0) {
            int a = contact.indexA;
            int b = contact.indexB;
            float w = contact.weight;
            final Vec2 va = m_velocityBuffer.data[a];
            final Vec2 vb = m_velocityBuffer.data[b];
            final float vx = vb.x - va.x;
            final float vy = vb.y - va.y;
            final float fx = viscousStrength * w * vx;
            final float fy = viscousStrength * w * vy;
            va.x += fx;
            va.y += fy;
            vb.x -= fx;
            vb.y -= fy;
        }
    }
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Body(com.almasb.fxgl.physics.box2d.dynamics.Body)

Example 2 with Body

use of com.almasb.fxgl.physics.box2d.dynamics.Body in project FXGL by AlmasB.

the class Contact method update.

public void update(ContactListener listener) {
    oldManifold.set(m_manifold);
    // Re-enable this contact.
    m_flags |= ENABLED_FLAG;
    boolean wasTouching = (m_flags & TOUCHING_FLAG) == TOUCHING_FLAG;
    boolean sensorA = m_fixtureA.isSensor();
    boolean sensorB = m_fixtureB.isSensor();
    boolean sensor = sensorA || sensorB;
    Body bodyA = m_fixtureA.getBody();
    Body bodyB = m_fixtureB.getBody();
    Transform xfA = bodyA.getTransform();
    Transform xfB = bodyB.getTransform();
    boolean touching;
    if (sensor) {
        Shape shapeA = m_fixtureA.getShape();
        Shape shapeB = m_fixtureB.getShape();
        touching = GenericCollision.testOverlap(pool, shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
        // Sensors don't generate manifolds.
        m_manifold.pointCount = 0;
    } else {
        evaluate(m_manifold, xfA, xfB);
        touching = m_manifold.pointCount > 0;
        // stored impulses to warm start the solver.
        for (int i = 0; i < m_manifold.pointCount; ++i) {
            ManifoldPoint mp2 = m_manifold.points[i];
            mp2.normalImpulse = 0.0f;
            mp2.tangentImpulse = 0.0f;
            ContactID id2 = mp2.id;
            for (int j = 0; j < oldManifold.pointCount; ++j) {
                ManifoldPoint mp1 = oldManifold.points[j];
                if (mp1.id.isEqual(id2)) {
                    mp2.normalImpulse = mp1.normalImpulse;
                    mp2.tangentImpulse = mp1.tangentImpulse;
                    break;
                }
            }
        }
        if (touching != wasTouching) {
            bodyA.setAwake(true);
            bodyB.setAwake(true);
        }
    }
    if (touching) {
        m_flags |= TOUCHING_FLAG;
    } else {
        m_flags &= ~TOUCHING_FLAG;
    }
    if (listener == null) {
        return;
    }
    if (!wasTouching && touching) {
        listener.beginContact(this);
    }
    if (wasTouching && !touching) {
        listener.endContact(this);
    }
    if (!sensor && touching) {
        listener.preSolve(this, oldManifold);
    }
}
Also used : Shape(com.almasb.fxgl.physics.box2d.collision.shapes.Shape) ManifoldPoint(com.almasb.fxgl.physics.box2d.collision.ManifoldPoint) ContactID(com.almasb.fxgl.physics.box2d.collision.ContactID) Transform(com.almasb.fxgl.physics.box2d.common.Transform) Body(com.almasb.fxgl.physics.box2d.dynamics.Body) ManifoldPoint(com.almasb.fxgl.physics.box2d.collision.ManifoldPoint)

Example 3 with Body

use of com.almasb.fxgl.physics.box2d.dynamics.Body 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 4 with Body

use of com.almasb.fxgl.physics.box2d.dynamics.Body in project FXGL by AlmasB.

the class ParticleSystem method solveDamping.

@SuppressWarnings("PMD.UnusedFormalParameter")
private void solveDamping(TimeStep step) {
    // reduces normal velocity of each contact
    float damping = m_dampingStrength;
    for (int k = 0; k < m_bodyContactCount; k++) {
        final ParticleBodyContact contact = m_bodyContactBuffer[k];
        int a = contact.index;
        Body b = contact.body;
        float w = contact.weight;
        float m = contact.mass;
        Vec2 n = contact.normal;
        Vec2 p = m_positionBuffer.data[a];
        final float tempX = p.x - b.m_sweep.c.x;
        final float tempY = p.y - b.m_sweep.c.y;
        final Vec2 velA = m_velocityBuffer.data[a];
        // getLinearVelocityFromWorldPointToOut, with -= velA
        float vx = -b.getAngularVelocity() * tempY + b.getLinearVelocity().x - velA.x;
        float vy = b.getAngularVelocity() * tempX + b.getLinearVelocity().y - velA.y;
        // done
        float vn = vx * n.x + vy * n.y;
        if (vn < 0) {
            final Vec2 f = tempVec;
            f.x = damping * w * m * vn * n.x;
            f.y = damping * w * m * vn * n.y;
            final float invMass = getParticleInvMass();
            velA.x += invMass * f.x;
            velA.y += invMass * f.y;
            f.x = -f.x;
            f.y = -f.y;
            b.applyLinearImpulse(f, p, true);
        }
    }
    for (int k = 0; k < m_contactCount; k++) {
        final ParticleContact contact = m_contactBuffer[k];
        int a = contact.indexA;
        int b = contact.indexB;
        float w = contact.weight;
        Vec2 n = contact.normal;
        final Vec2 velA = m_velocityBuffer.data[a];
        final Vec2 velB = m_velocityBuffer.data[b];
        final float vx = velB.x - velA.x;
        final float vy = velB.y - velA.y;
        float vn = vx * n.x + vy * n.y;
        if (vn < 0) {
            float fx = damping * w * vn * n.x;
            float fy = damping * w * vn * n.y;
            velA.x += fx;
            velA.y += fy;
            velB.x -= fx;
            velB.y -= fy;
        }
    }
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Body(com.almasb.fxgl.physics.box2d.dynamics.Body)

Example 5 with Body

use of com.almasb.fxgl.physics.box2d.dynamics.Body in project FXGL by AlmasB.

the class ParticleSystem method solvePowder.

private void solvePowder(final TimeStep step) {
    float powderStrength = m_powderStrength * getCriticalVelocity(step);
    float minWeight = 1.0f - JBoxSettings.particleStride;
    for (int k = 0; k < m_bodyContactCount; k++) {
        final ParticleBodyContact contact = m_bodyContactBuffer[k];
        int a = contact.index;
        if ((m_flagsBuffer.data[a] & ParticleTypeInternal.b2_powderParticle) != 0) {
            float w = contact.weight;
            if (w > minWeight) {
                Body b = contact.body;
                float m = contact.mass;
                Vec2 p = m_positionBuffer.data[a];
                Vec2 n = contact.normal;
                final Vec2 f = tempVec;
                final Vec2 va = m_velocityBuffer.data[a];
                final float inter = powderStrength * m * (w - minWeight);
                final float pInvMass = getParticleInvMass();
                f.x = inter * n.x;
                f.y = inter * n.y;
                va.x -= pInvMass * f.x;
                va.y -= pInvMass * f.y;
                b.applyLinearImpulse(f, p, true);
            }
        }
    }
    for (int k = 0; k < m_contactCount; k++) {
        final ParticleContact contact = m_contactBuffer[k];
        if ((contact.flags & ParticleTypeInternal.b2_powderParticle) != 0) {
            float w = contact.weight;
            if (w > minWeight) {
                int a = contact.indexA;
                int b = contact.indexB;
                Vec2 n = contact.normal;
                final Vec2 va = m_velocityBuffer.data[a];
                final Vec2 vb = m_velocityBuffer.data[b];
                final float inter = powderStrength * (w - minWeight);
                final float fx = inter * n.x;
                final float fy = inter * n.y;
                va.x -= fx;
                va.y -= fy;
                vb.x += fx;
                vb.y += fy;
            }
        }
    }
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) Body(com.almasb.fxgl.physics.box2d.dynamics.Body)

Aggregations

Body (com.almasb.fxgl.physics.box2d.dynamics.Body)10 Vec2 (com.almasb.fxgl.core.math.Vec2)6 ManifoldPoint (com.almasb.fxgl.physics.box2d.collision.ManifoldPoint)2 Shape (com.almasb.fxgl.physics.box2d.collision.shapes.Shape)2 ContactID (com.almasb.fxgl.physics.box2d.collision.ContactID)1 Manifold (com.almasb.fxgl.physics.box2d.collision.Manifold)1 WorldManifold (com.almasb.fxgl.physics.box2d.collision.WorldManifold)1 Transform (com.almasb.fxgl.physics.box2d.common.Transform)1 Fixture (com.almasb.fxgl.physics.box2d.dynamics.Fixture)1 TimeStep (com.almasb.fxgl.physics.box2d.dynamics.TimeStep)1 VelocityConstraintPoint (com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint)1