Search in sources :

Example 1 with CollisionResults

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

the class TerrainTestModifyHeight method getWorldIntersection.

private Vector3f getWorldIntersection() {
    Vector3f origin = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.0f);
    Vector3f direction = cam.getWorldCoordinates(new Vector2f(settings.getWidth() / 2, settings.getHeight() / 2), 0.3f);
    direction.subtractLocal(origin).normalizeLocal();
    Ray ray = new Ray(origin, direction);
    CollisionResults results = new CollisionResults();
    int numCollisions = terrain.collideWith(ray, results);
    if (numCollisions > 0) {
        CollisionResult hit = results.getClosestCollision();
        return hit.getContactPoint();
    }
    return null;
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) CollisionResults(com.jme3.collision.CollisionResults) Vector2f(com.jme3.math.Vector2f) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray)

Example 2 with CollisionResults

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

Example 3 with CollisionResults

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

the class TestTriangleCollision method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    CollisionResults results = new CollisionResults();
    BoundingVolume bv = geom1.getWorldBound();
    golem.collideWith(bv, results);
    if (results.size() > 0) {
        geom1.getMaterial().setColor("Color", ColorRGBA.Red);
    } else {
        geom1.getMaterial().setColor("Color", ColorRGBA.Blue);
    }
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) BoundingVolume(com.jme3.bounding.BoundingVolume)

Example 4 with CollisionResults

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

the class TestMousePick method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    Vector3f origin = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
    Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
    direction.subtractLocal(origin).normalizeLocal();
    Ray ray = new Ray(origin, direction);
    CollisionResults results = new CollisionResults();
    shootables.collideWith(ray, results);
    //        }
    if (results.size() > 0) {
        CollisionResult closest = results.getClosestCollision();
        mark.setLocalTranslation(closest.getContactPoint());
        Quaternion q = new Quaternion();
        q.lookAt(closest.getContactNormal(), Vector3f.UNIT_Y);
        mark.setLocalRotation(q);
        rootNode.attachChild(mark);
    } else {
        rootNode.detachChild(mark);
    }
}
Also used : CollisionResult(com.jme3.collision.CollisionResult) CollisionResults(com.jme3.collision.CollisionResults) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray)

Example 5 with CollisionResults

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

the class CollisionUtil method checkCollisionBase.

private static void checkCollisionBase(Collidable a, Collidable b, int expected) {
    // Test bounding volume methods
    if (a instanceof BoundingVolume && b instanceof BoundingVolume) {
        BoundingVolume bv1 = (BoundingVolume) a;
        BoundingVolume bv2 = (BoundingVolume) b;
        assert bv1.intersects(bv2) == (expected != 0);
    }
    // Test standard collideWith method
    CollisionResults results = new CollisionResults();
    int numCollisions = a.collideWith(b, results);
    assert results.size() == numCollisions;
    assert numCollisions == expected;
    // force the results to be sorted here..
    results.getClosestCollision();
    if (results.size() > 0) {
        assert results.getCollision(0) == results.getClosestCollision();
    }
    if (results.size() == 1) {
        assert results.getClosestCollision() == results.getFarthestCollision();
    }
}
Also used : BoundingVolume(com.jme3.bounding.BoundingVolume)

Aggregations

CollisionResult (com.jme3.collision.CollisionResult)22 CollisionResults (com.jme3.collision.CollisionResults)21 Ray (com.jme3.math.Ray)15 Vector3f (com.jme3.math.Vector3f)15 TempVars (com.jme3.util.TempVars)8 Spatial (com.jme3.scene.Spatial)6 Geometry (com.jme3.scene.Geometry)5 Vector2f (com.jme3.math.Vector2f)4 BoundingBox (com.jme3.bounding.BoundingBox)3 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)3 BoundingVolume (com.jme3.bounding.BoundingVolume)2 Triangle (com.jme3.math.Triangle)2 Node (com.jme3.scene.Node)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Point2d (javax.vecmath.Point2d)2 MouseEventState (com.cas.robot.common.MouseEventState)1 BoundingSphere (com.jme3.bounding.BoundingSphere)1 ColorRGBA (com.jme3.math.ColorRGBA)1