/** * converts the provided {@link Vector3f} into a {@link Color} object. * * @param vector3f The vector of three floats representing color * @return The color object */ public static Color of(Vector3f vector3f) { return new Color(Math.round(vector3f.getX()), Math.round(vector3f.getY()), Math.round(vector3f.getZ())); }
@Test public void testOfVector3f() { final Vector3f vecGray = new Vector3f(128.0, 128.459, 127.5); final Color colorGray = Color.of(vecGray); assertTrue(Color.GRAY.equals(colorGray)); }
/** * 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()); }
for (int angle = 0; angle < 360; angle += 15) { final double angleRads = Math.toRadians(angle); rim.add(new Vector3f( radius * TrigMath.cos(angleRads), -halfHeight, final Vector3f top = new Vector3f(0, halfHeight, 0); final Vector3f bottomNormal = new Vector3f(0, -1, 0); addVector(positions, new Vector3f(0, -halfHeight, 0));// 0 addVector(normals, bottomNormal); 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();
/** * 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()); }
@Override public void run(Entity entity) { Physics physics = entity.ensureAndGet(Physics.class); if (entity.getMotion().lengthSquared() > 0) { boolean onGroundPreviously = entity.isOnGround(); entity.setPositionFromSystem(entity.getPosition().add(entity.getMotion())); boolean onGroundNow = entity.isOnGround(); if (!onGroundPreviously && onGroundNow) { entity.setPositionFromSystem(new Vector3f(entity.getPosition().getX(), entity.getPosition().getFloorY(), entity.getPosition().getZ())); entity.setMotion(Vector3f.ZERO); } else { entity.setMotion(entity.getMotion().mul(1f - physics.getDrag())); if (!onGroundNow) { entity.setMotion(entity.getMotion().sub(0, physics.getGravity(), 0)); } } } } }
final Vector3f v00 = p01.sub(p00); final Vector3f v01 = p02.sub(p01); final Vector3f v10 = p11.sub(p10); final Vector3f v11 = p12.sub(p11); final Matrix3f m0 = new Matrix3f( v00.getX(), v01.getX(), v10.getX(), v00.getY(), v01.getY(), v10.getY(), v00.getZ(), v01.getZ(), v10.getZ()); final float d0 = m0.determinant(); final Matrix3f m1 = new Matrix3f( v00.getX(), v01.getX(), v11.getX(), v00.getY(), v01.getY(), v11.getY(), v00.getZ(), v01.getZ(), v11.getZ()); final float d1 = m1.determinant(); if (Math.abs(d0) > GenericMath.FLT_EPSILON * 10 || Math.abs(d1) > GenericMath.FLT_EPSILON * 10) { return null; final Vector3f v02 = p00.sub(p02); final Vector3f v12 = p10.sub(p12); if (isZero(v00.cross(v10)) && checkForOverlap(p00, v00, p10, v10)) { return new Vector2i(0, 0); } else if (isZero(v00.cross(v11)) && checkForOverlap(p00, v00, p11, v11)) { return new Vector2i(0, 1); } else if (isZero(v00.cross(v12)) && checkForOverlap(p00, v00, p12, v12)) { return new Vector2i(0, 2); } else if (isZero(v01.cross(v10)) && checkForOverlap(p01, v01, p10, v10)) { return new Vector2i(1, 0); } else if (isZero(v01.cross(v11)) && checkForOverlap(p01, v01, p11, v11)) { return new Vector2i(1, 1);
final Vector3f top = new Vector3f(0, halfHeight + radius, 0); addVector(normals, top.normalize()); final double radTheta = Math.toRadians(theta); final float sinPhi = TrigMath.sin(radPhi); final Vector3f p = new Vector3f( radius * sinPhi * TrigMath.sin(radTheta), radius * TrigMath.cos(radPhi), radius * sinPhi * TrigMath.cos(radTheta)); addVector(positions, p.add(0, halfHeight, 0)); addVector(normals, p.normalize()); final Vector3f bottom = top.negate(); addVector(normals, bottom.normalize()); final double radTheta = Math.toRadians(theta); final float sinPhi = TrigMath.sin(radPhi); final Vector3f p = new Vector3f( radius * sinPhi * TrigMath.sin(radTheta), -radius * TrigMath.cos(radPhi), radius * sinPhi * TrigMath.cos(radTheta)); addVector(positions, p.sub(0, halfHeight, 0)); addVector(normals, p.normalize());
final Vector3f v0 = new Vector3f(0.0f, -1.0f, 0.0f); final Vector3f v1 = new Vector3f(1.0f, 0.0f, 0.0f); final Vector3f v2 = new Vector3f(0.0f, 0.0f, 1.0f); final Vector3f v3 = new Vector3f(-1.0f, 0.0f, 0.0f); final Vector3f v4 = new Vector3f(0.0f, 0.0f, -1.0f); final Vector3f v5 = new Vector3f(0.0f, 1.0f, 0.0f); 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++);
/** * Calculates the linear interpolation between a and b with the given percent * * @param a The first know value * @param b The second know value * @param percent The percent * @return the interpolated vector */ public static Vector3f lerp(Vector3f a, Vector3f b, float percent) { return a.mul(1 - percent).add(b.mul(percent)); }
private Triangle[] subdivide() { final Vector3f e0 = v1.sub(v0).div(2); final Vector3f va = v0.add(e0); final Vector3f e1 = v2.sub(v1).div(2); final Vector3f vb = v1.add(e1); final Vector3f e2 = v0.sub(v2).div(2); final Vector3f vc = v2.add(e2); return new Triangle[]{ new Triangle(v0, va, vc), new Triangle(va, v1, vb), new Triangle(vc, vb, v2), new Triangle(va, vb, vc) }; } }
@Override public Vector3f mul(float a) { return mul(a, a, a); }
public Vector3f add(Vector3f v) { return add(v.x, v.y, v.z); }
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(); }
private static boolean checkForOverlap(Vector3f p0, Vector3f v0, Vector3f p1, Vector3f v1) { final Vector3f p10 = p1.sub(p0); if (!isZero(p10.cross(v0))) { return false; } float t = getNonNaN(p10.div(v0)); if (t >= 0 && t <= 1) { return true; } t = getNonNaN(p0.sub(p1).div(v1)); return t >= 0 && t <= 1; }
public Vector3f project(float x, float y, float z) { final float lengthSquared = x * x + y * y + z * z; if (Math.abs(lengthSquared) < GenericMath.FLT_EPSILON) { throw new ArithmeticException("Cannot project onto the zero vector"); } final float a = dot(x, y, z) / lengthSquared; return new Vector3f(a * x, a * y, a * z); }