Search in sources :

Example 16 with CollisionResults

use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.

the class TestDepthOfField method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f);
    Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f);
    direction.subtractLocal(origin).normalizeLocal();
    Ray ray = new Ray(origin, direction);
    CollisionResults results = new CollisionResults();
    int numCollisions = terrain.collideWith(ray, results);
    if (numCollisions > 0) {
        CollisionResult hit = results.getClosestCollision();
        fpsText.setText("" + hit.getDistance());
        dofFilter.setFocusDistance(hit.getDistance() / 10.0f);
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) CollisionResults(com.jme3.collision.CollisionResults)

Example 17 with CollisionResults

use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.

the class BIHTree method collideWithRay.

private int collideWithRay(Ray r, Matrix4f worldMatrix, BoundingVolume worldBound, CollisionResults results) {
    TempVars vars = TempVars.get();
    try {
        CollisionResults boundResults = vars.collisionResults;
        boundResults.clear();
        worldBound.collideWith(r, boundResults);
        if (boundResults.size() > 0) {
            float tMin = boundResults.getClosestCollision().getDistance();
            float tMax = boundResults.getFarthestCollision().getDistance();
            if (tMax <= 0) {
                tMax = Float.POSITIVE_INFINITY;
            } else if (tMin == tMax) {
                tMin = 0;
            }
            if (tMin <= 0) {
                tMin = 0;
            }
            if (r.getLimit() < Float.POSITIVE_INFINITY) {
                tMax = Math.min(tMax, r.getLimit());
                if (tMin > tMax) {
                    return 0;
                }
            }
            //            return root.intersectBrute(r, worldMatrix, this, tMin, tMax, results);
            return root.intersectWhere(r, worldMatrix, this, tMin, tMax, results);
        }
        return 0;
    } finally {
        vars.release();
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) TempVars(com.jme3.util.TempVars)

Example 18 with CollisionResults

use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.

the class BIHNode method intersectWhere.

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) {
                // 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;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Triangle(com.jme3.math.Triangle) TempVars(com.jme3.util.TempVars)

Example 19 with CollisionResults

use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.

the class BIHTree method collideWithBoundingVolume.

private int collideWithBoundingVolume(BoundingVolume bv, Matrix4f worldMatrix, CollisionResults results) {
    BoundingBox bbox;
    if (bv instanceof BoundingSphere) {
        BoundingSphere sphere = (BoundingSphere) bv;
        bbox = new BoundingBox(bv.getCenter().clone(), sphere.getRadius(), sphere.getRadius(), sphere.getRadius());
    } else if (bv instanceof BoundingBox) {
        bbox = new BoundingBox((BoundingBox) bv);
    } else {
        throw new UnsupportedCollisionException("BoundingVolume:" + bv);
    }
    bbox.transform(worldMatrix.invert(), bbox);
    return root.intersectWhere(bv, bbox, worldMatrix, this, results);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) UnsupportedCollisionException(com.jme3.collision.UnsupportedCollisionException)

Example 20 with CollisionResults

use of com.jme3.collision.CollisionResults in project jmonkeyengine by jMonkeyEngine.

the class TestRayCollision method main.

public static void main(String[] args) {
    Ray r = new Ray(Vector3f.ZERO, Vector3f.UNIT_X);
    BoundingBox bbox = new BoundingBox(new Vector3f(5, 0, 0), 1, 1, 1);
    CollisionResults res = new CollisionResults();
    bbox.collideWith(r, res);
    System.out.println("Bounding:" + bbox);
    System.out.println("Ray: " + r);
    System.out.println("Num collisions: " + res.size());
    for (int i = 0; i < res.size(); i++) {
        System.out.println("--- Collision #" + i + " ---");
        float dist = res.getCollision(i).getDistance();
        Vector3f pt = res.getCollision(i).getContactPoint();
        System.out.println("distance: " + dist);
        System.out.println("point: " + pt);
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray)

Aggregations

CollisionResult (com.jme3.collision.CollisionResult)13 CollisionResults (com.jme3.collision.CollisionResults)11 Vector3f (com.jme3.math.Vector3f)8 TempVars (com.jme3.util.TempVars)8 Ray (com.jme3.math.Ray)6 BoundingBox (com.jme3.bounding.BoundingBox)3 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)3 BoundingVolume (com.jme3.bounding.BoundingVolume)2 Triangle (com.jme3.math.Triangle)2 Vector2f (com.jme3.math.Vector2f)2 Spatial (com.jme3.scene.Spatial)2 BoundingSphere (com.jme3.bounding.BoundingSphere)1 Matrix4f (com.jme3.math.Matrix4f)1 Quaternion (com.jme3.math.Quaternion)1 Node (com.jme3.scene.Node)1 VertexBuffer (com.jme3.scene.VertexBuffer)1 TerrainPatch (com.jme3.terrain.geomipmap.TerrainPatch)1 Direction (com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction)1 TerrainPickData (com.jme3.terrain.geomipmap.picking.TerrainPickData)1 FloatBuffer (java.nio.FloatBuffer)1