Search in sources :

Example 11 with AABB

use of org.jbox2d.collision.AABB in project libgdx by libgdx.

the class DefaultBroadPhaseBuffer method testOverlap.

@Override
public boolean testOverlap(int proxyIdA, int proxyIdB) {
    // return AABB.testOverlap(proxyA.aabb, proxyB.aabb);
    // return m_tree.overlap(proxyIdA, proxyIdB);
    final AABB a = m_tree.getFatAABB(proxyIdA);
    final AABB b = m_tree.getFatAABB(proxyIdB);
    if (b.lowerBound.x - a.upperBound.x > 0.0f || b.lowerBound.y - a.upperBound.y > 0.0f) {
        return false;
    }
    if (a.lowerBound.x - b.upperBound.x > 0.0f || a.lowerBound.y - b.upperBound.y > 0.0f) {
        return false;
    }
    return true;
}
Also used : AABB(org.jbox2d.collision.AABB)

Example 12 with AABB

use of org.jbox2d.collision.AABB in project libgdx by libgdx.

the class DefaultBroadPhaseBuffer method updatePairs.

@Override
public final void updatePairs(PairCallback callback) {
    // Reset pair buffer
    m_pairCount = 0;
    // Perform tree queries for all moving proxies.
    for (int i = 0; i < m_moveCount; ++i) {
        m_queryProxyId = m_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 = m_tree.getFatAABB(m_queryProxyId);
        // Query tree, create pairs and add them pair buffer.
        // log.debug("quering aabb: "+m_queryProxy.aabb);
        m_tree.query(this, fatAABB);
    }
    // log.debug("Number of pairs found: "+m_pairCount);
    // Reset move buffer
    m_moveCount = 0;
    // Sort the pair buffer to expose duplicates.
    Arrays.sort(m_pairBuffer, 0, m_pairCount);
    // Send the pairs back to the client.
    int i = 0;
    while (i < m_pairCount) {
        Pair primaryPair = m_pairBuffer[i];
        Object userDataA = m_tree.getUserData(primaryPair.proxyIdA);
        Object userDataB = m_tree.getUserData(primaryPair.proxyIdB);
        // log.debug("returning pair: "+userDataA+", "+userDataB);
        callback.addPair(userDataA, userDataB);
        ++i;
        // Skip any duplicate pairs.
        while (i < m_pairCount) {
            Pair pair = m_pairBuffer[i];
            if (pair.proxyIdA != primaryPair.proxyIdA || pair.proxyIdB != primaryPair.proxyIdB) {
                break;
            }
            ++i;
        }
    }
}
Also used : AABB(org.jbox2d.collision.AABB)

Example 13 with AABB

use of org.jbox2d.collision.AABB in project libgdx by libgdx.

the class DynamicTreeFlatNodes method raycast.

@Override
public void raycast(TreeRayCastCallback callback, RayCastInput input) {
    final Vec2 p1 = input.p1;
    final Vec2 p2 = input.p2;
    float p1x = p1.x, p2x = p2.x, p1y = p1.y, p2y = p2.y;
    float vx, vy;
    float rx, ry;
    float absVx, absVy;
    float cx, cy;
    float hx, hy;
    float tempx, tempy;
    r.x = p2x - p1x;
    r.y = p2y - p1y;
    assert ((r.x * r.x + r.y * r.y) > 0f);
    r.normalize();
    rx = r.x;
    ry = r.y;
    // v is perpendicular to the segment.
    vx = -1f * ry;
    vy = 1f * rx;
    absVx = MathUtils.abs(vx);
    absVy = MathUtils.abs(vy);
    // Separating axis for segment (Gino, p80).
    // |dot(v, p1 - c)| > dot(|v|, h)
    float maxFraction = input.maxFraction;
    // Build a bounding box for the segment.
    final AABB segAABB = aabb;
    // Vec2 t = p1 + maxFraction * (p2 - p1);
    // before inline
    // temp.set(p2).subLocal(p1).mulLocal(maxFraction).addLocal(p1);
    // Vec2.minToOut(p1, temp, segAABB.lowerBound);
    // Vec2.maxToOut(p1, temp, segAABB.upperBound);
    tempx = (p2x - p1x) * maxFraction + p1x;
    tempy = (p2y - p1y) * maxFraction + p1y;
    segAABB.lowerBound.x = p1x < tempx ? p1x : tempx;
    segAABB.lowerBound.y = p1y < tempy ? p1y : tempy;
    segAABB.upperBound.x = p1x > tempx ? p1x : tempx;
    segAABB.upperBound.y = p1y > tempy ? p1y : tempy;
    // end inline
    nodeStackIndex = 0;
    nodeStack[nodeStackIndex++] = m_root;
    while (nodeStackIndex > 0) {
        int node = nodeStack[--nodeStackIndex] = m_root;
        if (node == NULL_NODE) {
            continue;
        }
        final AABB nodeAABB = m_aabb[node];
        if (!AABB.testOverlap(nodeAABB, segAABB)) {
            continue;
        }
        // Separating axis for segment (Gino, p80).
        // |dot(v, p1 - c)| > dot(|v|, h)
        // node.aabb.getCenterToOut(c);
        // node.aabb.getExtentsToOut(h);
        cx = (nodeAABB.lowerBound.x + nodeAABB.upperBound.x) * .5f;
        cy = (nodeAABB.lowerBound.y + nodeAABB.upperBound.y) * .5f;
        hx = (nodeAABB.upperBound.x - nodeAABB.lowerBound.x) * .5f;
        hy = (nodeAABB.upperBound.y - nodeAABB.lowerBound.y) * .5f;
        tempx = p1x - cx;
        tempy = p1y - cy;
        float separation = MathUtils.abs(vx * tempx + vy * tempy) - (absVx * hx + absVy * hy);
        if (separation > 0.0f) {
            continue;
        }
        int child1 = m_child1[node];
        if (child1 == NULL_NODE) {
            subInput.p1.x = p1x;
            subInput.p1.y = p1y;
            subInput.p2.x = p2x;
            subInput.p2.y = p2y;
            subInput.maxFraction = maxFraction;
            float value = callback.raycastCallback(subInput, node);
            if (value == 0.0f) {
                // The client has terminated the ray cast.
                return;
            }
            if (value > 0.0f) {
                // Update segment bounding box.
                maxFraction = value;
                // temp.set(p2).subLocal(p1).mulLocal(maxFraction).addLocal(p1);
                // Vec2.minToOut(p1, temp, segAABB.lowerBound);
                // Vec2.maxToOut(p1, temp, segAABB.upperBound);
                tempx = (p2x - p1x) * maxFraction + p1x;
                tempy = (p2y - p1y) * maxFraction + p1y;
                segAABB.lowerBound.x = p1x < tempx ? p1x : tempx;
                segAABB.lowerBound.y = p1y < tempy ? p1y : tempy;
                segAABB.upperBound.x = p1x > tempx ? p1x : tempx;
                segAABB.upperBound.y = p1y > tempy ? p1y : tempy;
            }
        } else {
            nodeStack[nodeStackIndex++] = child1;
            nodeStack[nodeStackIndex++] = m_child2[node];
        }
    }
}
Also used : Vec2(org.jbox2d.common.Vec2) AABB(org.jbox2d.collision.AABB)

