# Display.getScreenCoordinates() uses Vector3f, why?

i am trying to convert world coordinates to screen coordinates. I am wondering why it uses a Vector3f to give on screen coordinates, what does the z value mean? thanks.

And am i correcting for thinking that it gives the onscreen value for a location in the scene graph?

Yes, you are right. Where X and Y are the pixel coordinates the world location would be rendered to. The Z component gives the distance to the screen relative to near and far plane (the value is between 0 and 1 if the world location is between near and far plane).

thank irrisor! But i am still a little confused as to what the Z value is. why is it between 0-1?

yes, good question: why must it be mapped to 0…1 ?

is the computation somehow easier then?

i've seen a mode in another game engine, where the far plane could be at infinity.

well, 0…1, 1…1000, -100…500, etc. it's all the same

colors are definded between 0 and 1 too. maybe the guys just wanted to keep those limits and not introduce some new ones

ok what do you mean by the near and far plane? are you referring to the camera's frustum? Also would a value of 0.5 for the z mean that the Vector3f location lies 1/2 way between the near and far plane?

Ogli said:

yes, good question: why must it be mapped to 0..1 ?
is the computation somehow easier then?

It has to be mapped to 0..1 because OpenGL uses this values in z-buffer, for perspective correct texture mapping, fog et caetera, and it expects it to be between 0 and 1.

i've seen a mode in another game engine, where the far plane could be at infinity.

Even in jME it can be - only infinity in worldSpace would transform then to 1.0 in "screenSpace".

Also would a value of 0.5 for the z mean that the Vector3f location lies 1/2 way between the near and far plane?

In this particular case yes, but only because (0.5)-1 = 0.5.

Hmm... Actually in OpenGL I think z lies between -1 and 1.
It's not linear - it's 1/z (images taken from http://www.dgp.toronto.edu/~ah/csc418/fall_2001/notes/ogl_vvol.html):

ok that helps me understand the z value, the issue im having is this. I have a JMEDesktop setup. I want it to be so that when i roll over a box with my mouse a component from the JMEDesktop moves to where the box would be. So what i did was get the Location of the box in the world by using getScreenCoordinates(node.getLocalTranslation(), position); Then told the component to go to the x, y of position. I then realized getScreenCoordinates and JMEDEsktop use different axies and flipped the y accordingly, but the component is still off. Any clue what is going on?

Use world, not local, translation:

getScreenCoordinates(node.getWorldTranslation(), position).

…and as for the z range: OpenGL uses -1…1 but getScreenCoordinates scales it to 0…1, and getWorldCoordinates expects z to be also in 0…1 range. So in jME z range in "screen space" is 0…1. Period.

oh, so that is what the the z for getWorldCoordinates() really means,

now im still having troubles with my GUI. getScreenCoordinates can only be be within the screen resolution, no? if they resolution is 800x600 and getScreenCoordinates gives me the point (810,610) then it isn't visible on screen? I am not really sure.

edit: ok all fixed, various errors of mine were at fault