2d coordinates

I want to make so that when pressing by a mouse on the screen objects appear exactly on the place where where pressing in meter from the camera. I tried
[java] Vector2f click2d = inputManager.getCursorPosition();
Vector3f click3d = cam.getWorldCoordinates(
new Vector2f(click2d.x, click2d.y), 0f).clone();
makeSmth(click3d);[/java]
but the object does not appear in the right place but closer to the center. How can I get the correct coordinates?

you will want to do something like this:

[java]
Vector2f click2d = inputManager.getCursorPosition();
Vector3f origin = cam.getWorldCoordinates(click2d, 0f);
Vector3f far = cam.getWorldCoordinates(click2d, 1f);

Vector3f direction = far.subtract(origin);
spatial.setLocalTranslation(origin.add(direction.normalize().multLocal(2)));[/java]

Note that this will put it 2 units (meters) infront of the camera, as we have the near clipping plane set to 1 unit by default, so you wouldn’t be able to see it very well otherwise.

1 Like

Unfortunately, it not absolutely that I meant.

@Phaser said: I want to make so that when pressing by a mouse on the screen objects appear exactly on the place where where pressing in meter from the camera. I tried [java] Vector2f click2d = inputManager.getCursorPosition(); Vector3f click3d = cam.getWorldCoordinates( new Vector2f(click2d.x, click2d.y), 0f).clone(); makeSmth(click3d);[/java] but the object does not appear in the right place but closer to the center. How can I get the correct coordinates?

Passing 0 as the last parameter to getWorldCoordinates() will put the objects right at the near plane. This is probably not what you want.

If you want your object some meters from the camera then convert those meters into the view space distance and pass that as the last parameter.
http://hub.jmonkeyengine.org/javadoc/com/jme3/renderer/Camera.html#getViewToProjectionZ(float)

1 Like

My main task consists in that the object was directly under the cursor, distance to the screen not so important, http://hub.jmonkeyengine.org/forum/topic/2d-coordinates/#post-236317

Take a look at the mouse picking tutorial (from the documentation tutorials links at the top of this page). That should be easily modifiable to give what you want.

1 Like
@Phaser said: I want to make so that when pressing by a mouse on the screen objects appear exactly on the place where where pressing in meter from the camera.

“in meter from the camera”

…but in your original version, this was “zero meters from the camera near plane”… which I’m guessing is not what you want.

1 Like
<cite>@Phaser said:</cite> Unfortunately, it not absolutely that I meant.

yeh, on second glance, without changes mine looks like it will only work with camera at the world origin. And actually it would also put it 3 units away from the camera origin, (2 units + near plane of 1).

@pspeed 's method looks better, if you attach your object to a node at the world origin, then this should just work:

[java]
float viewToProjectionZ = cam.getViewToProjectionZ(2);
Vector3f position = cam.getWorldCoordinates(inputManager.getCursorPosition(), viewToProjectionZ);
spatial.setLocalTranslation(position);[/java]

This should put it 2 units infront of the camera’s local -Z coordinate, at the correct mouse location. This doesn’t take into account rotation, so you will need to handle that as well (if desired).

1 Like
<cite>@wezrule said:</cite> yeh, on second glance, without changes mine looks like it will only work with camera at the world origin. And actually it would also put it 3 units away from the camera origin, (2 units + near plane of 1). ...
Strange, but I can't find this method.
1 Like

It was added in November 2012

https://code.google.com/p/jmonkeyengine/source/diff?spec=svn9934&r=9934&format=side&path=/trunk/engine/src/core/com/jme3/renderer/Camera.java

Have you updated to the latest stable? if so try download the nightly version

1 Like
<cite>@wezrule said:</cite> It was added in November 2012

https://code.google.com/p/jmonkeyengine/source/diff?spec=svn9934&r=9934&format=side&path=/trunk/engine/src/core/com/jme3/renderer/Camera.java

Have you updated to the latest stable? if so try download the nightly version

Where I can find it? The link at the top of this page doesn’t work :frowning:

In the jMEDK, go to Tools > Plugin -> Settings -> Nightly

Then do Help -> Check for updates. Although I think the build/update server is a bit screwed at the moment. In which case, you may have to build from svn yourself.

Otherwise you can just make your own utility function for it in the meanwhile:

[java] public float getViewToProjectionZ(float viewZPos) {
float far = cam.getFrustumFar();
float near = cam.getFrustumNear();
float a = far / (far - near);
float b = far * near / (near - far);
return a + b / viewZPos;
}[/java]

1 Like

You shouldn’t need nightly for that. It’s almost a year old so I’m betting it went into RC2 or at least a stable.

OP, what version of JME are you running?!?

Possibly, RC2 was released in Oct 2012. Not sure when/if there was a stable released after that