Search in sources :

Example 6 with Ray

use of com.jme3.math.Ray 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 7 with Ray

use of com.jme3.math.Ray 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 8 with Ray

use of com.jme3.math.Ray in project jmonkeyengine by jMonkeyEngine.

the class BoundingCollisionTest method testBoxRayCollision.

@Test
public void testBoxRayCollision() {
    BoundingBox box = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
    Ray ray = new Ray(Vector3f.ZERO, Vector3f.UNIT_Z);
    // XXX: seems incorrect, ray inside box should only generate
    // one result...
    checkCollision(box, ray, 2);
    ray.setOrigin(new Vector3f(0, 0, -5));
    checkCollision(box, ray, 2);
    // XXX: is this right? the ray origin is on the box's side..
    ray.setOrigin(new Vector3f(0, 0, 2f));
    checkCollision(box, ray, 0);
    ray.setOrigin(new Vector3f(0, 0, -2f));
    checkCollision(box, ray, 2);
    // parallel to the edge, touching the side
    ray.setOrigin(new Vector3f(0, 1f, -2f));
    checkCollision(box, ray, 2);
    // still parallel, but not touching the side
    ray.setOrigin(new Vector3f(0, 1f + FastMath.ZERO_TOLERANCE, -2f));
    checkCollision(box, ray, 0);
}
Also used : BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray) Test(org.junit.Test)

Example 9 with Ray

use of com.jme3.math.Ray in project jmonkeyengine by jMonkeyEngine.

the class BetterCharacterControl method checkCanUnDuck.

/**
     * This checks if the character can go from ducked to unducked state by
     * doing a ray test.
     */
protected boolean checkCanUnDuck() {
    TempVars vars = TempVars.get();
    Vector3f location = vars.vect1;
    Vector3f rayVector = vars.vect2;
    location.set(localUp).multLocal(FastMath.ZERO_TOLERANCE).addLocal(this.location);
    rayVector.set(localUp).multLocal(height + FastMath.ZERO_TOLERANCE).addLocal(location);
    List<PhysicsRayTestResult> results = space.rayTest(location, rayVector);
    vars.release();
    for (PhysicsRayTestResult physicsRayTestResult : results) {
        if (!physicsRayTestResult.getCollisionObject().equals(rigidBody)) {
            return false;
        }
    }
    return true;
}
Also used : PhysicsRayTestResult(com.jme3.bullet.collision.PhysicsRayTestResult) Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 10 with Ray

use of com.jme3.math.Ray in project jmonkeyengine by jMonkeyEngine.

the class EntropyComputeUtil method computeLodEntropy.

public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices) {
    // Bounding box for the terrain block
    BoundingBox bbox = (BoundingBox) terrainBlock.getBound();
    // Vertex positions for the block
    FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);
    // Prepare to cast rays
    Vector3f pos = new Vector3f();
    Vector3f dir = new Vector3f(0, -1, 0);
    Ray ray = new Ray(pos, dir);
    // Prepare collision results
    CollisionResults results = new CollisionResults();
    // Set the LOD indices on the block
    VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);
    terrainBlock.clearBuffer(Type.Index);
    if (lodIndices instanceof IntBuffer)
        terrainBlock.setBuffer(Type.Index, 3, (IntBuffer) lodIndices);
    else if (lodIndices instanceof ShortBuffer) {
        terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
    }
    // Recalculate collision mesh
    terrainBlock.createCollisionData();
    float entropy = 0;
    for (int i = 0; i < positions.limit() / 3; i++) {
        BufferUtils.populateFromBuffer(pos, positions, i);
        float realHeight = pos.y;
        pos.addLocal(0, bbox.getYExtent(), 0);
        ray.setOrigin(pos);
        results.clear();
        terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);
        if (results.size() > 0) {
            Vector3f contactPoint = results.getClosestCollision().getContactPoint();
            float delta = Math.abs(realHeight - contactPoint.y);
            entropy = Math.max(delta, entropy);
        }
    }
    // Restore original indices
    terrainBlock.clearBuffer(Type.Index);
    terrainBlock.setBuffer(originalIndices);
    return entropy;
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) VertexBuffer(com.jme3.scene.VertexBuffer) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) Ray(com.jme3.math.Ray) ShortBuffer(java.nio.ShortBuffer)

Aggregations

Vector3f (com.jme3.math.Vector3f)13 TempVars (com.jme3.util.TempVars)12 CollisionResult (com.jme3.collision.CollisionResult)10 CollisionResults (com.jme3.collision.CollisionResults)8 Ray (com.jme3.math.Ray)8 BoundingBox (com.jme3.bounding.BoundingBox)3 Vector2f (com.jme3.math.Vector2f)3 PhysicsRayTestResult (com.jme3.bullet.collision.PhysicsRayTestResult)2 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)2 Spatial (com.jme3.scene.Spatial)2 Matrix4f (com.jme3.math.Matrix4f)1 Quaternion (com.jme3.math.Quaternion)1 Triangle (com.jme3.math.Triangle)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 IntBuffer (java.nio.IntBuffer)1 ShortBuffer (java.nio.ShortBuffer)1