[SOLVED] I cant hide mouse cursor

Hi, I’ve made a custom camera (a class that controls the camera) to create a Camera FPS, using CameraNode, everything works, except for one thing, I can’t hide or “catch in window” the mouse cursor, in fact after disabling flyCamera, even if I put
inputManager.setCursorVisible(false);
the cursor will remain visible…
it works only if i put it in simpleUpdate…

here is my class that controls the camera:

public class FpsCameraController implements ActionListener, AnalogListener {

    private final Camera cam;
    private final InputManager inputManager;
    private CameraNode teaNode;
    protected float rotationSpeed = 1f;
    protected Vector3f initialUpVec;
     
    public FpsCameraController(Camera cam, InputManager inputManager) {
        this.cam = cam;
        this.inputManager = inputManager;
        initialUpVec = cam.getUp().clone();
        setUpCam();
        registerInput();
    }

    @Override
    public void onAction(String string, boolean bln, float f) {

    }
    
    private void setUpCam(){
        cam.setFrustumPerspective(45f, (float) cam.getWidth() / cam.getHeight(), 0.01f, 1000f);
        cam.setLocation(new Vector3f(0,6,0));
        teaNode = new CameraNode("playerCam",cam);
        teaNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera);
        inputManager.setCursorVisible(false);
        System.out.println(inputManager.isCursorVisible());
        cam.update();
    }
    String [] mappings = {
        CameraInput.FLYCAM_LEFT,
        CameraInput.FLYCAM_RIGHT,
        CameraInput.FLYCAM_UP,
        CameraInput.FLYCAM_DOWN,
    }; 
    private void registerInput(){
        inputManager.addMapping(CameraInput.FLYCAM_LEFT, new MouseAxisTrigger(MouseInput.AXIS_X, true),
                new KeyTrigger(KeyInput.KEY_LEFT));

        inputManager.addMapping(CameraInput.FLYCAM_RIGHT, new MouseAxisTrigger(MouseInput.AXIS_X, false),
                new KeyTrigger(KeyInput.KEY_RIGHT));

        inputManager.addMapping(CameraInput.FLYCAM_UP, new MouseAxisTrigger(MouseInput.AXIS_Y, false),
                new KeyTrigger(KeyInput.KEY_UP));

        inputManager.addMapping(CameraInput.FLYCAM_DOWN, new MouseAxisTrigger(MouseInput.AXIS_Y, true),
                new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addListener(this, mappings);
        inputManager.setCursorVisible(false);
    }
    
    public void unregisterInput() {
        if (inputManager == null) {
            return;
        }

        for (String s : mappings) {
            if (inputManager.hasMapping(s)) {
                inputManager.deleteMapping(s);
            }
        }

        inputManager.removeListener(this);
    }
    @Override
    public void onAnalog(String name, float value, float tpf) {
        System.out.println(name);
        if (name.equals(CameraInput.FLYCAM_LEFT)) {
            rotateCamera(value, initialUpVec);
        } else if (name.equals(CameraInput.FLYCAM_RIGHT)) {
            rotateCamera(-value, initialUpVec);
        } else if (name.equals(CameraInput.FLYCAM_UP)) {
            rotateCamera(-value , cam.getLeft());
        } else if (name.equals(CameraInput.FLYCAM_DOWN)) {
            rotateCamera(value, cam.getLeft());
        }
        
    }
    
    protected void rotateCamera(float value, Vector3f axis) {
        

        Matrix3f mat = new Matrix3f();
        mat.fromAngleNormalAxis(rotationSpeed * value, axis);

        Vector3f up = cam.getUp();
        Vector3f left = cam.getLeft();
        Vector3f dir = cam.getDirection();

        mat.mult(up, up);
        mat.mult(left, left);
        mat.mult(dir, dir);

        Quaternion q = new Quaternion();
        q.fromAxes(left, up, dir);
        q.normalizeLocal();

        //cam.setAxes(q);
        teaNode.setLocalRotation(q);
        //inputManager.setCursorVisible(false);
       // cam.update();
       // System.out.println(cam.getRotation());
    }
    
    
    public Camera getCamera(){
        return cam;
    }
    
    public CameraNode getCamNode(){
        return teaNode;
    }
    
    
    
}

it is very much a copy of flyCam,
To initialize it I do this:

    private void setUpCamera(Camera cam, InputManager inputManager){
        camController = new FpsCameraController(cam,inputManager);
        playerNode.attachChild(camController.getCamNode());
        camController.getCamNode().setLocalTranslation(0, 3f, 0);
    }

and in the main class in simpleInitApp I have:

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

I can’t quite understand why, thank you in advance for your support!

Because by the time you setCursorVisible(false) in simple init, it’s too late because the fly cam app state will already be going to disable it.

You can either remove the fly cam app state so that it isn’t even registered anymore or you can add a constructor to your app that adds only the app states that you want. (the right way)

The hackiest way is to enqueue your disabling of the cursor visibility so it runs on the next frame.

enqueue( () -> inputManager.setCursorVisible(false) );

But as said, in the long run, the right approach is to control what app states you want on app construction:

    public YourApp() {
        super(new StatsAppState(), new DebugKeysAppState(), new BasicProfilerState(false),
              new ScreenshotAppState("", System.currentTimeMillis()));
    }

…that also sets you up nicely for adding your own app states or for using app states from other libraries (like the CameraState, and MovementState from SiO2, for example)

4 Likes

This worked!!! Thank you!

1 Like