How to calculate distance between point and camera

I try to calculate the distance between a vector and the location of my camera.

For example:

public float distance(Camera camera, Vector3f target) {
	return camera.getLocation().distance(target);

I don’t understand why the getLocation() method returns a value which is too small. In my case the location of camera should be e.g. (100,100,100) but is (0.34,0.5,1.6).

How do you know?

Note: by default, pressing ‘c’ on the keyboard will dump the current camera location and orientation to console.

i guess you get local(parent node) location and not world(root node) location that you want. (tho i dont remember if camera getLocation() given local or global one and javadoc dont say)

this link can get you understand how Nodes works:

Camera has no parent so it’s position will always be in world space.

OP has not told us why they expect the position to be 100, 100, 100 or how they confirmed it wasn’t. So I don’t know if the problem is on why they think it’s supposed to be 100, 100, 100 or how they checked if it was 100, 100, 100… but the problem is in user space and not JME space.

So until OP posts back with where their misunderstanding/miscommunication is then we’ll just be guessing at random things.

ah right, then i got no idea why OP would expect it to have 100,100,100.

As @pspeed was hinting at, the issue is that you expect the camera to somewhere it isn’t, so posting the code where you set the camera location would be more relevant.

To be honest, I wasn’t even guessing that far.

I don’t know if OP ran through the debugger, added System.out.println(), or was just “guessing” based or an assumption made about ‘target’. (which would be my guess without knowing more)

But yes, we need more information.

The only things that I can say 100% with certainty:

  1. camera location is always where you put it. It never lies.
  2. distance() will accurately calculate the distance. It never lies.

So the problem is outside of the code presented and not a problem with JME but a problem with the user’s code.

I overlooked that’s the camera is wrapped by a ChaseCamera, may be that’s the cause for the “wrong” camera location.

1 Like

I don’t know why but I had to transform my Camera location like this:

public static Vector3f tranformToWorldCoordinate(Vector3f point) {
	point = point.divide(0.001f);
	point = new Vector3f(-1f * point.x, point.z, point.y);
	return point;

Now I can calculate the distance between a point and my camera location

Camera location is always in world space. Always. Like, even when you think it’s not, it is. Always. Always.Always.Always.Always.Always.Always.Always.

So if you have to do all of that to get the distance to “some object” then the object is not really where you think it is. Or you are getting its local translation instead of its world translation.

This is one of these types of “hey, here’s no information at all, what’s wrong?” type of questions. Like “I’m holding a piece of fuzzy red string, can someone tell me how long it is?”

Edit: to be clear, if you want to really figure out what the problem is then you will have to show us more code. You will have to add some System.out.println() calls around your code to show the different values to figure out which of your assumptions is incorrect. Because the issue is 100% in your code.

1 Like