Java Code Examples for com.jme3.math.Ray#setOrigin()

The following examples show how to use com.jme3.math.Ray#setOrigin() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TerrainCameraController.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Find where on the terrain the mouse intersects.
 */
protected Vector3f getTerrainCollisionPoint() {

    if (editorController.getTerrain(null) == null) {
        return null;
    }

    CollisionResults results = new CollisionResults();
    Ray ray = new Ray();
    Vector3f pos = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 0).clone();
    Vector3f dir = cam.getWorldCoordinates(new Vector2f(mouseX, mouseY), 0.3f).clone();
    dir.subtractLocal(pos).normalizeLocal();
    ray.setOrigin(pos);
    ray.setDirection(dir);
    editorController.getTerrain(null).collideWith(ray, results);
    if (results == null) {
        return null;
    }
    final CollisionResult result = results.getClosestCollision();
    if (result == null) {
        return null;
    }
    return result.getContactPoint();
}
 
Example 2
Source File: TestJoystick.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}
 
Example 3
Source File: BoundingCollisionTest.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@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);
}
 
Example 4
Source File: SceneEditTool.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private CollisionResult pick(Camera cam, Vector2f mouseLoc, Node node) {
    CollisionResults results = new CollisionResults();
    Ray ray = new Ray();
    Vector3f pos = cam.getWorldCoordinates(mouseLoc, 0).clone();
    Vector3f dir = cam.getWorldCoordinates(mouseLoc, 0.1f).clone();
    dir.subtractLocal(pos).normalizeLocal();
    ray.setOrigin(pos);
    ray.setDirection(dir);
    node.collideWith(ray, results);
    CollisionResult result = results.getClosestCollision();
    return result;
}
 
Example 5
Source File: EntropyComputeUtil.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
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);
    } else {
        terrainBlock.setBuffer(Type.Index, 3, (ByteBuffer) 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;
}
 
Example 6
Source File: EntropyComputeUtil.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
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;
}