com.jme3.collision.Collidable Java Examples

The following examples show how to use com.jme3.collision.Collidable. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BoundingSphere.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray, results);
    } else if (other instanceof Triangle){
        Triangle t = (Triangle) other;
        return collideWithTri(t, results);
    } else if (other instanceof BoundingVolume) {
        if (intersects((BoundingVolume)other)) {
            CollisionResult result = new CollisionResult();
            results.addCollision(result);
            return 1;
        }
        return 0;
    } else if (other instanceof Spatial) {
        return other.collideWith(this, results);
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #2
Source File: BoundingSphere.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) {
    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray, results);
    } else if (other instanceof Triangle){
        Triangle t = (Triangle) other;
        
        float r2 = radius * radius;
        float d1 = center.distanceSquared(t.get1());
        float d2 = center.distanceSquared(t.get2());
        float d3 = center.distanceSquared(t.get3());
        
        if (d1 <= r2 || d2 <= r2 || d3 <= r2) {
            CollisionResult r = new CollisionResult();
            r.setDistance(FastMath.sqrt(Math.min(Math.min(d1, d2), d3)) - radius);
            results.addCollision(r);
            return 1;
        }

        return 0;
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #3
Source File: BoundingBox.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) {
    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray, results);
    } else if (other instanceof Triangle) {
        Triangle t = (Triangle) other;
        if (intersects(t.get1(), t.get2(), t.get3())) {
            CollisionResult r = new CollisionResult();
            results.addCollision(r);
            return 1;
        }
        return 0;
    } else {
        throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
    }
}
 
Example #4
Source File: Mesh.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Handles collision detection, internal use only.
 * User code should only use collideWith() on scene
 * graph elements such as {@link Spatial}s.
 */
public int collideWith(Collidable other, 
                       Matrix4f worldMatrix,
                       BoundingVolume worldBound,
                       CollisionResults results){

    if (getVertexCount() == 0) {
        return 0;
    }
    
    if (collisionTree == null){
        createCollisionData();
    }
    
    return collisionTree.collideWith(other, worldMatrix, worldBound, results);
}
 
Example #5
Source File: Geometry.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) {
    // Force bound to update
    checkDoBoundUpdate();
    // Update transform, and compute cached world matrix
    computeWorldMatrix();

    assert (refreshFlags & (RF_BOUND | RF_TRANSFORM)) == 0;

    if (mesh != null) {
        // NOTE: BIHTree in mesh already checks collision with the
        // mesh's bound
        int prevSize = results.size();
        int added = mesh.collideWith(other, cachedWorldMat, worldBound, results);
        int newSize = results.size();
        for (int i = prevSize; i < newSize; i++) {
            results.getCollisionDirect(i).setGeometry(this);
        }
        return added;
    }
    return 0;
}
 
Example #6
Source File: Ray.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) {
    if (other instanceof BoundingVolume) {
        BoundingVolume bv = (BoundingVolume) other;
        return bv.collideWith(this, results);
    } else if (other instanceof AbstractTriangle) {
        AbstractTriangle tri = (AbstractTriangle) other;
        float d = intersects(tri.get1(), tri.get2(), tri.get3());
        if (Float.isInfinite(d) || Float.isNaN(d)) {
            return 0;
        }

        Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin);
        results.addCollision(new CollisionResult(point, d));
        return 1;
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #7
Source File: TerrainQuad.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results){
    int total = 0;

    if (other instanceof Ray)
        return collideWithRay((Ray)other, results);

    // if it didn't collide with this bbox, return
    if (other instanceof BoundingVolume)
        if (!this.getWorldBound().intersects((BoundingVolume)other))
            return total;

    for (Spatial child : children){
        total += child.collideWith(other, results);
    }
    return total;
}
 
