Search in sources :

Example 6 with CollisionResults

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

the class BoundingBox method collideWithRay.

/**
     * @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray)
     */
private int collideWithRay(Ray ray, CollisionResults results) {
    TempVars vars = TempVars.get();
    try {
        Vector3f diff = vars.vect1.set(ray.origin).subtractLocal(center);
        Vector3f direction = vars.vect2.set(ray.direction);
        //float[] t = {0f, Float.POSITIVE_INFINITY};
        // use one of the tempvars arrays
        float[] t = vars.fWdU;
        t[0] = 0;
        t[1] = Float.POSITIVE_INFINITY;
        float saveT0 = t[0], saveT1 = t[1];
        boolean notEntirelyClipped = clip(+direction.x, -diff.x - xExtent, t) && clip(-direction.x, +diff.x - xExtent, t) && clip(+direction.y, -diff.y - yExtent, t) && clip(-direction.y, +diff.y - yExtent, t) && clip(+direction.z, -diff.z - zExtent, t) && clip(-direction.z, +diff.z - zExtent, t);
        if (notEntirelyClipped && (t[0] != saveT0 || t[1] != saveT1)) {
            if (t[1] > t[0]) {
                float[] distances = t;
                Vector3f point0 = new Vector3f(ray.direction).multLocal(distances[0]).addLocal(ray.origin);
                Vector3f point1 = new Vector3f(ray.direction).multLocal(distances[1]).addLocal(ray.origin);
                CollisionResult result = new CollisionResult(point0, distances[0]);
                results.addCollision(result);
                result = new CollisionResult(point1, distances[1]);
                results.addCollision(result);
                return 2;
            }
            Vector3f point = new Vector3f(ray.direction).multLocal(t[0]).addLocal(ray.origin);
            CollisionResult result = new CollisionResult(point, t[0]);
            results.addCollision(result);
            return 1;
        }
        return 0;
    } finally {
        vars.release();
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) TempVars(com.jme3.util.TempVars)

Example 7 with CollisionResults

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

the class BoundingSphere method collideWith.

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 ((Spatial) other).collideWith(this, results);
    } else {
        throw new UnsupportedCollisionException();
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) Spatial(com.jme3.scene.Spatial) UnsupportedCollisionException(com.jme3.collision.UnsupportedCollisionException)

Example 8 with CollisionResults

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

the class BoundingSphere method collideWithRay.

/*
     * (non-Javadoc)
     *
     * @see com.jme.bounding.BoundingVolume#intersectsWhere(com.jme.math.Ray)
     */
