SOLVED - Mouse to world Coords

I have a need to select an item and move it with the mouse.

I can get mouse coord and get screen coords and then do a ray and get the closet object it is clicking on.

My real problem is when I hold down the mouse I want to drag the object (Box) around the screen.
But the getWorldCoordinates basically returns -1 - 1 no matter where the camera is at

			Vector2f click2d = inputManager.getCursorPosition();
			Vector3f click3d = GuiGlobals.camera.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f);
			System.out.println("spatial "+spatial.getWorldTranslation()+" Cursor 2d pos:"+ click2d +" 3d space "+click3d+" cam "+GuiGlobals.camera.getLocation());


So if I’m in the upper left corner of the screen I see

Upper Left corner of screen
spatial (-0.73191965, 0.41421354, 1.0) Cursor 2d pos:(6.0, 1938.0) 3d space (-0.73191965, 0.4133595, 1.0) cam (0.0, 0.0, 10.0)
spatial (-0.73191965, 0.4133595, 1.0) Cursor 2d pos:(5.0, 1938.0) 3d space (-0.7323467, 0.4133595, 1.0) cam (0.0, 0.0, 10.0)

Lower right corner of the screen output
spatial (0.73063856, -0.41165137, 1.0) Cursor 2d pos:(3431.0, 5.0) 3d space (0.73063856, -0.41207844, 1.0) cam (0.0, 0.0, 10.0)
spatial (0.73063856, -0.41207844, 1.0) Cursor 2d pos:(3432.0, 5.0) 3d space (0.7310656, -0.41207844, 1.0) cam (0.0, 0.0, 10.0)

Spatial original location:
spatial (-2.0, 0.0, 1.0)

The spatial is basically in the middle of the screen based on camera location. But when I click on the screen I’m getting x of basically -1 to 1 and also for y.

How do I convert that to world coordinates.

Thanks,

At 0 distance away from the eye, the point is at best at the near plane and so will give you strange numbers.

If you want the point in 3D space at the objects distance from the camera then you should use that.

The javadoc explains this and how to use the parameter:
https://javadoc.jmonkeyengine.org/v3.5.1-stable/com/jme3/renderer/Camera.html#getWorldCoordinates-com.jme3.math.Vector2f-float-

…but somehow gets cut off before referring to the Camera (jMonkeyEngine3) method for translating from world distance to view space distance.

So I changed it to this and now it works. Thanks for the info, it works great.

			Vector2f click2d = inputManager.getCursorPosition();
			float dist = spatial.getWorldTranslation().distance(GuiGlobals.camera.getLocation());
			float projZ = GuiGlobals.camera.getViewToProjectionZ(dist);

			Vector3f click3d = GuiGlobals.camera.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 	projZ);

If you find that the position distorts near the edge of the screen then it will be because the distance should be planar from the camera instead of a straight circular distance.

Vector3f relativeVector = spatial.getWorldTranslation().subtract(cam.getLocation());
float dist = cam.getDirection().dot(relativeVector)

1 Like