Example #8
Source File: TerrainPatch.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
    if (refreshFlags != 0)
        throw new IllegalStateException("Scene graph must be updated" +
                                        " before checking collision");

    if (other instanceof BoundingVolume)
        if (!getWorldBound().intersects((BoundingVolume)other))
            return 0;
    
    if(other instanceof Ray)
        return collideWithRay((Ray)other, results);
    else if (other instanceof BoundingVolume)
        return collideWithBoundingVolume((BoundingVolume)other, results);
    else {
        throw new UnsupportedCollisionException("TerrainPatch cannnot collide with "+other.getClass().getName());
    }
}
 
Example #9
Source File: BoundingBox.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other) {
    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray);
    } else if (other instanceof Triangle) {
        Triangle t = (Triangle) other;
        if (intersects(t.get1(), t.get2(), t.get3())) {
            return 1;
        }
        return 0;
    } else if (other instanceof BoundingVolume) {
        return intersects((BoundingVolume) other) ? 1 : 0;
    } else {
        throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
    }
}
 
Example #10
Source File: Geometry.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    // Force bound to update
    checkDoBoundUpdate();
    // Update transform, and compute cached world matrix
    computeWorldMatrix();

    assert (refreshFlags & (RF_BOUND | RF_TRANSFORM)) == 0;

    if (mesh != null) {
        // NOTE: BIHTree in mesh already checks collision with the
        // mesh's bound
        int prevSize = results.size();
        int added = mesh.collideWith(other, cachedWorldMat, worldBound, results);
        int newSize = results.size();
        for (int i = prevSize; i < newSize; i++) {
            results.getCollisionDirect(i).setGeometry(this);
        }
        return added;
    }
    return 0;
}
 
Example #11
Source File: TerrainPatch.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
    if (refreshFlags != 0)
        throw new IllegalStateException("Scene graph must be updated" +
                                        " before checking collision");

    if (other instanceof BoundingVolume)
        if (!getWorldBound().intersects((BoundingVolume)other))
            return 0;

    if(other instanceof Ray)
        return collideWithRay((Ray)other, results);
    else if (other instanceof BoundingVolume)
        return collideWithBoundingVolume((BoundingVolume)other, results);
    else {
        throw new UnsupportedCollisionException("TerrainPatch cannot collide with "+other.getClass().getName());
    }
}
 
Example #12
Source File: TerrainQuad.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results){
    int total = 0;

    if (other instanceof Ray)
        return collideWithRay((Ray)other, results);

    // if it didn't collide with this bbox, return
    if (other instanceof BoundingVolume)
        if (!this.getWorldBound().intersects((BoundingVolume)other))
            return total;

    for (Spatial child : children){
        total += child.collideWith(other, results);
    }
    return total;
}
 
Example #13
Source File: Ray.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    if (other instanceof BoundingVolume) {
        BoundingVolume bv = (BoundingVolume) other;
        return bv.collideWith(this, results);
    } else if (other instanceof AbstractTriangle) {
        AbstractTriangle tri = (AbstractTriangle) other;
        float d = intersects(tri.get1(), tri.get2(), tri.get3());
        if (Float.isInfinite(d) || Float.isNaN(d)) {
            return 0;
        }

        Vector3f point = new Vector3f(direction).multLocal(d).addLocal(origin);
        results.addCollision(new CollisionResult(point, d));
        return 1;
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #14
Source File: BIHTree.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public int collideWith(Collidable other,
        Matrix4f worldMatrix,
        BoundingVolume worldBound,
        CollisionResults results) {

    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray, worldMatrix, worldBound, results);
    } else if (other instanceof BoundingVolume) {
        BoundingVolume bv = (BoundingVolume) other;
        return collideWithBoundingVolume(bv, worldMatrix, results);
    } else {
        throw new UnsupportedCollisionException("Collidable:" + other);
    }
}
 
Example #15
Source File: BatchNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    int total = 0;
    for (Spatial child : children.getArray()) {
        if (!isBatch(child)) {
            total += child.collideWith(other, results);
        }
    }
    return total;
}
 
Example #16
Source File: Mesh.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Handles collision detection, internal use only.
 * User code should only use collideWith() on scene
 * graph elements such as {@link Spatial}s.
 */