private int collideWithRay(Ray ray, CollisionResults results) {
    TempVars vars = TempVars.get();
    Vector3f diff = vars.vect1.set(ray.getOrigin()).subtractLocal(center);
    float a = diff.dot(diff) - (getRadius() * getRadius());
    float a1, discr, root;
    if (a <= 0.0) {
        // inside sphere
        a1 = ray.direction.dot(diff);
        discr = (a1 * a1) - a;
        root = FastMath.sqrt(discr);
        float distance = root - a1;
        Vector3f point = new Vector3f(ray.direction).multLocal(distance).addLocal(ray.origin);
        CollisionResult result = new CollisionResult(point, distance);
        results.addCollision(result);
        vars.release();
        return 1;
    }
    a1 = ray.direction.dot(diff);
    vars.release();
    if (a1 >= 0.0) {
        return 0;
    }
    discr = a1 * a1 - a;
    if (discr < 0.0) {
        return 0;
    } else if (discr >= FastMath.ZERO_TOLERANCE) {
        root = FastMath.sqrt(discr);
        float dist = -a1 - root;
        Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
        results.addCollision(new CollisionResult(point, dist));
        dist = -a1 + root;
        point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
        results.addCollision(new CollisionResult(point, dist));
        return 2;
    } else {
        float dist = -a1;
        Vector3f point = new Vector3f(ray.direction).multLocal(dist).addLocal(ray.origin);
        results.addCollision(new CollisionResult(point, dist));
        return 1;
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) TempVars(com.jme3.util.TempVars)

Example 9 with CollisionResults

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

the class BoundingSphere method collideWithTri.

private int collideWithTri(Triangle tri, CollisionResults results) {
    TempVars tvars = TempVars.get();
    try {
        // Much of this is based on adaptation from this algorithm:
        // http://realtimecollisiondetection.net/blog/?p=103
        // ...mostly the stuff about eliminating sqrts wherever
        // possible.
        // Math is done in center-relative space.
        Vector3f a = tri.get1().subtract(center, tvars.vect1);
        Vector3f b = tri.get2().subtract(center, tvars.vect2);
        Vector3f c = tri.get3().subtract(center, tvars.vect3);
        Vector3f ab = b.subtract(a, tvars.vect4);
        Vector3f ac = c.subtract(a, tvars.vect5);
        // Check the plane... if it doesn't intersect the plane
        // then it doesn't intersect the triangle.
        Vector3f n = ab.cross(ac, tvars.vect6);
        float d = a.dot(n);
        float e = n.dot(n);
        if (d * d > radius * radius * e) {
            // Can't possibly intersect
            return 0;
        }
        // We intersect the verts, or the edges, or the face...
        // First check against the face since it's the most
        // specific.
        // Calculate the barycentric coordinates of the
        // sphere center
        Vector3f v0 = ac;
        Vector3f v1 = ab;
        // a was P relative, so p.subtract(a) is just -a
        // instead of wasting a vector we'll just negate the
        // dot products below... it's all v2 is used for.
        Vector3f v2 = a;
        float dot00 = v0.dot(v0);
        float dot01 = v0.dot(v1);
        float dot02 = -v0.dot(v2);
        float dot11 = v1.dot(v1);
        float dot12 = -v1.dot(v2);
        float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
        float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
        float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
        if (u >= 0 && v >= 0 && (u + v) <= 1) {
            // We intersect... and we even know where
            Vector3f part1 = ac;
            Vector3f part2 = ab;
            Vector3f p = center.add(a.add(part1.mult(u)).addLocal(part2.mult(v)));
            CollisionResult r = new CollisionResult();
            Vector3f normal = n.normalize();
            // a is center relative, so -a points to center
            float dist = -normal.dot(a);
            dist = dist - radius;
            r.setDistance(dist);
            r.setContactNormal(normal);
            r.setContactPoint(p);
            results.addCollision(r);
            return 1;
        }
        // Check the edges looking for the nearest point
        // that is also less than the radius.  We don't care
        // about points that are farther away than that.
        Vector3f nearestPt = null;
        float nearestDist = radius * radius;
        Vector3f base;
        Vector3f edge;
        float t;
        // Edge AB
        base = a;
        edge = ab;
        t = -edge.dot(base) / edge.dot(edge);
        if (t >= 0 && t <= 1) {
            Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect8);
            // distance squared to origin
            float distSq = Q.dot(Q);
            if (distSq < nearestDist) {
                nearestPt = Q;
                nearestDist = distSq;
            }
        }
        // Edge AC
        base = a;
        edge = ac;
        t = -edge.dot(base) / edge.dot(edge);
        if (t >= 0 && t <= 1) {
            Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect9);
            // distance squared to origin
            float distSq = Q.dot(Q);
            if (distSq < nearestDist) {
                nearestPt = Q;
                nearestDist = distSq;
            }
        }
        // Edge BC
        base = b;
        Vector3f bc = c.subtract(b);
        edge = bc;
        t = -edge.dot(base) / edge.dot(edge);
        if (t >= 0 && t <= 1) {
            Vector3f Q = base.add(edge.mult(t, tvars.vect7), tvars.vect10);
            // distance squared to origin
            float distSq = Q.dot(Q);
            if (distSq < nearestDist) {
                nearestPt = Q;
                nearestDist = distSq;
            }
        }
        // done.       
        if (nearestPt != null) {
            // We have a hit
            float dist = FastMath.sqrt(nearestDist);
            Vector3f cn = nearestPt.divide(-dist);
            CollisionResult r = new CollisionResult();
            r.setDistance(dist - radius);
            r.setContactNormal(cn);
            r.setContactPoint(nearestPt.add(center));
            results.addCollision(r);
            return 1;
        }
        // Finally check each of the triangle corners
        // Vert A
        base = a;
        // distance squared to origin
        t = base.dot(base);
        if (t < nearestDist) {
            nearestDist = t;
            nearestPt = base;
        }
        // Vert B
        base = b;
        // distance squared to origin
        t = base.dot(base);
        if (t < nearestDist) {
            nearestDist = t;
            nearestPt = base;
        }
        // Vert C
        base = c;
        // distance squared to origin
        t = base.dot(base);
        if (t < nearestDist) {
            nearestDist = t;
            nearestPt = base;
        }
        if (nearestPt != null) {
            // We have a hit
            float dist = FastMath.sqrt(nearestDist);
            Vector3f cn = nearestPt.divide(-dist);
            CollisionResult r = new CollisionResult();
            r.setDistance(dist - radius);
            r.setContactNormal(cn);
            r.setContactPoint(nearestPt.add(center));
            results.addCollision(r);
            return 1;
        }
        // Nothing hit... oh, well 
        return 0;
    } finally {
        tvars.release();
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) TempVars(com.jme3.util.TempVars)

Example 10 with CollisionResults

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

the class BoundingVolume method collideWith.

public int collideWith(Collidable other) {
    TempVars tempVars = TempVars.get();
    try {
        CollisionResults tempResults = tempVars.collisionResults;
        tempResults.clear();
        return collideWith(other, tempResults);
    } finally {
        tempVars.release();
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) TempVars(com.jme3.util.TempVars)

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