Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. /**
  2.  * Takes the mouses position and an objects origin, and calculates a new position along the moveDirection line.
  3.  * @param origin
  4.  * @param moveDirection
  5.  * @return
  6.  */
  7. private static Vector3f projectMouseTo3D(Vector3f origin, Vector3f moveDirection) {
  8.     // Get 2d line
  9.     Vector2f p1 = MatrixUtils.project3Dto2D(origin);
  10.     Vector2f p2 = MatrixUtils.project3Dto2D(origin.add(moveDirection, new Vector3f()));
  11.     Vector2f p3 = MatrixUtils.project3Dto2D(origin.sub(moveDirection, new Vector3f()));
  12.     p2.sub(p1).normalize().mul(256).add(p1);
  13.     p3.sub(p1).normalize().mul(256).add(p1);
  14.  
  15.     // Get closest point to the line from the mouses position
  16.     Vector2f mouse = new Vector2f( (float)RenderableApplication.mouseX, (float)RenderableApplication.mouseY );
  17.     Vector2f closest = getProjectedPointLine( p2, p3, mouse );
  18.    
  19.     // Reproject the closest point to 3d ray
  20.     Vector3f ray = MatrixUtils.project2Dto3D(closest);
  21.    
  22.     // Generate a plane along the moveDirection
  23.     Vector3f normal = getPlaneNormal( moveDirection );
  24.    
  25.     // Return the intersection from ray to normal plane
  26.     return linePlaneIntersection( origin, normal, Pipeline.pipeline_get().getCamera().getPosition().toJoml(), ray);
  27. }
  28.  
  29. /**
  30.  * Calculates a RIGHT VECTOR for a given direction vector.
  31.  * Essentially the same math used when calculating a view matrix right vector.
  32.  * @param directionVector
  33.  * @return
  34.  */
  35. private static Vector3f getPlaneNormal( Vector3f directionVector ) {
  36.     Vector3f up = new Vector3f(0, 0, 1);
  37.     if ( directionVector.z == 1 || directionVector.z == -1 )
  38.         up.set(0,1,0);
  39.  
  40.     float dirX = directionVector.x;
  41.     float dirY = directionVector.y;
  42.     float dirZ = directionVector.z;
  43.     float upX = up.x;
  44.     float upY = up.y;
  45.     float upZ = up.z;
  46.    
  47.     // right = direction x up
  48.     float rightX, rightY, rightZ;
  49.     rightX = dirY * upZ - dirZ * upY;
  50.     rightY = dirZ * upX - dirX * upZ;
  51.     rightZ = dirX * upY - dirY * upX;
  52.     // normalize right
  53.     float invRightLength = 1.0f / (float) Math.sqrt(rightX * rightX + rightY * rightY + rightZ * rightZ);
  54.     rightX *= invRightLength;
  55.     rightY *= invRightLength;
  56.     rightZ *= invRightLength;
  57.    
  58.     return new Vector3f( rightX, rightY, rightZ );
  59. }
  60.  
  61. /**
  62.  * Find the nearest point on line AB from point P
  63.  * @param A
  64.  * @param B
  65.  * @param P
  66.  * @return
  67.  */
  68. private static Vector2f getProjectedPointLine( Vector2f A, Vector2f B, Vector2f P ) {
  69.     Vector2f vectorAP = P.sub(A, new Vector2f());
  70.     Vector2f vectorAB = B.sub(A, new Vector2f());
  71.    
  72.     float magnitudeAB = (float) (Math.pow(vectorAB.x,2)+Math.pow(vectorAB.y,2));
  73.     float ABAPproduct = vectorAB.x*vectorAP.x + vectorAB.y*vectorAP.y;
  74.     float distance = ABAPproduct / magnitudeAB;
  75.    
  76.     return new Vector2f(A.x + vectorAB.x*distance, A.y + vectorAB.y*distance);
  77. }
  78.  
  79. /**
  80.  * Return the point where a line intersects a plane
  81.  * @param planePoint
  82.  * @param planeNormal
  83.  * @param linePoint
  84.  * @param lineDirection
  85.  * @return
  86.  */
  87. private static Vector3f linePlaneIntersection(Vector3f planePoint, Vector3f planeNormal, Vector3f linePoint, Vector3f lineDirection) {
  88.     if (planeNormal.dot(lineDirection) == 0) {
  89.         return null;
  90.     }
  91.  
  92.     float t = (planeNormal.dot(planePoint) - planeNormal.dot(linePoint)) / planeNormal.dot(lineDirection);
  93.     Vector3f ret = lineDirection.mul(t, new Vector3f());
  94.     return ret.add(linePoint);
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement