Search in sources :

Example 1 with ManifoldPoint

use of com.almasb.fxgl.physics.box2d.collision.ManifoldPoint 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 2 with ManifoldPoint

use of com.almasb.fxgl.physics.box2d.collision.ManifoldPoint in project FXGL by AlmasB.

the class ContactSolver method init.

public void init(ContactSolverDef def) {
    TimeStep step = def.step;
    m_count = def.count;
    if (m_positionConstraints.length < m_count) {
        ContactPositionConstraint[] old = m_positionConstraints;
        m_positionConstraints = new ContactPositionConstraint[Math.max(old.length * 2, m_count)];
        System.arraycopy(old, 0, m_positionConstraints, 0, old.length);
        for (int i = old.length; i < m_positionConstraints.length; i++) {
            m_positionConstraints[i] = new ContactPositionConstraint();
        }
    }
    if (m_velocityConstraints.length < m_count) {
        ContactVelocityConstraint[] old = m_velocityConstraints;
        m_velocityConstraints = new ContactVelocityConstraint[Math.max(old.length * 2, m_count)];
        System.arraycopy(old, 0, m_velocityConstraints, 0, old.length);
        for (int i = old.length; i < m_velocityConstraints.length; i++) {
            m_velocityConstraints[i] = new ContactVelocityConstraint();
        }
    }
    m_positions = def.positions;
    m_velocities = def.velocities;
    m_contacts = def.contacts;
    for (int i = 0; i < m_count; ++i) {
        final Contact contact = m_contacts[i];
        final Fixture fixtureA = contact.m_fixtureA;
        final Fixture fixtureB = contact.m_fixtureB;
        final Shape shapeA = fixtureA.getShape();
        final Shape shapeB = fixtureB.getShape();
        final float radiusA = shapeA.getRadius();
        final float radiusB = shapeB.getRadius();
        final Body bodyA = fixtureA.getBody();
        final Body bodyB = fixtureB.getBody();
        final Manifold manifold = contact.getManifold();
        int pointCount = manifold.pointCount;
        assert pointCount > 0;
        ContactVelocityConstraint vc = m_velocityConstraints[i];
        vc.friction = contact.getFriction();
        vc.restitution = contact.getRestitution();
        vc.tangentSpeed = contact.getTangentSpeed();
        vc.indexA = bodyA.m_islandIndex;
        vc.indexB = bodyB.m_islandIndex;
        vc.invMassA = bodyA.m_invMass;
        vc.invMassB = bodyB.m_invMass;
        vc.invIA = bodyA.m_invI;
        vc.invIB = bodyB.m_invI;
        vc.contactIndex = i;
        vc.pointCount = pointCount;
        vc.K.setZero();
        vc.normalMass.setZero();
        ContactPositionConstraint pc = m_positionConstraints[i];
        pc.indexA = bodyA.m_islandIndex;
        pc.indexB = bodyB.m_islandIndex;
        pc.invMassA = bodyA.m_invMass;
        pc.invMassB = bodyB.m_invMass;
        pc.localCenterA.set(bodyA.m_sweep.localCenter);
        pc.localCenterB.set(bodyB.m_sweep.localCenter);
        pc.invIA = bodyA.m_invI;
        pc.invIB = bodyB.m_invI;
        pc.localNormal.set(manifold.localNormal);
        pc.localPoint.set(manifold.localPoint);
        pc.pointCount = pointCount;
        pc.radiusA = radiusA;
        pc.radiusB = radiusB;
        pc.type = manifold.type;
        for (int j = 0; j < pointCount; j++) {
            ManifoldPoint cp = manifold.points[j];
            VelocityConstraintPoint vcp = vc.points[j];
            if (step.warmStarting) {
                vcp.normalImpulse = step.dtRatio * cp.normalImpulse;
                vcp.tangentImpulse = step.dtRatio * cp.tangentImpulse;
            } else {
                vcp.normalImpulse = 0;
                vcp.tangentImpulse = 0;
            }
            vcp.rA.setZero();
            vcp.rB.setZero();
            vcp.normalMass = 0;
            vcp.tangentMass = 0;
            vcp.velocityBias = 0;
            pc.localPoints[j].x = cp.localPoint.x;
            pc.localPoints[j].y = cp.localPoint.y;
        }
    }
}
Also used : Shape(com.almasb.fxgl.physics.box2d.collision.shapes.Shape) ManifoldPoint(com.almasb.fxgl.physics.box2d.collision.ManifoldPoint) ManifoldPoint(com.almasb.fxgl.physics.box2d.collision.ManifoldPoint) VelocityConstraintPoint(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint) TimeStep(com.almasb.fxgl.physics.box2d.dynamics.TimeStep) WorldManifold(com.almasb.fxgl.physics.box2d.collision.WorldManifold) Manifold(com.almasb.fxgl.physics.box2d.collision.Manifold) VelocityConstraintPoint(com.almasb.fxgl.physics.box2d.dynamics.contacts.ContactVelocityConstraint.VelocityConstraintPoint) Fixture(com.almasb.fxgl.physics.box2d.dynamics.Fixture) Body(com.almasb.fxgl.physics.box2d.dynamics.Body)

Aggregations

ManifoldPoint (com.almasb.fxgl.physics.box2d.collision.ManifoldPoint)2 Shape (com.almasb.fxgl.physics.box2d.collision.shapes.Shape)2 Body (com.almasb.fxgl.physics.box2d.dynamics.Body)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