TerrainGrid Mouse Pick Collision

Hi i spended few hours searching and googling but cant find anything. I want make terrain editor same like SceneComposer, but i have problem with mouse pick. Here you can see video… Ray cast always point to same position, but i want make cast ray directly straight from mouseCursor, so i shouldn’t se line, but just dot.

CrossHair picker working fine :slight_smile: i want same behavior for mouse picket to, just set start point with mouse and not center of screen.

 private Vector3f getTerrainIntersection() {
        CollisionResults results = new CollisionResults();

        Vector3f loc = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f);
        Vector3f dir = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);
        dir = dir.subtractLocal(loc).normalizeLocal();
                
        Ray ray = new Ray(loc, dir);

        terrain.getParent().collideWith(ray, results);
        if (rootNode.hasChild(line)) {
            line.removeFromParent();
        }
        line = new Geometry("Line", new Line(loc, dir));
        
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Orange);
        mat.getAdditionalRenderState().setWireframe(true);
        line.setMaterial(mat);
        rootNode.attachChild(line);
        
        if (results.size() > 0) {
            return results.getClosestCollision().getContactPoint();
        }
        return null;
    }

I’m sure it’s in the documentation somewhere but I can’t find it, so this will have to do.

Ahh, i found problem and maybe possible bug in JME3

I used this, terrain lowering working without problem, but terrain raising have problem…

I added terrain.updateModelBound(); under 111 line and everything works fine now :slight_smile:

The documentation seems to indicate the you will also need to reset the collision data:
https://wiki.jmonkeyengine.org/jme3/advanced/custom_meshes.html

N.B.: This does not work on TerrainQuad. Please use the TerrainQuad.adjustHeight() function to edit the TerrainQuad mesh instead. Additionally, if you want to use collisions on them afterwards, you need to call TerrainPatch.getMesh().createCollisionData(); to update the collision data, else it will collide with what seems to be the old mesh.

I don’t know if that’s really true, though. I’d have expected height map based terrain to be doing something more efficient than the default mesh collisions. (Also, it would be better to clear it than to recreate it… let it create it automatically the next time picking is attempted.)