Example 14 with AABB

use of org.jbox2d.collision.AABB in project libgdx by libgdx.

the class DynamicTree method moveProxy.

@Override
public final boolean moveProxy(int proxyId, final AABB aabb, Vec2 displacement) {
    assert (aabb.isValid());
    assert (0 <= proxyId && proxyId < m_nodeCapacity);
    final DynamicTreeNode node = m_nodes[proxyId];
    assert (node.child1 == null);
    final AABB nodeAABB = node.aabb;
    // if (nodeAABB.contains(aabb)) {
    if (nodeAABB.lowerBound.x <= aabb.lowerBound.x && nodeAABB.lowerBound.y <= aabb.lowerBound.y && aabb.upperBound.x <= nodeAABB.upperBound.x && aabb.upperBound.y <= nodeAABB.upperBound.y) {
        return false;
    }
    removeLeaf(node);
    // Extend AABB
    final Vec2 lowerBound = nodeAABB.lowerBound;
    final Vec2 upperBound = nodeAABB.upperBound;
    lowerBound.x = aabb.lowerBound.x - Settings.aabbExtension;
    lowerBound.y = aabb.lowerBound.y - Settings.aabbExtension;
    upperBound.x = aabb.upperBound.x + Settings.aabbExtension;
    upperBound.y = aabb.upperBound.y + Settings.aabbExtension;
    // Predict AABB displacement.
    final float dx = displacement.x * Settings.aabbMultiplier;
    final float dy = displacement.y * Settings.aabbMultiplier;
    if (dx < 0.0f) {
        lowerBound.x += dx;
    } else {
        upperBound.x += dx;
    }
    if (dy < 0.0f) {
        lowerBound.y += dy;
    } else {
        upperBound.y += dy;
    }
    insertLeaf(proxyId);
    return true;
}
Also used : Vec2(org.jbox2d.common.Vec2) AABB(org.jbox2d.collision.AABB)

Example 15 with AABB

use of org.jbox2d.collision.AABB in project libgdx by libgdx.

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 + MathUtils.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(org.jbox2d.collision.AABB)

Aggregations

AABB (org.jbox2d.collision.AABB)21 Vec2 (org.jbox2d.common.Vec2)8 DynamicTree (org.jbox2d.collision.broadphase.DynamicTree)1 Shape (org.jbox2d.collision.shapes.Shape)1 Transform (org.jbox2d.common.Transform)1 Contact (org.jbox2d.dynamics.contacts.Contact)1 Joint (org.jbox2d.dynamics.joints.Joint)1 PulleyJoint (org.jbox2d.dynamics.joints.PulleyJoint)1 ParticleBodyContact (org.jbox2d.particle.ParticleBodyContact)1 ParticleContact (org.jbox2d.particle.ParticleContact)1 Test (org.junit.Test)1