# Getting screen coordinates of camera = Infinity? [Solved]

Should be simple enough.

[java]Vector3f screenLoc = sceneViewport.getCamera().getScreenCoordinates(sceneViewport.getCamera().getLocation());[/java]

This should give me the screen coords of the camera, but without fail it gives me “NaN, -Infinity, -Infinity”. I don’t think I’m doing anything wrong. At worse I was expecting a negative or weird Z but not the above.

Anyone could point me to a proper way to get those coordinates? It’d be much appreciated.

Thanks.

The camera is all screen coordinates when you think about it. Seriously, if you find the theoretical ray of every pixel and trace it back to its origin then it’s the camera location.

What are you trying to do with this information?

screen coord of the camera is 0,0 by definition.

Edit : mhh sorry drank too much tonight, it’s 0,0 in clip space (that goes from -1 to 1). What i meant is that it’s the center of your screen whatever happen. so it’s width/2, height/2

Am I to understand that if getScreenCoordinates(…) returns anything not a number (NaN, Infinity) then it’s out of screen? I always thought this would return a negative or weird Z in this case.

@nehon said:
screen coord of the camera is 0,0 by definition.

Don't you mean screen width / 2 and height / 2? 0, 0 is lower left of the screen. Unless I'm misunderstanding.

@pspeed said:
What are you trying to do with this information?

I'm fixing the lens flare. There's something I wanted to test and it meant using the camera location, but I'll use the screen's middle (width and height / 2).
Am I to understand that if getScreenCoordinates(...) returns anything not a number (NaN, Infinity) then it's out of screen? I always thought this would return a negative or weird Z in this case.

It's not really about being "off screen" it's about the fact that math has no way of representing "all values" as a single answer... it's "not a number", ie: NaN.

I'm hoping that I'm making sense here but it feels like this point is still being missed so I'll clarify further...

The camera is at some position. From this position extends a pyramid looking volume called the frustum. The screen represents a slice of this some distance out from the camera. Every pixel on the screen is essentially a "ray" from the camera's location through that pixel on the screen.

So the camera's location on screen is all of the pixels.

A point slightly away from the camera in any direction technically has an answer. It might be negative it might be a really huge number. It might make sense or it might be strange.

But the point _right on_ the camera location has no answer because it's every pixel on the screen. So the question doesn't really make sense and that's why I was trying to tease out what you really meant.
1 Like

I understand the mechanics of what you’re describing Paul, but the camera also has a physical representation of its location in the world; from which point it sees things (in a given direction).

When you use getLocation() shouldn’t the method return the actual location of the camera in the scene? And in case of a chase cam, the relation of it towards the spatial it’s attached to and the “real” camera (distance, rotation, etc).

I’m saying this because if you use setLocation(vec) it will place the cam at those world coordinates.

@pspeed said:
So the camera's location on screen is all of the pixels.

That doesn't make sense at all to me with the camera's getLocation() method.

I have a "targeter" element in the game where I use getScreenCoordinates() on it and when it's behind the screen, that "targeter" still gets drawn (because I haven't taken the time to do what I wanted to do with it properly yet, there are things more urging than that).

[java]
/**
* <code>getLocation</code> retrieves the location vector of the camera.
*
* @return the position of the camera.
* @see Camera#getLocation()
*/
public Vector3f getLocation() {
return location;
}
[/java]

As the above's Javadoc says, the method should return a world coordinates Vector3f of the camera location in the scene, from which I should get half the screen height/width if I use getScreenCoordinates on it.

Am I assuming too much? Could it be that the camera position is in local coordinates instead of world coordinates or something else going on?

You are mixing up the scene graph and what effectively happens when rendering in OpenGL.

@normen said:
You are mixing up the scene graph and what effectively happens when rendering in OpenGL.

I need to understand why getScreenCoordinates on something like "-0.1593323, 271.95956, 531.99506" (a cam location from a println) won't give me a proper screen location.

Here's a little test I just did.

As debug (and fast travel) I have a method to move the ship to planets' location. Putting the following code:

[java]
Vector3f pos = planetSystemNodeList.get(planetPosition).getChild("Planet Body").getWorldTranslation();
playerShip.setLocalTranslation(pos);
playerShip.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
System.out.printf("Moved to planet %d at %sn", planetPosition, pos.toString());
System.out.println("Camera location in world : " + getViewport().getCamera().getLocation());
System.out.println("Camera location on screen : " + getViewport().getCamera().getScreenCoordinates(getViewport().getCamera().getLocation()));
[/java]

Gave me the following:

Moved to planet 0 at (84.975845, -0.9783899, 23.059315)
Camera location in world : (3.6450274, 253.74495, 504.6185)
Camera location on screen : (NaN, -Infinity, -Infinity)

Granted the camera location can't be exactly the same vector as the planet, but it should be a lot closer.

I just don't understand why the camera's getLocation() isn't its proper place in the scene... In fact, I don't understand why getLocation() is there at all if it doesn't reflect its location in the scene.

It might sound as I'm arguing but I'm not. I'm just dismayed and would very much understand.

The thing you are asking is: “Where on the screen is the ray that crosses 0/0/0 (camera location)?” The answer is: “All of them cross 0/0/0”

An image is worth a thousand words (blue is the screen, the lines are the “light rays” towards the camera):

You can ask about the red spot and get the green spot as an answer, but you cannot ask for the yellow spot, its “all of them, NaN”

1 Like
@normen said:
The thing you are asking is: "Where on the screen is the ray that crosses 0/0/0 (camera location)?" The answer is: "All of them cross 0/0/0"
An image is worth a thousand words (blue is the screen, the lines are the "light rays" towards the camera):
You can ask about the red spot and get the green spot as an answer, but you cannot ask for the yellow spot, its "all of them, NaN"

You're actually saying that if a spatial is located at exactly the same location as the camera is, using getScreenCoordinates(...) on it will yield the same NaN, Infinity, Infinity as if it were done on the camera itself. That's what I'm deducing.

On the other hand, if I added/subtracted a fraction to the camera location, I would get a "real" result since that point wouldn't be where everything is converging... hmmm.

Yes. What would actually happen in rl when you place an object directly where the camera is you would get the same result. “Where on the screen is the center of the object?” – “All over the screen”. You have to think about the fact that you go from that 2d space of the screen and try to deduce a 3d information from it.

1 Like

I get it now. The camera location being the focal point of the scene. Everything can be translated to screen coordinates except that exact point in space.

Alright then. Now I understand. Thanks all.

I get it now. The camera location being the focal point of the scene. Everything can be translated to screen coordinates except that exact point in space.

Alright then. Now I understand. :D Thanks all.

:) Yay... enlightenment. :)

I feared there was no point in my belaboring but I'm glad there was.
@pspeed said:
:) Yay... enlightenment. :)

I feared there was no point in my belaboring but I'm glad there was.

AFAIC there's always a point when it leads to understanding. ;) Of course not everyone wants to get the fine details but I'm not part of those. :) The big question is always -if- it'll lead to a successful enlightenment. ;)