Search in sources :

Example 1 with AABB

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

the class ParticleSystem method updateBodyContacts.

private void updateBodyContacts() {
    final AABB aabb = temp;
    aabb.lowerBound.x = Float.MAX_VALUE;
    aabb.lowerBound.y = Float.MAX_VALUE;
    aabb.upperBound.x = -Float.MAX_VALUE;
    aabb.upperBound.y = -Float.MAX_VALUE;
    for (int i = 0; i < m_count; i++) {
        Vec2 p = m_positionBuffer.data[i];
        Vec2.minToOut(aabb.lowerBound, p, aabb.lowerBound);
        Vec2.maxToOut(aabb.upperBound, p, aabb.upperBound);
    }
    aabb.lowerBound.x -= m_particleDiameter;
    aabb.lowerBound.y -= m_particleDiameter;
    aabb.upperBound.x += m_particleDiameter;
    aabb.upperBound.y += m_particleDiameter;
    m_bodyContactCount = 0;
    ubccallback.system = this;
    m_world.queryAABB(ubccallback, aabb);
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 2 with AABB

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

the class Fixture method synchronize.

void synchronize(BroadPhase broadPhase, Transform transform1, Transform transform2) {
    if (proxyCount == 0) {
        return;
    }
    for (int i = 0; i < proxyCount; ++i) {
        FixtureProxy proxy = proxies[i];
        // Compute an AABB that covers the swept shape (may miss some rotation effect).
        AABB aabb1 = pool1;
        AABB aabb2 = pool2;
        shape.computeAABB(aabb1, transform1, proxy.childIndex);
        shape.computeAABB(aabb2, transform2, proxy.childIndex);
        proxy.aabb.combine(aabb1, aabb2);
        displacement.x = transform2.p.x - transform1.p.x;
        displacement.y = transform2.p.y - transform1.p.y;
        broadPhase.moveProxy(proxy.proxyId, proxy.aabb, displacement);
    }
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 3 with AABB

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

the class DynamicTree method insertLeaf.

private void insertLeaf(int leaf_index) {
    DynamicTreeNode leaf = m_nodes[leaf_index];
    if (root == null) {
        root = leaf;
        root.parent = null;
        return;
    }
    // find the best sibling
    AABB leafAABB = leaf.aabb;
    DynamicTreeNode index = root;
    while (index.child1 != null) {
        final DynamicTreeNode node = index;
        DynamicTreeNode child1 = node.child1;
        DynamicTreeNode child2 = node.child2;
        float area = node.aabb.getPerimeter();
        combinedAABB.combine(node.aabb, leafAABB);
        float combinedArea = combinedAABB.getPerimeter();
        // Cost of creating a new parent for this node and the new leaf
        float cost = 2.0f * combinedArea;
        // Minimum cost of pushing the leaf further down the tree
        float inheritanceCost = 2.0f * (combinedArea - area);
        // Cost of descending into child1
        float cost1;
        if (child1.child1 == null) {
            combinedAABB.combine(leafAABB, child1.aabb);
            cost1 = combinedAABB.getPerimeter() + inheritanceCost;
        } else {
            combinedAABB.combine(leafAABB, child1.aabb);
            float oldArea = child1.aabb.getPerimeter();
            float newArea = combinedAABB.getPerimeter();
            cost1 = (newArea - oldArea) + inheritanceCost;
        }
        // Cost of descending into child2
        float cost2;
        if (child2.child1 == null) {
            combinedAABB.combine(leafAABB, child2.aabb);
            cost2 = combinedAABB.getPerimeter() + inheritanceCost;
        } else {
            combinedAABB.combine(leafAABB, child2.aabb);
            float oldArea = child2.aabb.getPerimeter();
            float newArea = combinedAABB.getPerimeter();
            cost2 = newArea - oldArea + inheritanceCost;
        }
        // Descend according to the minimum cost.
        if (cost < cost1 && cost < cost2) {
            break;
        }
        // Descend
        if (cost1 < cost2) {
            index = child1;
        } else {
            index = child2;
        }
    }
    DynamicTreeNode sibling = index;
    DynamicTreeNode oldParent = m_nodes[sibling.id].parent;
    final DynamicTreeNode newParent = allocateNode();
    newParent.parent = oldParent;
    newParent.setUserData(null);
    newParent.aabb.combine(leafAABB, sibling.aabb);
    newParent.height = sibling.height + 1;
    if (oldParent != null) {
        // The sibling was not the root.
        if (oldParent.child1 == sibling) {
            oldParent.child1 = newParent;
        } else {
            oldParent.child2 = newParent;
        }
        newParent.child1 = sibling;
        newParent.child2 = leaf;
        sibling.parent = newParent;
        leaf.parent = newParent;
    } else {
        // The sibling was the root.
        newParent.child1 = sibling;
        newParent.child2 = leaf;
        sibling.parent = newParent;
        leaf.parent = newParent;
        root = newParent;
    }
    // Walk back up the tree fixing heights and AABBs
    index = leaf.parent;
    while (index != null) {
        index = balance(index);
        DynamicTreeNode child1 = index.child1;
        DynamicTreeNode child2 = index.child2;
        assert child1 != null;
        assert child2 != null;
        index.height = 1 + Math.max(child1.height, child2.height);
        index.aabb.combine(child1.aabb, child2.aabb);
        index = index.parent;
    }
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 4 with AABB

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

the class DynamicTree method createProxy.

@Override
public final int createProxy(final AABB aabb, Object userData) {
    final DynamicTreeNode node = allocateNode();
    int proxyId = node.id;
    // Fatten the aabb
    final AABB nodeAABB = node.aabb;
    nodeAABB.lowerBound.x = aabb.lowerBound.x - JBoxSettings.aabbExtension;
    nodeAABB.lowerBound.y = aabb.lowerBound.y - JBoxSettings.aabbExtension;
    nodeAABB.upperBound.x = aabb.upperBound.x + JBoxSettings.aabbExtension;
    nodeAABB.upperBound.y = aabb.upperBound.y + JBoxSettings.aabbExtension;
    node.setUserData(userData);
    insertLeaf(proxyId);
    return proxyId;
}
Also used : AABB(com.almasb.fxgl.physics.box2d.collision.AABB)

Example 5 with AABB

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

the class DynamicTree 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.getLengthAndNormalize();
    rx = r.x;
    ry = r.y;
    // v is perpendicular to the segment.
    vx = -1f * ry;
    vy = 1f * rx;
    absVx = FXGLMath.abs(vx);
    absVy = FXGLMath.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++] = root;
    while (nodeStackIndex > 0) {
        final DynamicTreeNode node = nodeStack[--nodeStackIndex];
        if (node == null) {
            continue;
        }
        final AABB nodeAABB = node.aabb;
        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 = FXGLMath.abs(vx * tempx + vy * tempy) - (absVx * hx + absVy * hy);
        if (separation > 0.0f) {
            continue;
        }
        if (node.child1 == null) {
            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.id);
            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 {
            if (nodeStack.length - nodeStackIndex - 2 <= 0) {
                DynamicTreeNode[] newBuffer = new DynamicTreeNode[nodeStack.length * 2];
                System.arraycopy(nodeStack, 0, newBuffer, 0, nodeStack.length);
                nodeStack = newBuffer;
            }
            nodeStack[nodeStackIndex++] = node.child1;
            nodeStack[nodeStackIndex++] = node.child2;
        }
    }
}
Also used : Vec2(com.almasb.fxgl.core.math.Vec2) 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