diff --git a/paper-api/src/main/java/org/bukkit/Location.java b/paper-api/src/main/java/org/bukkit/Location.java
index e3f362fdc9..0f74f6d48b 100644
--- a/paper-api/src/main/java/org/bukkit/Location.java
+++ b/paper-api/src/main/java/org/bukkit/Location.java
@@ -167,45 +167,79 @@ public class Location implements Cloneable {
}
/**
- * Sets the yaw of this location
+ * Sets the yaw of this location, measured in degrees.
+ *
+ *
A yaw of 0 or 360 represents the positive z direction.
+ *
A yaw of 180 represents the negative z direction.
+ *
A yaw of 90 represents the negative x direction.
+ *
A yaw of 270 represents the positive x direction.
+ *
+ * Increasing yaw values are the equivalent of turning to your
+ * right-facing, increasing the scale of the next respective axis, and
+ * decreasing the scale of the previous axis.
*
- * @param yaw New yaw
+ * @param yaw new rotation's yaw
*/
public void setYaw(float yaw) {
this.yaw = yaw;
}
/**
- * Gets the yaw of this location
+ * Gets the yaw of this location, measured in degrees.
+ *
+ *
A yaw of 0 or 360 represents the positive z direction.
+ *
A yaw of 180 represents the negative z direction.
+ *
A yaw of 90 represents the negative x direction.
+ *
A yaw of 270 represents the positive x direction.
+ *
+ * Increasing yaw values are the equivalent of turning to your
+ * right-facing, increasing the scale of the next respective axis, and
+ * decreasing the scale of the previous axis.
*
- * @return Yaw
+ * @return the rotation's yaw
*/
public float getYaw() {
return yaw;
}
/**
- * Sets the pitch of this location
+ * Sets the pitch of this location, measured in degrees.
+ *
+ *
A pitch of 0 represents level forward facing.
+ *
A pitch of 90 represents downward facing, or negative y
+ * direction.
+ *
A pitch of -90 represents upward facing, or positive y direction.
+ *
+ * Increasing pitch values the equivalent of looking down.
*
- * @param pitch New pitch
+ * @param pitch new incline's pitch
*/
public void setPitch(float pitch) {
this.pitch = pitch;
}
/**
- * Gets the pitch of this location
+ * Sets the pitch of this location, measured in degrees.
+ *
+ *
A pitch of 0 represents level forward facing.
+ *
A pitch of 90 represents downward facing, or negative y
+ * direction.
+ *
A pitch of -90 represents upward facing, or positive y direction.
+ *
+ * Increasing pitch values the equivalent of looking down.
*
- * @return Pitch
+ * @return the incline's pitch
*/
public float getPitch() {
return pitch;
}
/**
- * Gets a Vector pointing in the direction that this Location is facing
+ * Gets a unit-vector pointing in the direction that this Location is
+ * facing.
*
- * @return Vector
+ * @return a vector pointing the direction of this location's {@link
+ * #getPitch() pitch} and {@link #getYaw() yaw}
*/
public Vector getDirection() {
Vector vector = new Vector();
@@ -215,14 +249,47 @@ public class Location implements Cloneable {
vector.setY(-Math.sin(Math.toRadians(rotY)));
- double h = Math.cos(Math.toRadians(rotY));
+ double xz = Math.cos(Math.toRadians(rotY));
- vector.setX(-h * Math.sin(Math.toRadians(rotX)));
- vector.setZ(h * Math.cos(Math.toRadians(rotX)));
+ vector.setX(-xz * Math.sin(Math.toRadians(rotX)));
+ vector.setZ(xz * Math.cos(Math.toRadians(rotX)));
return vector;
}
+ /**
+ * Sets the {@link #getYaw() yaw} and {@link #getPitch() pitch} to point
+ * in the direction of the vector.
+ */
+ public Location setDirection(Vector vector) {
+ /*
+ * Sin = Opp / Hyp
+ * Cos = Adj / Hyp
+ * Tan = Opp / Adj
+ *
+ * x = -Opp
+ * z = Adj
+ */
+ final double _2PI = 2 * Math.PI;
+ final double x = vector.getX();
+ final double z = vector.getZ();
+
+ if (x == 0 && z == 0) {
+ pitch = vector.getY() > 0 ? -90 : 90;
+ return this;
+ }
+
+ double theta = Math.atan2(-x, z);
+ yaw = (float) Math.toDegrees((theta + _2PI) % _2PI);
+
+ double x2 = NumberConversions.square(x);
+ double z2 = NumberConversions.square(z);
+ double xz = Math.sqrt(x2 + z2);
+ pitch = (float) Math.toDegrees(Math.atan(-vector.getY() / xz));
+
+ return this;
+ }
+
/**
* Adds the location by another.
*
diff --git a/paper-api/src/test/java/org/bukkit/LocationTest.java b/paper-api/src/test/java/org/bukkit/LocationTest.java
new file mode 100644
index 0000000000..fa24776335
--- /dev/null
+++ b/paper-api/src/test/java/org/bukkit/LocationTest.java
@@ -0,0 +1,196 @@
+package org.bukkit;
+
+import static org.hamcrest.Matchers.*;
+import static org.junit.Assert.*;
+
+import java.util.List;
+import java.util.Random;
+
+import org.bukkit.util.Vector;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
+
+import com.google.common.collect.ImmutableList;
+
+@RunWith(Parameterized.class)
+public class LocationTest {
+ private static final double δ = 1.0 / 1000000;
+ /**
+ *