double distance = xz1.distance(xz2); Vector2d p1 = Vector2d.UNIT_Y.mul(eyePos.getY()); Vector2d p2 = new Vector2d(distance, targetPos.getY()); Vector2d v1 = p2.sub(p1); Vector2d v2 = Vector2d.UNIT_X.mul(distance); final double pitchRad = Math.acos(v1.dot(v2) / (v1.length() * v2.length())); final double pitchDeg = pitchRad * 180 / Math.PI * (-v1.getY() / Math.abs(v1.getY())); v1 = p2.sub(p1); v2 = Vector2d.UNIT_Y.mul(v1.getY()); double yawRad = Math.acos(v1.dot(v2) / (v1.length() * v2.length())); double yawDeg = yawRad * 180 / Math.PI; if (v1.getX() < 0 && v1.getY() < 0) { yawDeg = 180 - yawDeg; } else if (v1.getX() > 0 && v1.getY() < 0) { yawDeg = 270 - (90 - yawDeg); } else if (v1.getX() > 0 && v1.getY() > 0) { yawDeg = 270 + (90 - yawDeg);
/** * Returns a new transform representing a rotation in the xy plane, around a * given point. The rotation is given is half turns. The actual rotation is * {@code halfTurns * 180}. The tile corner flags change the point to be the * tile corner or edge instead of the center. When both flags are false, the * center is used. When only one is true the edge on the opposite axis to * the flag is used. When both are true the upper corner is used. * * @param halfTurns The number of half turns in this rotation * @param point The point of rotation, as tile coordinates * @param tileCornerX Whether or not to use the corner of the tile instead * of the center on the x axis * @param tileCornerY Whether or not to use the corner of the tile instead * of the center on the y axis * @return The new rotation transform */ public static DiscreteTransform2 fromRotation(int halfTurns, Vector2i point, boolean tileCornerX, boolean tileCornerY) { Vector2d pointDouble = point.toDouble(); if (tileCornerX) { pointDouble = pointDouble.add(0.5, 0); } if (tileCornerY) { pointDouble = pointDouble.add(0, 0.5); } return new DiscreteTransform2( Matrix3d.createTranslation(pointDouble.negate()).rotate(Complexd.fromAngleDeg(halfTurns * 180)).translate(pointDouble)); }
public Vector2d transform(double x, double y) { return new Vector2d( m00 * x + m01 * y, m10 * x + m11 * y); }
p1.sub(o); p2.sub(o); Vector2d d = new Vector2d(); d.sub(p2, p1); double dSq = d.lengthSquared();
/** * Creates a new complex from the angle defined from the first to the second vector. * * @param from The first vector * @param to The second vector * @return The complex defined by the angle between the vectors */ public static Complexd fromRotationTo(Vector2d from, Vector2d to) { return fromAngleRad(TrigMath.acos(from.dot(to) / (from.length() * to.length()))); }
/** * 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 Vector2d lerp(Vector2d a, Vector2d b, double percent) { return a.mul(1 - percent).add(b.mul(percent)); }
public Vector2d mul(float x, float y) { return mul((double) x, (double) y); }
public Vector2d sub(float x, float y) { return sub((double) x, (double) y); }
public double distance(float x, float y) { return distance((double) x, (double) y); }
public Vector2d add(Vector2d v) { return add(v.x, v.y); }
public double dot(Vector2d v) { return dot(v.x, v.y); }
@Override public int compareTo(Vector2d v) { return (int) Math.signum(lengthSquared() - v.lengthSquared()); }
@Override public Vector2d toDouble() { return new Vector2d(x, y); }