mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-13 11:15:57 -07:00
SPIGOT-5240: Vector.angle not valid for angles very close to each other
By: md_5 <git@md-5.net>
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package org.bukkit.util;
|
package org.bukkit.util;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.primitives.Doubles;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
@@ -202,7 +203,7 @@ public class Vector implements Cloneable, ConfigurationSerializable {
|
|||||||
* @return angle in radians
|
* @return angle in radians
|
||||||
*/
|
*/
|
||||||
public float angle(@NotNull Vector other) {
|
public float angle(@NotNull Vector other) {
|
||||||
double dot = dot(other) / (length() * other.length());
|
double dot = Doubles.constrainToRange(dot(other) / (length() * other.length()), -1.0, 1.0);
|
||||||
|
|
||||||
return (float) Math.acos(dot);
|
return (float) Math.acos(dot);
|
||||||
}
|
}
|
||||||
|
@@ -114,4 +114,12 @@ public class VectorTest {
|
|||||||
assertEquals(a, v.angle(axis), Vector.getEpsilon());
|
assertEquals(a, v.angle(axis), Vector.getEpsilon());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSmallAngle() {
|
||||||
|
Vector a = new Vector(-0.13154885489775203, 0.0, 0.12210868381700482);
|
||||||
|
Vector b = new Vector(-0.7329152226448059, -0.0, 0.6803199648857117);
|
||||||
|
|
||||||
|
assertTrue(Double.isFinite(a.angle(b)));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user