Search in sources :

Example 21 with CollisionResults

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

the class TerrainQuad method findPick.

/**
     * Gather the terrain patches that intersect the given ray (toTest).
     * This only tests the bounding boxes
     * @param toTest
     * @param results
     */
public void findPick(Ray toTest, List<TerrainPickData> results) {
    if (getWorldBound() != null) {
        if (getWorldBound().intersects(toTest)) {
            // further checking needed.
            for (int i = 0; i < getQuantity(); i++) {
                if (children.get(i) instanceof TerrainPatch) {
                    TerrainPatch tp = (TerrainPatch) children.get(i);
                    tp.ensurePositiveVolumeBBox();
                    if (tp.getWorldBound().intersects(toTest)) {
                        CollisionResults cr = new CollisionResults();
                        toTest.collideWith(tp.getWorldBound(), cr);
                        if (cr != null && cr.getClosestCollision() != null) {
                            cr.getClosestCollision().getDistance();
                            results.add(new TerrainPickData(tp, cr.getClosestCollision()));
                        }
                    }
                } else if (children.get(i) instanceof TerrainQuad) {
                    ((TerrainQuad) children.get(i)).findPick(toTest, results);
                }
            }
        }
    }
}
Also used : TerrainPickData(com.jme3.terrain.geomipmap.picking.TerrainPickData) CollisionResults(com.jme3.collision.CollisionResults)

Example 22 with CollisionResults

use of com.jme3.collision.CollisionResults 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 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