Picking with the mouse

I’m trying to figure out how to use the mouse to pick an object in jME3. The sample uses the center of the viewport to determine the pick ray, but I want to be able to point and click with the mouse. The getXAbsolute() and getYAbsolute() methods seem to be gone in jME3. I’ve been through a lot of docs and forum posts, but I still can’t find the right methods to get the 2D mouse coordinates and translate them into a pick ray into the 3D world.



Can anyone help with this, please?



Thanks,

Lee

Hello Picking can help you

Thanks for your reply, rickard, but that was the example I was referring to. It uses the center of the viewport (based on the camera) to determine the pick ray:



[java]Ray ray = new Ray(cam.getLocation(), cam.getDirection());[/java]



I want to use a mouse click to create that ray, but I’m not sure how.

Sorry. I guess i didn’t read your post properly.



You use the inputmanager’s getCursorPosition() to get the mouse position.



There’s a good example in the test/collision folder, called TestMousePick

Hello!



[java]

Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());

// System.out.println(mouseCoords);

mouseRay = new Ray(cam.getWorldCoordinates(mouseCoords, 0),

cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(

cam.getWorldCoordinates(mouseCoords, 0)).normalizeLocal());

[/java]



Is the code I use all over in my code to get the screen position of the mouse and fire a ray. Your mileage may vary. Make sure you call MouseInput.setCursorVisible(true), to get the cursor on your screen.



~FlaH

1 Like

Take a look at TestMousePick in the JmeTests. That should solve your problems.

tehflah said:
Hello!

[java]
Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());
// System.out.println(mouseCoords);
mouseRay = new Ray(cam.getWorldCoordinates(mouseCoords, 0),
cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(
cam.getWorldCoordinates(mouseCoords, 0)).normalizeLocal());
[/java]

Is the code I use all over in my code to get the screen position of the mouse and fire a ray. Your mileage may vary. Make sure you call MouseInput.setCursorVisible(true), to get the cursor on your screen.

~FlaH

YaY! I was doing something similar but I was tracking the mouse with a RawInputListener