Add the screenPointToRay method to the Camera class
hi everyone, could you add this method to the Camera class? This would make the code more intuitive and easier for programmers to understand, hiding the complexity of how Ray is calculated. Obviously it's not a must, but it would definitely help younger programmers, especially in point and click games. What do you think?
For example, the object at the mouse position can be determined with the following code
Camera cam = ...;
Ray ray = cam.screenPointToRay(inputManager.getCursorPosition());
CollisionResults results = new CollisionResults();
rootNode.collideWith(ray, results);
if (results.size() > 0) {
CollisionResult closest = results.getClosestCollision();
float dist = closest.getDistance();
Vector3f point = closest.getContactPoint();
Geometry hit = closest.getGeometry();
System.out.println(" You shot: " + hit + " at " + point + ", " + dist + " wu away.");
}
public class Camera {
...
/**
* Returns a ray going from camera through a screen point. usage is:
* <pre>
* Ray ray = cam.screenPointToRay(inputManager.getCursorPosition());
* </pre>
*/
public Ray screenPointToRay(Vector2f click2d) {
// Convert screen click to 3d position
Vector3f click3d = getWorldCoordinates(new Vector2f(click2d), 0f).clone();
Vector3f dir = getWorldCoordinates(new Vector2f(click2d), 1f).subtractLocal(click3d);
// Aim the ray from the clicked spot forwards.
Ray ray = new Ray(click3d, dir);
return ray;
}
}
Here are some references: https://docs.unity3d.com/Manual/CameraRays.html
If you want to make a set of utility classes for younger programmers then that sounds like a good idea.
I'm kind of against adding this to the core code, though. JME already has too much magic that ends up hurting new developers in the long run (see 99% of SimpleApplication) and I think this would be one of those cases. There are a lot of really valuable lessons about how this is working in those two lines of code that you've effectively hidden.
It's also not really camera's responsibility to deal with this kind of thing... every new line of code is a maintenance burden. (Plus there are already other frameworks like Lemur that hid all of this away even better by even taking care of the clicking for you.)
Also, a "younger programmer" not knowing how to do this is a good indicator that they haven't even looked at the tutorials... since it's like in an obviously titled tutorial: "Hello Picking"
Unless there's substantial discussion, I plan to close this issue in about 48 hours.