Hiding Mouse Cursor in jME3

I’m experiencing a small problem - hiding the mouse cursor. What can I use to make it invisible?

If you set your camera to “dragToRotate(false)” then the cursor is removed automatically if that does not conflict with your other code and design it would be an easy solution^^

Goto upper search bar, type: mouse cursor. Click on the right result and read:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:mouse_picking?s[]=mouse&s[]=cursor

@enum, your code didn’t work :confused: Gave me a null pointer exception.



I also tried inputManager.setCursorVisible(false);, from the link Dodikles gave me, but when using it, the pointer remains in the center of the screen (I can’t move it). I am using a Chase Camera.

if [java]chaseCam.setDragToRotate(false);[/java] gives you a NPE, then obviously your chaseCam does not contain an object of type ChaseCamera but null ; )

I didn’t understand the last part. The following is my code:



[java]public void camera(Camera cam) {

chaseCam = new ChaseCamera(cam, geom);

chaseCam.setDefaultHorizontalRotation(90FastMath.DEG_TO_RAD);

chaseCam.setDefaultVerticalRotation(0
FastMath.DEG_TO_RAD);

chaseCam.setChasingSensitivity(0f);

chaseCam.setDefaultDistance(10);

}[/java]



Adding your code at the end results in the NPE.

You need to pass the inputManager to the constructor of the ChaseCamera then. I didn’t remember that. And indeed it does the same as Dodikles recommended :wink:

When I did that, the mouse remained all the time in the middle of the screen.

I believe that the camera needs to be updated for the cursor to disappear, but I am not sure right now. Alternatively, you can just position the cursor in the lower right corner, thus positioning it out of screen.

I can’t do the last thing - as the mouse-position is important in the game (space-shooter). When I use inputManager.setCursorVisible… the cursor does disappear, but then the mouse can’t be moved from the center of the screen.

Can someone tell me if I can access the JFrame of the main app [or if it has one]? If I could access it, I would be able to change the cursor myself.

If you do not create a JFrame yourself and add a JME Canvas to it then there is no JFrame it’s an OpenGL Window which is quite different from JFrame so you should try to use the methods provided by JME.


the cursor does disappear, but then the mouse can’t be moved from the center of the screen.

You say you can't use it to control your view anymore?

I use a chaseCamera. When I use inputManager.setCursorVisible the cursor returns all the time to the middle of the screen.

1 Like

When the mouse is not visible, you will still receive your mouse movements via the inputListener. Just supply your inputManager with analogListeners that will listen to MouseAxisTriggers and then adjust your space ship when a trigger gets fired. The flyCam actually does the same in its interna.

So what you mean is that I work out the mouse position myself? Any hints how to do that?

Well, you do it yourself! Read out the cursor position at the start, hide it and remove it, and from there on you keep track of it by your own methods… that easy.

I think you didn’t understand - or I didn’t. When I use inputManager.setCursorVisible, you can’t move the cursor away from the centre. While it is invisible, it stays permanently in the center. (I know it doesn’t move, even though it is invisible, because I have an object that stays wherever the mouse is.

Any help anyone? Every time I add inputManager.setCursorVisible(false), the pointer can’t be moved away from the center. How can I go round it?

Dude… did you understand what I told you about inputManager and analogListeners?



So it is invisible or not?



This is the code I gave you. And it works well:



[java]if (name.equals(“toggle_cursor_visibility”) && !pressed) {

flyMode = !flyMode;

game.camera.setEnabled(flyMode);

game.getInputManager().setCursorVisible(!flyMode);

}

[/java]

First, you save the cursor position before hiding it. Then you create an analogListener like this:



[java]

private AnalogListener analogListener = new AnalogListener() {



public void onAnalog(String name, float value, float tpf) {

camLoc = game.getCamera().getLocation();

camDir = game.getCamera().getDirection();

camLeft = game.getCamera().getLeft();



if (name.equals(“mouse_moveup”) || name.equals(“mouse_movedown”)

|| name.equals(“mouse_moveleft”) || name.equals(“mouse_moveright”)) {



//Calculate new cursor position or new camera direction WHATEVER

}



game.getCamera().setLocation(camLoc);

}



};

[/java]



In your initialize()-method, do something like:



[java]

getInputManager().addMapping(“mouse_moveright”, new MouseAxisTrigger(MouseInput.AXIS_X, true));

getInputManager().addMapping(“mouse_moveleft”, new MouseAxisTrigger(MouseInput.AXIS_X, false));

getInputManager().addMapping(“mouse_moveup”, new MouseAxisTrigger(MouseInput.AXIS_Y, true));

getInputManager().addMapping(“mouse_movedown”, new MouseAxisTrigger(MouseInput.AXIS_Y, false));

[/java]



And register the actions with



[java]

game.getInputManager().addListener(analogListener, “mouse_moveleft”);

game.getInputManager().addListener(analogListener, “mouse_moveright”);

game.getInputManager().addListener(analogListener, “mouse_moveup”);

game.getInputManager().addListener(analogListener, “mouse_movedown”);

[/java]



These are completely basic things which you should have understood after reading the tutorials. I’m sorry but if you don’t grasp this essential things, how are you supposed to continue your project? We won’t program it for you. You really have to read… and read… and read again. Try out things, look into other code (always a good idea to look into the JME sources)!!! And now give me my +1 :smiley:

1 Like

Thanks for your patience man… I try my best. I will try this code right after the football match I’m watching finishes.



+1’d :stuck_out_tongue: