Search in sources :

Example 1 with Point

use of com.jme3.scene.plugins.blender.meshes.Point 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 2 with Point

use of com.jme3.scene.plugins.blender.meshes.Point 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 3 with Point

use of com.jme3.scene.plugins.blender.meshes.Point 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 4 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method distanceToEdge.

public float distanceToEdge(Vector3f point) {
    // compute coordinates of point in box coordinate system
    TempVars vars = TempVars.get();
    Vector3f closest = vars.vect1;
    point.subtract(center, closest);
    // project test point onto box
    float sqrDistance = 0.0f;
    float delta;
    if (closest.x < -xExtent) {
        delta = closest.x + xExtent;
        sqrDistance += delta * delta;
        closest.x = -xExtent;
    } else if (closest.x > xExtent) {
        delta = closest.x - xExtent;
        sqrDistance += delta * delta;
        closest.x = xExtent;
    }
    if (closest.y < -yExtent) {
        delta = closest.y + yExtent;
        sqrDistance += delta * delta;
        closest.y = -yExtent;
    } else if (closest.y > yExtent) {
        delta = closest.y - yExtent;
        sqrDistance += delta * delta;
        closest.y = yExtent;
    }
    if (closest.z < -zExtent) {
        delta = closest.z + zExtent;
        sqrDistance += delta * delta;
        closest.z = -zExtent;
    } else if (closest.z > zExtent) {
        delta = closest.z - zExtent;
        sqrDistance += delta * delta;
        closest.z = zExtent;
    }
    vars.release();
    return FastMath.sqrt(sqrDistance);
}
Also used : TempVars(com.jme3.util.TempVars)

Example 5 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class BoundingBox method computeFromTris.

public void computeFromTris(int[] indices, Mesh mesh, int start, int end) {
    if (end - start <= 0) {
        return;
    }
    TempVars vars = TempVars.get();
    Vector3f vect1 = vars.vect1;
    Vector3f vect2 = vars.vect2;
    Triangle triangle = vars.triangle;
    Vector3f min = vect1.set(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
    Vector3f max = vect2.set(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
    Vector3f point;
    for (int i = start; i < end; i++) {
        mesh.getTriangle(indices[i], triangle);
        point = triangle.get(0);
        checkMinMax(min, max, point);
        point = triangle.get(1);
        checkMinMax(min, max, point);
        point = triangle.get(2);
        checkMinMax(min, max, point);
    }
    center.set(min.addLocal(max));
    center.multLocal(0.5f);
    xExtent = max.x - center.x;
    yExtent = max.y - center.y;
    zExtent = max.z - center.z;
    vars.release();
}
Also used : TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)27 TempVars (com.jme3.util.TempVars)19 FloatBuffer (java.nio.FloatBuffer)6 ColorRGBA (com.jme3.math.ColorRGBA)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 SpotLight (com.jme3.light.SpotLight)4 Quaternion (com.jme3.math.Quaternion)4 Spatial (com.jme3.scene.Spatial)4 ArrayList (java.util.ArrayList)4 CollisionResult (com.jme3.collision.CollisionResult)3 Light (com.jme3.light.Light)3 Triangle (com.jme3.math.Triangle)3 Vector2f (com.jme3.math.Vector2f)3 Geometry (com.jme3.scene.Geometry)3 Mesh (com.jme3.scene.Mesh)3 BoundingSphere (com.jme3.bounding.BoundingSphere)2 MotionPath (com.jme3.cinematic.MotionPath)2 MotionPathListener (com.jme3.cinematic.MotionPathListener)2 MotionEvent (com.jme3.cinematic.events.MotionEvent)2