Hi all. I’m trying to use Camera.getWorldCoordinates to transform the cursor position to a position in space in front of the camera, but I’m getting some really weird results. I think I am just using the standard approach here:
[java]
Vector2f mouse2d = inputManager.getCursorPosition();
Vector3f mouse3d = app.getCamera().getWorldCoordinates( mouse2d, zPos );
// Print coordinates to screen
mouseText.setText(mouse2d.toString() + “n” + mouse3d.toString());
[/java]
It seems like no matter what value I set zPos to, it behaves as if it were actually set to 0. The only exception is if I set zPos to exactly 1, in which case it suddenly behaves as if it were set to -1000.
Can anyone suggest what I’m doing wrong here? Thanks!
If it was set to zero it would return NaN, is that the case?
Nope, if I set zPos to zero, it returns something like (0, 0, -1) when I point at the center of the screen (if my cam’s at 0,0,0 and facing along negative z axis).
Some other examples:
zPos = 10 → (0, 0, 0.11)
zPos = -100 → (0, 0, -0.01)
zPos = 1 → (0, 0, -1000) (this is 1000 units in front of the camera)
The zPos is dependent on the cam frustum so these values are not strange.
Edit: If you have problems understanding cam.getWorldCoordinates(), do a forum search.
I did some searching and thought I understood what it was supposed to do… I found this from nehon:
zPos is the z coord in view space (also called camera space), meaning, the space which origin is the camera. In this space positive Z go toward the screen.
So, zPos is a distance along the z axis in view space, and getWorldCoordinates() transforms that position in view space to a position in world space? I'm afraid I still don't understand the results that I'm getting. Shouldn't larger zPos values project the point farther away from the camera? And why would a value of 1 suddenly give such a different result?
The value should only be 0-1
Ok, I think I see now. It’s 0.0 to 1.0 as a fraction of the far viewing frustum. Thanks for your help!
@suddenastronaut said:
Ok, I think I see now. It's 0.0 to 1.0 as a fraction of the far viewing frustum. Thanks for your help!
I think it's as a fraction of the distance between the near and far planes of the frustum.
The pixel on screen actually represents a vector in view space (which I'm sure you already know) but sometimes it's better just to treat it that way. So create a vector from the world coordinates of zpos 1 and zpos 0... then normalize it. That's the vector through that pixel on the screen.
...then you can project it to whatever distance you want from the camera.
Alright, that’s a little different from what I was expecting, but it makes sense. Thanks!