So I’ve followed the Mouse Picking tutorial (as well as dugg through all the various “mouse picking” topics on this forum) and now have the following code:

String hit = results.getCollision(i).getGeometry().getName();

infoPanel.setText(i + ": " + hit + " at " + pt + ", " + dist + “units away”);

}

The above displays the coordinates of the resulting collision. The problem that I am having (and I suspect it has to do with the z coordinates of vectors pos and dir) is that the ray seems to shoot off at an angle. That is, when I click the water in front of me, it may note that in fact the ray collided with the sky (and vice versa). Also, unless I move the camera, the points that I click appear to remain the same.

Summarizing: I have a problem converting the 2D mouse coordinates into 3D. Any tips would be greatly appreciated. See screenshots below:

you have the screen space coordinates of the mouse (x,y), and you want the coordinates of the point picked in world space.

What i’d do : multilply the invProjMatrix by the mouse coordinates. set the z value of the resultant vector with the length of the ray. this should give you the coordinates of the point in view space.

To get them in world space multiply the invWorldMatrix with the view space coordinates.

Your mouse coordinates are in the Projection Space. you need to translate them into worldspace.

But, with a simple transformation you won’t be able to restore the z coordinate.

The rays is shot in view space. The view space is the camera space with its origin at the position of the camera.

the z axis in view space goes straight forward. So when you shoot the ray, the length of the ray is the z coordinate of the intersecting point in view space. (i assume here that the direction of the ray is the direction of the cam).

mhhh, reading back the tutorial, this line Vector3f pt = results.getCollision(i).getWorldContactPoint(); should return what you need.

lol, a nightmare indeed. Thanks. I will read through it though.

I already use Vector3f pt = results.getCollision(i).getWorldContactPoint(). The problem is that no matter where I click on the screen, it always returns the same point:

I click on the screen. Returns a point. I click somewhere else; still returns the same point.

I move. I click. I get a different point. Now, I continue clicking anyway on the screen, and I will still receive the same point, until I start moving again (so, if, for example, I click at mouse(x=0, y=0) I get the same result than if I click at mouse(x=150, y=100). Hence I suspect that there is a problem with the z axis somewhere. Below the entire code:

private void onMouseClicked() {

ray = new Ray();

Vector2f mouseCords = new Vector2f(mouseX, mouseY);