Search in sources :

Example 6 with AABB

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

the class DefaultBroadPhaseBuffer method testOverlap.

@Override
public boolean testOverlap(int proxyIdA, int proxyIdB) {
    AABB a = tree.getFatAABB(proxyIdA);
    AABB b = tree.getFatAABB(proxyIdB);
    return AABB.testOverlap(a, b);
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 7 with AABB

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

the class DynamicTree method validateMetrics.

private void validateMetrics(DynamicTreeNode node) {
    if (node == null) {
        return;
    }
    DynamicTreeNode child1 = node.child1;
    DynamicTreeNode child2 = node.child2;
    if (node.child1 == null) {
        assert (child1 == null);
        assert (child2 == null);
        assert (node.height == 0);
        return;
    }
    assert (child1 != null && 0 <= child1.id && child1.id < m_nodeCapacity);
    assert (child2 != null && 0 <= child2.id && child2.id < m_nodeCapacity);
    int height1 = child1.height;
    int height2 = child2.height;
    int height;
    height = 1 + JBoxUtils.max(height1, height2);
    assert (node.height == height);
    AABB aabb = new AABB();
    aabb.combine(child1.aabb, child2.aabb);
    assert (aabb.lowerBound.equals(node.aabb.lowerBound));
    assert (aabb.upperBound.equals(node.aabb.upperBound));
    validateMetrics(child1);
    validateMetrics(child2);
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 8 with AABB

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

the class ParticleSystem method createParticleGroup.

@SuppressWarnings("PMD.DontUseFloatTypeForLoopIndices")
public ParticleGroup createParticleGroup(ParticleGroupDef groupDef) {
    float stride = getParticleStride();
    final Transform identity = tempTransform;
    identity.setIdentity();
    Transform transform = tempTransform2;
    transform.setIdentity();
    int firstIndex = m_count;
    if (groupDef.getShape() != null) {
        final ParticleDef particleDef = tempParticleDef;
        particleDef.setTypeFlags(groupDef.getTypeFlags());
        particleDef.color = groupDef.getColor();
        particleDef.setUserData(groupDef.getUserData());
        Shape shape = groupDef.getShape();
        transform.set(groupDef.getPosition(), groupDef.getAngle());
        AABB aabb = temp;
        int childCount = shape.getChildCount();
        for (int childIndex = 0; childIndex < childCount; childIndex++) {
            if (childIndex == 0) {
                shape.computeAABB(aabb, identity, childIndex);
            } else {
                AABB childAABB = temp2;
                shape.computeAABB(childAABB, identity, childIndex);
                aabb.combine(childAABB);
            }
        }
        final float upperBoundY = aabb.upperBound.y;
        final float upperBoundX = aabb.upperBound.x;
        for (float y = FXGLMath.floor(aabb.lowerBound.y / stride) * stride; y < upperBoundY; y += stride) {
            for (float x = FXGLMath.floor(aabb.lowerBound.x / stride) * stride; x < upperBoundX; x += stride) {
                Vec2 p = tempVec;
                p.x = x;
                p.y = y;
                if (shape.containsPoint(identity, p)) {
                    Transform.mulToOut(transform, p, p);
                    particleDef.position.x = p.x;
                    particleDef.position.y = p.y;
                    p.subLocal(groupDef.getPosition());
                    Vec2.crossToOutUnsafe(groupDef.getAngularVelocity(), p, particleDef.velocity);
                    particleDef.velocity.addLocal(groupDef.getLinearVelocity());
                    createParticle(particleDef);
                }
            }
        }
    }
    int lastIndex = m_count;
    ParticleGroup group = new ParticleGroup();
    group.m_system = this;
    group.m_firstIndex = firstIndex;
    group.m_lastIndex = lastIndex;
    group.m_groupFlags = groupDef.getGroupFlags();
    group.m_strength = groupDef.getStrength();
    group.m_userData = groupDef.getUserData();
    group.m_transform.set(transform);
    group.m_destroyAutomatically = groupDef.isDestroyAutomatically();
    group.m_prev = null;
    group.m_next = m_groupList;
    if (m_groupList != null) {
        m_groupList.m_prev = group;
    }
    m_groupList = group;
    ++m_groupCount;
    for (int i = firstIndex; i < lastIndex; i++) {
        m_groupBuffer[i] = group;
    }
    updateContacts(true);
    if ((groupDef.getTypeFlags() & k_pairFlags) != 0) {
        for (int k = 0; k < m_contactCount; k++) {
            ParticleContact contact = m_contactBuffer[k];
            int a = contact.indexA;
            int b = contact.indexB;
            if (a > b) {
                int temp = a;
                a = b;
                b = temp;
            }
            if (firstIndex <= a && b < lastIndex) {
                if (m_pairCount >= m_pairCapacity) {
                    int oldCapacity = m_pairCapacity;
                    int newCapacity = m_pairCount != 0 ? 2 * m_pairCount : JBoxSettings.minParticleBufferCapacity;
                    m_pairBuffer = reallocateBuffer(Pair.class, m_pairBuffer, oldCapacity, newCapacity);
                    m_pairCapacity = newCapacity;
                }
                Pair pair = m_pairBuffer[m_pairCount];
                pair.indexA = a;
                pair.indexB = b;
                pair.flags = contact.flags;
                pair.strength = groupDef.getStrength();
                pair.distance = m_positionBuffer.data[a].distanceF(m_positionBuffer.data[b]);
                m_pairCount++;
            }
        }
    }
    if ((groupDef.getTypeFlags() & k_triadFlags) != 0) {
        VoronoiDiagram diagram = new VoronoiDiagram(lastIndex - firstIndex);
        for (int i = firstIndex; i < lastIndex; i++) {
            diagram.addGenerator(m_positionBuffer.data[i], i);
        }
        diagram.generate(stride / 2);
        createParticleGroupCallback.system = this;
        createParticleGroupCallback.def = groupDef;
        createParticleGroupCallback.firstIndex = firstIndex;
        diagram.getNodes(createParticleGroupCallback);
    }
    if ((groupDef.getGroupFlags() & ParticleGroupType.b2_solidParticleGroup) != 0) {
        computeDepthForGroup(group);
    }
    return group;
}
Also used : Shape(com.almasb.fxgl.physics.box2d.collision.shapes.Shape) Vec2(com.almasb.fxgl.core.math.Vec2) Transform(com.almasb.fxgl.physics.box2d.common.Transform) AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 9 with AABB

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

the class ParticleSystem method solveCollision.

private void solveCollision(TimeStep step) {
    final AABB aabb = temp;
    final Vec2 lowerBound = aabb.lowerBound;
    final Vec2 upperBound = aabb.upperBound;
    lowerBound.x = Float.MAX_VALUE;
    lowerBound.y = Float.MAX_VALUE;
    upperBound.x = -Float.MAX_VALUE;
    upperBound.y = -Float.MAX_VALUE;
    for (int i = 0; i < m_count; i++) {
        final Vec2 v = m_velocityBuffer.data[i];
        final Vec2 p1 = m_positionBuffer.data[i];
        final float p1x = p1.x;
        final float p1y = p1.y;
        final float p2x = p1x + step.dt * v.x;
        final float p2y = p1y + step.dt * v.y;
        final float bx = p1x < p2x ? p1x : p2x;
        final float by = p1y < p2y ? p1y : p2y;
        lowerBound.x = lowerBound.x < bx ? lowerBound.x : bx;
        lowerBound.y = lowerBound.y < by ? lowerBound.y : by;
        final float b1x = p1x > p2x ? p1x : p2x;
        final float b1y = p1y > p2y ? p1y : p2y;
        upperBound.x = upperBound.x > b1x ? upperBound.x : b1x;
        upperBound.y = upperBound.y > b1y ? upperBound.y : b1y;
    }
    sccallback.step = step;
    sccallback.system = this;
    m_world.queryAABB(sccallback, aabb);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 10 with AABB

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

the class DefaultBroadPhaseBuffer method updatePairs.

@Override
public void updatePairs(PairCallback callback) {
    // Reset pair buffer
    pairCount = 0;
    // Perform tree queries for all moving proxies.
    for (int i = 0; i < moveCount; ++i) {
        m_queryProxyId = moveBuffer[i];
        if (m_queryProxyId == NULL_PROXY) {
            continue;
        }
        // We have to query the tree with the fat AABB so that
        // we don't fail to create a pair that may touch later.
        final AABB fatAABB = tree.getFatAABB(m_queryProxyId);
        // Query tree, create pairs and add them pair buffer.
        tree.query(this, fatAABB);
    }
    // Reset move buffer
    moveCount = 0;
    // Sort the pair buffer to expose duplicates.
    Arrays.sort(pairBuffer, 0, pairCount);
    // Send the pairs back to the client.
    int i = 0;
    while (i < pairCount) {
        Pair primaryPair = pairBuffer[i];
        Object userDataA = tree.getUserData(primaryPair.proxyIdA);
        Object userDataB = tree.getUserData(primaryPair.proxyIdB);
        callback.addPair(userDataA, userDataB);
        ++i;
        // Skip any duplicate pairs.
        while (i < pairCount) {
            Pair pair = pairBuffer[i];
            if (pair.proxyIdA != primaryPair.proxyIdA || pair.proxyIdB != primaryPair.proxyIdB) {
                break;
            }
            ++i;
        }
    }
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Aggregations

AABB (com.almasb.fxgl.physics.box2d.collision.AABB)11 Vec2 (com.almasb.fxgl.core.math.Vec2)5 Shape (com.almasb.fxgl.physics.box2d.collision.shapes.Shape)1 Transform (com.almasb.fxgl.physics.box2d.common.Transform)1