Wyatt Gillette пре 7 месеци
родитељ
комит
d411fa3071
1 измењених фајлова са 43 додато и 3 уклоњено
  1. 43 3
      jme3-core/src/main/java/com/jme3/renderer/Camera.java

+ 43 - 3
jme3-core/src/main/java/com/jme3/renderer/Camera.java

@@ -42,6 +42,7 @@ import com.jme3.math.FastMath;
 import com.jme3.math.Matrix4f;
 import com.jme3.math.Plane;
 import com.jme3.math.Quaternion;
+import com.jme3.math.Ray;
 import com.jme3.math.Vector2f;
 import com.jme3.math.Vector3f;
 import com.jme3.math.Vector4f;
@@ -1570,6 +1571,40 @@ public class Camera implements Savable, Cloneable {
         return store;
     }
 
+    /**
+     * Returns a ray going from camera through a screen point.
+     * <p>
+     * Resulting ray is in world space, starting on the near plane
+     * of the camera and going through position's (x,y) pixel coordinates on the screen.
+     * 
+     * @param click2d A {@link Vector2f} representing the 2D screen coordinates (in pixels)
+     * @return A {@link Ray} object representing the picking ray in world coordinates.
+     *
+     * <h3>Usage Example:</h3>
+     * <pre>{@code
+     * Ray pickingRay = cam.screenPointToRay(inputManager.getCursorPosition());
+     *
+     * // Now 'pickingRay' can be used for intersection tests with 3D objects
+     * // e.g., pickingRay.intersects(someSpatial.getWorldBound());
+     * }</pre>
+     */
+    public Ray screenPointToRay(Vector2f click2d) {
+        TempVars vars = TempVars.get();
+        Vector3f nearPoint = vars.vect1;
+        Vector3f farPoint = vars.vect2;
+
+        // Get the world coordinates for the near and far points
+        getWorldCoordinates(click2d, 0, nearPoint);
+        getWorldCoordinates(click2d, 1, farPoint);
+
+        // Calculate direction and normalize
+        Vector3f direction = farPoint.subtractLocal(nearPoint).normalizeLocal();
+
+        Ray ray = new Ray(nearPoint, direction);
+        vars.release();
+        return ray;
+    }
+
     /**
      * Returns the display width.
      *
@@ -1590,9 +1625,14 @@ public class Camera implements Savable, Cloneable {
 
     @Override
     public String toString() {
-        return "Camera[location=" + location + "\n, direction=" + getDirection() + "\n"
-                + "res=" + width + "x" + height + ", parallel=" + parallelProjection + "\n"
-                + "near=" + frustumNear + ", far=" + frustumFar + "]";
+        return getClass().getSimpleName()
+                + "[location=" + location
+                + ", direction=" + getDirection()
+                + ", res=" + width + "x" + height
+                + ", parallel=" + parallelProjection
+                + ", near=" + frustumNear
+                + ", far=" + frustumFar
+                + "]";
     }
 
     @Override