Distance from camera to visible object

I think this might be a really noobie question, but how can I cast a ray from the camera to any visible object? I want to do this in order to determine the distance to each object

1 Like

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking.
There’s also a HelloPicking example that comes with the jme engine.

Thanks. I know this one and I’m currently using it. But the direction of the ray here is dictated by the direction of the camera. I am interested in determining the direction towards any visible object, not based on the camera’s direction.

I found something that might the right solution:

[java]
final Vector3f objectLocation = spatial.getWorldTranslation();
float distance = cam.distanceToNearPlane(objectLocation);
[/java]

Will this do?

Ah ok.
I really don’t know about that method.
Didn’t try it, but guess if ray casting towards the center of the objects is enough for your needs, something like this might work:
Ray ray = new Ray(cam.getLocation(), geometryYouAreInterestedIn.getWorldLocation().substract(cam.getLocation()).normalize());

The camera has a location (getLocation) which returns a Vector3f, which has the method distance(Vector3f otherVec).

So…

[java]
cam.getLocation().distance(obj.getLocalTranslation());
[/java]

Won’t care what direction the camera is facing. Or even if it is the camera… etc, etc, amen.

1 Like