Search in sources :

Example 11 with CollisionResults

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

the class BIHNode method intersectWhere.

public final int intersectWhere(Ray r, Matrix4f worldMatrix, BIHTree tree, float sceneMin, float sceneMax, CollisionResults results) {
    TempVars vars = TempVars.get();
    ArrayList<BIHStackData> stack = vars.bihStack;
    stack.clear();
    //        float tHit = Float.POSITIVE_INFINITY;
    Vector3f o = vars.vect1.set(r.getOrigin());
    Vector3f d = vars.vect2.set(r.getDirection());
    Matrix4f inv = vars.tempMat4.set(worldMatrix).invertLocal();
    inv.mult(r.getOrigin(), r.getOrigin());
    // Fixes rotation collision bug
    inv.multNormal(r.getDirection(), r.getDirection());
    //        inv.multNormalAcross(r.getDirection(), r.getDirection());
    float[] origins = { r.getOrigin().x, r.getOrigin().y, r.getOrigin().z };
    float[] invDirections = { 1f / r.getDirection().x, 1f / r.getDirection().y, 1f / r.getDirection().z };
    r.getDirection().normalizeLocal();
    Vector3f v1 = vars.vect3, v2 = vars.vect4, v3 = vars.vect5;
    int cols = 0;
    stack.add(new BIHStackData(this, sceneMin, sceneMax));
    stackloop: while (stack.size() > 0) {
        BIHStackData data = stack.remove(stack.size() - 1);
        BIHNode node = data.node;
        float tMin = data.min, tMax = data.max;
        if (tMax < tMin) {
            continue;
        }
        leafloop: while (node.axis != 3) {
            // while node is not a leaf
            int a = node.axis;
            // find the origin and direction value for the given axis
            float origin = origins[a];
            float invDirection = invDirections[a];
            float tNearSplit, tFarSplit;
            BIHNode nearNode, farNode;
            tNearSplit = (node.leftPlane - origin) * invDirection;
            tFarSplit = (node.rightPlane - origin) * invDirection;
            nearNode = node.left;
            farNode = node.right;
            if (invDirection < 0) {
                float tmpSplit = tNearSplit;
                tNearSplit = tFarSplit;
                tFarSplit = tmpSplit;
                BIHNode tmpNode = nearNode;
                nearNode = farNode;
                farNode = tmpNode;
            }
            if (tMin > tNearSplit && tMax < tFarSplit) {
                continue stackloop;
            }
            if (tMin > tNearSplit) {
                tMin = max(tMin, tFarSplit);
                node = farNode;
            } else if (tMax < tFarSplit) {
                tMax = min(tMax, tNearSplit);
                node = nearNode;
            } else {
                stack.add(new BIHStackData(farNode, max(tMin, tFarSplit), tMax));
                tMax = min(tMax, tNearSplit);
                node = nearNode;
            }
        }
        // a leaf
        for (int i = node.leftIndex; i <= node.rightIndex; i++) {
            tree.getTriangle(i, v1, v2, v3);
            float t = r.intersects(v1, v2, v3);
            if (!Float.isInfinite(t)) {
                if (worldMatrix != null) {
                    worldMatrix.mult(v1, v1);
                    worldMatrix.mult(v2, v2);
                    worldMatrix.mult(v3, v3);
                    float t_world = new Ray(o, d).intersects(v1, v2, v3);
                    t = t_world;
                }
                Vector3f contactNormal = Triangle.computeTriangleNormal(v1, v2, v3, null);
                Vector3f contactPoint = new Vector3f(d).multLocal(t).addLocal(o);
                float worldSpaceDist = o.distance(contactPoint);
                CollisionResult cr = new CollisionResult(contactPoint, worldSpaceDist);
                cr.setContactNormal(contactNormal);
                cr.setTriangleIndex(tree.getTriangleIndex(i));
                results.addCollision(cr);
                cols++;
            }
        }
    }
    vars.release();
    r.setOrigin(o);
    r.setDirection(d);
    return cols;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Matrix4f(com.jme3.math.Matrix4f) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars) Ray(com.jme3.math.Ray)

Example 12 with CollisionResults

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

the class BIHNode method intersectBrute.

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

Example 13 with CollisionResults

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

the class BoundingBox method collideWith.

@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;
        if (intersects(t.get1(), t.get2(), t.get3())) {
            CollisionResult r = new CollisionResult();
            results.addCollision(r);
            return 1;
        }
        return 0;
    } else if (other instanceof BoundingVolume) {
        if (intersects((BoundingVolume) other)) {
            CollisionResult r = new CollisionResult();
            results.addCollision(r);
            return 1;
        }
        return 0;
    } else if (other instanceof Spatial) {
        return ((Spatial) other).collideWith(this, results);
    } else {
        throw new UnsupportedCollisionException("With: " + other.getClass().getSimpleName());
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Spatial(com.jme3.scene.Spatial) UnsupportedCollisionException(com.jme3.collision.UnsupportedCollisionException)

Example 14 with CollisionResults

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

the class TestJoystick method pick.

private static CollisionResults pick(Camera cam, Vector2f mouseLoc, Node node) {
    CollisionResults results = new CollisionResults();
    Ray ray = new Ray();
    Vector3f pos = new Vector3f(mouseLoc.x, mouseLoc.y, -1);
    Vector3f dir = new Vector3f(mouseLoc.x, mouseLoc.y, 1);
    dir.subtractLocal(pos).normalizeLocal();
    ray.setOrigin(pos);
    ray.setDirection(dir);
    node.collideWith(ray, results);
    return results;
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray)

Example 15 with CollisionResults

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

the class TestJoystick method pickGamePad.

private void pickGamePad(Vector2f mouseLoc) {
    if (lastButton != null) {
        CollisionResults cresults = pick(cam, mouseLoc, gamepad);
        for (CollisionResult cr : cresults) {
            Node n = cr.getGeometry().getParent();
            if (n != null && (n instanceof ButtonView)) {
                String b = ((ButtonView) n).getName().substring("Button:".length());
                String name = lastButton.getJoystick().getName().replaceAll(" ", "\\\\ ");
                String id = lastButton.getLogicalId().replaceAll(" ", "\\\\ ");
                System.out.println(name + "." + id + "=" + b);
                return;
            }
        }
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) CollisionResults(com.jme3.collision.CollisionResults) Node(com.jme3.scene.Node)

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