Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Takes the mouses position and an objects origin, and calculates a new position along the moveDirection line.
- * @param origin
- * @param moveDirection
- * @return
- */
- private static Vector3f projectMouseTo3D(Vector3f origin, Vector3f moveDirection) {
- // Get 2d line
- Vector2f p1 = MatrixUtils.project3Dto2D(origin);
- Vector2f p2 = MatrixUtils.project3Dto2D(origin.add(moveDirection, new Vector3f()));
- Vector2f p3 = MatrixUtils.project3Dto2D(origin.sub(moveDirection, new Vector3f()));
- p2.sub(p1).normalize().mul(256).add(p1);
- p3.sub(p1).normalize().mul(256).add(p1);
- // Get closest point to the line from the mouses position
- Vector2f mouse = new Vector2f( (float)RenderableApplication.mouseX, (float)RenderableApplication.mouseY );
- Vector2f closest = getProjectedPointLine( p2, p3, mouse );
- // Reproject the closest point to 3d ray
- Vector3f ray = MatrixUtils.project2Dto3D(closest);
- // Generate a plane along the moveDirection
- Vector3f normal = getPlaneNormal( moveDirection );
- // Return the intersection from ray to normal plane
- return linePlaneIntersection( origin, normal, Pipeline.pipeline_get().getCamera().getPosition().toJoml(), ray);
- }
- /**
- * Calculates a RIGHT VECTOR for a given direction vector.
- * Essentially the same math used when calculating a view matrix right vector.
- * @param directionVector
- * @return
- */
- private static Vector3f getPlaneNormal( Vector3f directionVector ) {
- Vector3f up = new Vector3f(0, 0, 1);
- if ( directionVector.z == 1 || directionVector.z == -1 )
- up.set(0,1,0);
- float dirX = directionVector.x;
- float dirY = directionVector.y;
- float dirZ = directionVector.z;
- float upX = up.x;
- float upY = up.y;
- float upZ = up.z;
- // right = direction x up
- float rightX, rightY, rightZ;
- rightX = dirY * upZ - dirZ * upY;
- rightY = dirZ * upX - dirX * upZ;
- rightZ = dirX * upY - dirY * upX;
- // normalize right
- float invRightLength = 1.0f / (float) Math.sqrt(rightX * rightX + rightY * rightY + rightZ * rightZ);
- rightX *= invRightLength;
- rightY *= invRightLength;
- rightZ *= invRightLength;
- return new Vector3f( rightX, rightY, rightZ );
- }
- /**
- * Find the nearest point on line AB from point P
- * @param A
- * @param B
- * @param P
- * @return
- */
- private static Vector2f getProjectedPointLine( Vector2f A, Vector2f B, Vector2f P ) {
- Vector2f vectorAP = P.sub(A, new Vector2f());
- Vector2f vectorAB = B.sub(A, new Vector2f());
- float magnitudeAB = (float) (Math.pow(vectorAB.x,2)+Math.pow(vectorAB.y,2));
- float ABAPproduct = vectorAB.x*vectorAP.x + vectorAB.y*vectorAP.y;
- float distance = ABAPproduct / magnitudeAB;
- return new Vector2f(A.x + vectorAB.x*distance, A.y + vectorAB.y*distance);
- }
- /**
- * Return the point where a line intersects a plane
- * @param planePoint
- * @param planeNormal
- * @param linePoint
- * @param lineDirection
- * @return
- */
- private static Vector3f linePlaneIntersection(Vector3f planePoint, Vector3f planeNormal, Vector3f linePoint, Vector3f lineDirection) {
- if (planeNormal.dot(lineDirection) == 0) {
- return null;
- }
- float t = (planeNormal.dot(planePoint) - planeNormal.dot(linePoint)) / planeNormal.dot(lineDirection);
- Vector3f ret = lineDirection.mul(t, new Vector3f());
- return ret.add(linePoint);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement