Java Code Examples for com.jme3.collision.CollisionResults#addCollision()

The following examples show how to use com.jme3.collision.CollisionResults#addCollision() . 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: BresenhamTerrainPicker.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * This method adds the found Collision to an existing collisionResult.
 * @param results The results to add this collision to
 * @param patch The TerrainPatch which collided
 * @param intersection The actual intersection position
 * @param hit The hit triangle
 * @param distance The distance at which the hit occurred
 * @return Whether the collision was accepted to the list or whether it has been deduplicated
 */
private boolean addCollision(CollisionResults results, TerrainPatch patch, Vector3f intersection, Triangle hit, float distance) {
    CollisionResult cr = new CollisionResult(intersection.clone(), distance);
    cr.setGeometry(patch);
    cr.setContactNormal(hit.getNormal());
    cr.setTriangleIndex(hit.getIndex()); // this will probably always be 0

    for (int i = 0; i < results.size(); i++) {
        CollisionResult compare = results.getCollision(i);
        if (compare.getDistance() == cr.getDistance() && compare.getGeometry() == cr.getGeometry() &&
            compare.getContactPoint().equals(cr.getContactPoint()) &&
            compare.getContactNormal().equals(cr.getContactNormal())) {
                return false; // Collision already available, deduplicate.
        }
    }

    results.addCollision(cr);
    return true;
}
 
Example 2
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 3
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 4
Source File: Octnode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public final void intersectWhere(Ray r, Geometry[] geoms, float sceneMin, float sceneMax,
                                        CollisionResults results){
    for (OCTTriangle t : tris){
        float d = r.intersects(t.get1(), t.get2(), t.get3());
        if (Float.isInfinite(d))
            continue;

        Vector3f contactPoint = new Vector3f(r.getDirection()).multLocal(d).addLocal(r.getOrigin());
        CollisionResult result = new CollisionResult(geoms[t.getGeometryIndex()],
                                                     contactPoint,
                                                     d,
                                                     t.getTriangleIndex());
        results.addCollision(result);
    }
    for (int i = 0; i < 8; i++){
        Octnode child = children[i];
        if (child == null)
            continue;

        if (child.bbox.intersects(r)){
            child.intersectWhere(r, geoms, sceneMin, sceneMax, results);
        }
    }
}
 
Example 5
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 6
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 7
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 8
Source File: BIHNode.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public final int intersectBrute(Ray r,
        Matrix4f worldMatrix,
        BIHTree tree,
        float sceneMin,
        float sceneMax,
        CollisionResults results) {
    float tHit = Float.POSITIVE_INFINITY;
    
    TempVars vars = TempVars.get();

    Vector3f v1 = vars.vect1,
            v2 = vars.vect2,
            v3 = vars.vect3;

    int cols = 0;

    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    stack.add(new BIHStackData(this, 0, 0));
    stackloop:
    while (stack.size() > 0) {

        BIHStackData data = stack.remove(stack.size() - 1);
        BIHNode node = data.node;

        leafloop:
        while (node.axis != 3) { // while node is not a leaf
            BIHNode nearNode, farNode;
            nearNode = node.left;
            farNode = node.right;

            stack.add(new BIHStackData(farNode, 0, 0));
            node = nearNode;
        }

        // a leaf
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, v1, v2, v3);

            if (worldMatrix != null) {
                worldMatrix.mult(v1, v1);
                worldMatrix.mult(v2, v2);
                worldMatrix.mult(v3, v3);
            }

            float t = r.intersects(v1, v2, v3);
            if (t < tHit) {
                tHit = t;
                Vector3f contactPoint = new Vector3f(r.direction).multLocal(tHit).addLocal(r.origin);
                CollisionResult cr = new CollisionResult(contactPoint, tHit);
                cr.setTriangleIndex(tree.getTriangleIndex(i));
                results.addCollision(cr);
                cols++;
            }
        }
    }
    vars.release();
    return cols;
}
 
Example 9
Source File: BIHNode.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public final int intersectBrute(Ray r,
        Matrix4f worldMatrix,
        BIHTree tree,
        float sceneMin,
        float sceneMax,
        CollisionResults results) {
    float tHit = Float.POSITIVE_INFINITY;
    
    TempVars vars = TempVars.get();

    Vector3f v1 = vars.vect1,
            v2 = vars.vect2,
            v3 = vars.vect3;

    int cols = 0;

    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    stack.add(new BIHStackData(this, 0, 0));
    stackloop:
    while (stack.size() > 0) {

        BIHStackData data = stack.remove(stack.size() - 1);
        BIHNode node = data.node;

        leafloop:
        while (node.axis != 3) { // while node is not a leaf
            BIHNode nearNode, farNode;
            nearNode = node.left;
            farNode = node.right;

            stack.add(new BIHStackData(farNode, 0, 0));
            node = nearNode;
        }

        // a leaf
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, v1, v2, v3);

            if (worldMatrix != null) {
                worldMatrix.mult(v1, v1);
                worldMatrix.mult(v2, v2);
                worldMatrix.mult(v3, v3);
            }

            float t = r.intersects(v1, v2, v3);
            if (t < tHit) {
                tHit = t;
                Vector3f contactPoint = new Vector3f(r.direction).multLocal(tHit).addLocal(r.origin);
                CollisionResult cr = new CollisionResult(contactPoint, tHit);
                cr.setTriangleIndex(tree.getTriangleIndex(i));
                results.addCollision(cr);
                cols++;
            }
        }
    }
    vars.release();
    return cols;
}