Search in sources :

Example 11 with CollisionResult

use of com.jme3.collision.CollisionResult 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 12 with CollisionResult

use of com.jme3.collision.CollisionResult 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 13 with CollisionResult

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

the class BresenhamTerrainPicker method getTerrainIntersection.

public Vector3f getTerrainIntersection(Ray worldPick, CollisionResults results) {
    worldPickRay.set(worldPick);
    List<TerrainPickData> pickData = new ArrayList<TerrainPickData>();
    root.findPick(worldPick.clone(), pickData);
    Collections.sort(pickData);
    if (pickData.isEmpty())
        return null;
    workRay.set(worldPick);
    for (TerrainPickData pd : pickData) {
        TerrainPatch patch = pd.targetPatch;
        tracer.getGridSpacing().set(patch.getWorldScale());
        tracer.setGridOrigin(patch.getWorldTranslation());
        workRay.getOrigin().set(worldPick.getDirection()).multLocal(pd.cr.getDistance() - .1f).addLocal(worldPick.getOrigin());
        tracer.startWalk(workRay);
        final Vector3f intersection = new Vector3f();
        final Vector2f loc = tracer.getGridLocation();
        if (tracer.isRayPerpendicularToGrid()) {
            Triangle hit = new Triangle();
            checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit);
            float distance = worldPickRay.origin.distance(intersection);
            CollisionResult cr = new CollisionResult(intersection, distance);
            cr.setGeometry(patch);
            cr.setContactNormal(hit.getNormal());
            results.addCollision(cr);
            return intersection;
        }
        while (loc.x >= -1 && loc.x <= patch.getSize() && loc.y >= -1 && loc.y <= patch.getSize()) {
            //System.out.print(loc.x+","+loc.y+" : ");
            // check the triangles of main square for intersection.
            Triangle hit = new Triangle();
            if (checkTriangles(loc.x, loc.y, workRay, intersection, patch, hit)) {
                // we found an intersection, so return that!
                float distance = worldPickRay.origin.distance(intersection);
                CollisionResult cr = new CollisionResult(intersection, distance);
                cr.setGeometry(patch);
                results.addCollision(cr);
                cr.setContactNormal(hit.getNormal());
                return intersection;
            }
            // because of how we get our height coords, we will
            // sometimes be off by a grid spot, so we check the next
            // grid space up.
            int dx = 0, dz = 0;
            Direction d = tracer.getLastStepDirection();
            switch(d) {
                case PositiveX:
                case NegativeX:
                    dx = 0;
                    dz = 1;
                    break;
                case PositiveZ:
                case NegativeZ:
                    dx = 1;
                    dz = 0;
                    break;
            }
            if (checkTriangles(loc.x + dx, loc.y + dz, workRay, intersection, patch, hit)) {
                // we found an intersection, so return that!
                float distance = worldPickRay.origin.distance(intersection);
                CollisionResult cr = new CollisionResult(intersection, distance);
                results.addCollision(cr);
                cr.setGeometry(patch);
                cr.setContactNormal(hit.getNormal());
                return intersection;
            }
            tracer.next();
        }
    }
    return null;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) ArrayList(java.util.ArrayList) Triangle(com.jme3.math.Triangle) Direction(com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction) TerrainPatch(com.jme3.terrain.geomipmap.TerrainPatch)

Aggregations

CollisionResult (com.jme3.collision.CollisionResult)13 TempVars (com.jme3.util.TempVars)6 Vector3f (com.jme3.math.Vector3f)5 CollisionResults (com.jme3.collision.CollisionResults)4 Ray (com.jme3.math.Ray)3 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)2 Triangle (com.jme3.math.Triangle)2 Vector2f (com.jme3.math.Vector2f)2 Spatial (com.jme3.scene.Spatial)2 Matrix4f (com.jme3.math.Matrix4f)1 Quaternion (com.jme3.math.Quaternion)1 Node (com.jme3.scene.Node)1 TerrainPatch (com.jme3.terrain.geomipmap.TerrainPatch)1 Direction (com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction)1 ArrayList (java.util.ArrayList)1