/** * Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead. * * @param v The vector to attempt to normalize * @return The normalized vector, or the zero vector if it couldn't be normalized. */ public static Vector3f normalizeSafe(Vector3f v) { try { return v.normalize(); } catch (ArithmeticException ex) { return Vector3f.ZERO; } }
/** * Attempts to normalize a vector. If this fails, the method catches the exception and return a zero vector of the same dimension instead. * * @param v The vector to attempt to normalize * @return The normalized vector, or the zero vector if it couldn't be normalized. */ public static Vector3f normalizeSafe(Vector3f v) { try { return v.normalize(); } catch (ArithmeticException ex) { return Vector3f.ZERO; } }
default Vector3f getDirectionVector() { Rotation rotation = getRotation(); double y = -Math.sin(Math.toRadians(rotation.getPitch())); double xz = Math.cos(Math.toRadians(rotation.getPitch())); double x = -xz * Math.sin(Math.toRadians(rotation.getYaw())); double z = xz * Math.cos(Math.toRadians(rotation.getYaw())); return new Vector3f(x, y, z).normalize(); }
/** * Creates a "look at" matrix for the given eye point. * * @param size The size of the matrix, minimum of 4 * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static MatrixNf createLookAt(int size, Vector3f eye, Vector3f at, Vector3f up) { if (size < 4) { throw new IllegalArgumentException("Minimum matrix size is 4"); } final Vector3f f = at.sub(eye).normalize(); up = up.normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f).normalize(); final MatrixNf mat = new MatrixNf(size); mat.set(0, 0, s.getX()); mat.set(0, 1, s.getY()); mat.set(0, 2, s.getZ()); mat.set(1, 0, u.getX()); mat.set(1, 1, u.getY()); mat.set(1, 2, u.getZ()); mat.set(2, 0, -f.getX()); mat.set(2, 1, -f.getY()); mat.set(2, 2, -f.getZ()); return mat.translate(eye.mul(-1).toVectorN()); }
/** * Creates a "look at" matrix for the given eye point. * * @param size The size of the matrix, minimum of 4 * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static MatrixNf createLookAt(int size, Vector3f eye, Vector3f at, Vector3f up) { if (size < 4) { throw new IllegalArgumentException("Minimum matrix size is 4"); } final Vector3f f = at.sub(eye).normalize(); up = up.normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f).normalize(); final MatrixNf mat = new MatrixNf(size); mat.set(0, 0, s.getX()); mat.set(0, 1, s.getY()); mat.set(0, 2, s.getZ()); mat.set(1, 0, u.getX()); mat.set(1, 1, u.getY()); mat.set(1, 2, u.getZ()); mat.set(2, 0, -f.getX()); mat.set(2, 1, -f.getY()); mat.set(2, 2, -f.getZ()); return mat.translate(eye.mul(-1).toVectorN()); }
final Vector3f bn = new Vector3f(b.getX() / radiusSquared, halfHeight - b.getY(), b.getZ() / radiusSquared).normalize(); final Vector3f nextBN = new Vector3f(nextB.getX() / radiusSquared, halfHeight - nextB.getY(), nextB.getZ() / radiusSquared).normalize(); final Vector3f tn = bn.add(nextBN).normalize();
triangle.setV0(triangle.getV0().normalize().mul(radius)); triangle.setV1(triangle.getV1().normalize().mul(radius)); triangle.setV2(triangle.getV2().normalize().mul(radius)); } else { addVector(positions, vt0); addVector(normals, vt0.normalize()); addedVertices.put(vt0, index); addAll(indices, index++); } else { addVector(positions, vt1); addVector(normals, vt1.normalize()); addedVertices.put(vt1, index); addAll(indices, index++); } else { addVector(positions, vt2); addVector(normals, vt2.normalize()); addedVertices.put(vt2, index); addAll(indices, index++);
/** * Creates a "look at" matrix for the given eye point. * * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static Matrix4f createLookAt(Vector3f eye, Vector3f at, Vector3f up) { final Vector3f f = at.sub(eye).normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f); final Matrix4f mat = new Matrix4f( s.getX(), s.getY(), s.getZ(), 0, u.getX(), u.getY(), u.getZ(), 0, -f.getX(), -f.getY(), -f.getZ(), 0, 0, 0, 0, 1); return mat.translate(eye.negate()); }
/** * Creates a "look at" matrix for the given eye point. * * @param eye The position of the camera * @param at The point that the camera is looking at * @param up The "up" vector * @return A rotational transform that corresponds to a camera looking at the given point */ public static Matrix4f createLookAt(Vector3f eye, Vector3f at, Vector3f up) { final Vector3f f = at.sub(eye).normalize(); final Vector3f s = f.cross(up).normalize(); final Vector3f u = s.cross(f); final Matrix4f mat = new Matrix4f( s.getX(), s.getY(), s.getZ(), 0, u.getX(), u.getY(), u.getZ(), 0, -f.getX(), -f.getY(), -f.getZ(), 0, 0, 0, 0, 1); return mat.translate(eye.negate()); }
final Vector3f n = new Vector3f(t.getX(), 0, t.getZ()).normalize();