public int collideWith(Collidable other,
        Matrix4f worldMatrix,
        BoundingVolume worldBound,
        CollisionResults results) {

    switch (mode) {
        case Points:
        case Lines:
        case LineStrip:
        case LineLoop:
            /*
             * Collisions can be detected only with triangles,
             * and there are no triangles in this mesh.
             */
            return 0;
    }

    if (getVertexCount() == 0) {
        return 0;
    }

    if (collisionTree == null) {
        createCollisionData();
    }

    return collisionTree.collideWith(other, worldMatrix, worldBound, results);
}
 
Example #17
Source File: BoundingVolume.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public int collideWith(Collidable other) {
    TempVars tempVars = TempVars.get();
    try {
        CollisionResults tempResults = tempVars.collisionResults;
        tempResults.clear();
        return collideWith(other, tempResults);
    } finally {
        tempVars.release();
    }
}
 
Example #18
Source File: Node.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results){
    int total = 0;
    for (Spatial child : children.getArray()){
        total += child.collideWith(other, results);
    }
    return total;
}
 
Example #19
Source File: BoundingSphere.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public int collideWith(Collidable other) {
    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray);
    } else if (other instanceof Triangle){
        return super.collideWith(other);
    } else if (other instanceof BoundingVolume) {
        return intersects((BoundingVolume)other) ? 1 : 0;
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #20
Source File: BIHTree.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public int collideWith(Collidable other,
        Matrix4f worldMatrix,
        BoundingVolume worldBound,
        CollisionResults results) {

    if (other instanceof Ray) {
        Ray ray = (Ray) other;
        return collideWithRay(ray, worldMatrix, worldBound, results);
    } else if (other instanceof BoundingVolume) {
        BoundingVolume bv = (BoundingVolume) other;
        return collideWithBoundingVolume(bv, worldMatrix, results);
    } else {
        throw new UnsupportedCollisionException();
    }
}
 
Example #21
Source File: PickEventSession.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public RootEntry( Collidable root, ViewPort viewport, String layer ) {
    this.viewport = viewport;
    this.root = root;
    this.layer = layer;
}
 
Example #22
Source File: CollisionData.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int collideWith(Collidable other,
Matrix4f worldMatrix,
BoundingVolume worldBound,
CollisionResults results);
 
Example #23
Source File: BatchedGeometry.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
    throw new UnsupportedOperationException("Not supported yet.");
}
 
Example #24
Source File: BIHNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }
 
Example #25
Source File: AbstractTriangle.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results){
    return other.collideWith(this, results);
}
 
Example #26
Source File: BIHNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final int intersectWhere(Collidable col,
            BoundingBox box,
            Matrix4f worldMatrix,
            BIHTree tree,
            CollisionResults results) {

        TempVars vars = TempVars.get();
        ArrayList<BIHStackData> stack = vars.bihStack;
        stack.clear();

        float[] minExts = {box.getCenter().x - box.getXExtent(),
            box.getCenter().y - box.getYExtent(),
            box.getCenter().z - box.getZExtent()};

        float[] maxExts = {box.getCenter().x + box.getXExtent(),
            box.getCenter().y + box.getYExtent(),
            box.getCenter().z + box.getZExtent()};

        stack.add(new BIHStackData(this, 0, 0));

        Triangle t = new Triangle();
        int cols = 0;

        stackloop:
        while (stack.size() > 0) {
            BIHNode node = stack.remove(stack.size() - 1).node;

            while (node.axis != 3) {
                int a = node.axis;

                float maxExt = maxExts[a];
                float minExt = minExts[a];

                if (node.leftPlane < node.rightPlane) {
                    // means there's a gap in the middle
                    // if the box is in that gap, we stop there
                    if (minExt > node.leftPlane
                            && maxExt < node.rightPlane) {
                        continue stackloop;
                    }
                }

                if (maxExt < node.rightPlane) {
                    node = node.left;
                } else if (minExt > node.leftPlane) {
                    node = node.right;
                } else {
                    stack.add(new BIHStackData(node.right, 0, 0));
                    node = node.left;
                }
//                if (maxExt < node.leftPlane
//                 && maxExt < node.rightPlane){
//                    node = node.left;
//                }else if (minExt > node.leftPlane
//                       && minExt > node.rightPlane){
//                    node = node.right;
//                }else{

//                }
            }

            for (int i = node.leftIndex; i <= node.rightIndex; i++) {
                tree.getTriangle(i, t.get1(), t.get2(), t.get3());
                if (worldMatrix != null) {
                    worldMatrix.mult(t.get1(), t.get1());
                    worldMatrix.mult(t.get2(), t.get2());
                    worldMatrix.mult(t.get3(), t.get3());
                }

                int added = col.collideWith(t, results);

                if (added > 0) {
                    int index = tree.getTriangleIndex(i);
                    int start = results.size() - added;

                    for (int j = start; j < results.size(); j++) {
                        CollisionResult cr = results.getCollisionDirect(j);
                        cr.setTriangleIndex(index);
                    }

                    cols += added;
                }
            }
        }
        vars.release();
        return cols;
    }
 
Example #27
Source File: BlenderKey.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public int collideWith(Collidable other, CollisionResults results) throws UnsupportedCollisionException {
    return 0;
}
 
Example #28
Source File: PickEventSession.java    From Lemur with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
protected PickEventSession( Map<Collidable, RootEntry> roots ) {
    this.roots.putAll(roots);
    this.rootList = null;
}
 
Example #29
Source File: ArmatureDebugger.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    return armatureNode.collideWith(other, results);
}
 
Example #30
Source File: Node.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public int collideWith(Collidable other, CollisionResults results) {
    int total = 0;
    // optimization: try collideWith BoundingVolume to avoid possibly redundant tests on children
    // number 4 in condition is somewhat arbitrary.
    // When there is only one child, the boundingVolume test is redundant at all.
    // The idea is when there are few children,
    // it can be too expensive to test boundingVolume first.
    /*
    I'm removing this change until some issues can be addressed and I really
    think it needs to be implemented a better way anyway.
    First, it causes issues for anyone doing collideWith() with BoundingVolumes
    and expecting it to trickle down to the children.  For example, children
    with BoundingSphere bounding volumes and collideWith(BoundingSphere).  Doing
    a collision check at the parent level then has to do a BoundingSphere to BoundingBox
    collision which isn't resolved.  (Having to come up with a collision point in that
    case is tricky and the first sign that this is the wrong approach.)
    Second, the rippling changes this caused to 'optimize' collideWith() for this
    special use-case are another sign that this approach was a bit dodgy.  The whole
    idea of calculating a full collision just to see if the two shapes collide at all
    is very wasteful.
    A proper implementation should support a simpler boolean check that doesn't do
    all of that calculation.  For example, if 'other' is also a BoundingVolume (ie: 99.9%
    of all non-Ray cases) then a direct BV to BV intersects() test can be done.  So much
    faster.  And if 'other' _is_ a Ray then the BV.intersects(Ray) call can be done.
    I don't have time to do it right now but I'll at least un-break a bunch of peoples'
    code until it can be 'optimized' properly.  Hopefully it's not too late to back out
    the other dodgy ripples this caused.  -pspeed (hindsight-expert ;))
    Note: the code itself is relatively simple to implement but I don't have time to
    a) test it, and b) see if '> 4' is still a decent check for it.  Could be it's fast
    enough to do all the time for > 1.
    if (children.size() > 4)
    {
      BoundingVolume bv = this.getWorldBound();
      if (bv==null) return 0;

      // collideWith without CollisionResults parameter used to avoid allocation when possible
      if (bv.collideWith(other) == 0) return 0;
    }
    */
    for (Spatial child : children.getArray()) {
        total += child.collideWith(other, results);
    }
    return total;
}