jmonkeyengine icon indicating copy to clipboard operation
jmonkeyengine copied to clipboard

Add the screenPointToRay method to the Camera class

Open capdevon opened this issue 5 years ago • 1 comments

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

capdevon avatar May 05 '21 11:05 capdevon

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"

pspeed42 avatar May 05 '21 14:05 pspeed42

Unless there's substantial discussion, I plan to close this issue in about 48 hours.

capdevon avatar Feb 09 '23 14:02 capdevon