Unlocking the mouse from a JMonkey window

846 views Asked by At

Normally the mouse is locked to the window and is not visible; controlling the camera in the style of first person shooters.

My understanding is you unlocking the mouse from a JMonkey window and make it visible by calling

inputManager.setCursorVisible(true);

However this has no visible effect. This is demonstrated within the following example program:

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", b);

        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        inputManager.setCursorVisible(true);

        rootNode.attachChild(geom);
    }

}

Calling flyCam.setDragToRotate(true); unlocks the mouse but also causes a number of DragToRotate behaviours (unsurpisingly)

3

There are 3 answers

0
Richard Tingle On

The solution to this seems to be that the flycam must also be disabled. So

inputManager.setCursorVisible(true);
flyCam.setEnabled(false);

Or as a full example

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", b);

        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        inputManager.setCursorVisible(true);
        flyCam.setEnabled(false);
        //flyCam.setDragToRotate(true);
        inputManager.setCursorVisible(true);
        rootNode.attachChild(geom);
    }

}
2
Tim B On

Disabling the FlyCam is one way to do this, but a better way is never to add it in the first place. If you create a new constructor for your app and call the second constructor for SimpleApplication you can pass in a list of Application States to use and these completely replace the standard set so you can pick exactly which ones you want.

1
user3473680 On

Or simply:

flyCam.setDragToRotate(true);

inputManager.setCursorVisible(true);

The flyCam can still be on