Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/main/java/net/theevilreaper/aves/util/Vectors.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
import net.minestom.server.coordinate.Vec;
import org.jetbrains.annotations.Contract;

import java.security.SecureRandom;
import java.util.concurrent.ThreadLocalRandom;

/**
* The class contains some useful method to work with vectors.
* Utility class providing mathematical vector operations and generation functions
* tailored for Minecraft server development using Minestom's {@link Vec} and {@link Pos}.
* All random vector computations leverage non-blocking, thread-safe {@link ThreadLocalRandom} instances
* for high-throughput performance across server tick workers.
*
* @author theEvilReaper
* @version 1.0.1
* @version 1.1.0
* @since 1.0.0
*/
public final class Vectors {

private static final SecureRandom random = new SecureRandom();

private Vectors() {
}

Expand All @@ -25,8 +26,8 @@ private Vectors() {
*
* @return a new vector with random values
*/
@Contract(pure = true)
public static Vec getRandomVector() {
var random = ThreadLocalRandom.current();
double x = random.nextDouble() * 2.0D - 1.0D;
double y = random.nextDouble() * 2.0D - 1.0D;
double z = random.nextDouble() * 2.0D - 1.0D;
Expand All @@ -51,9 +52,8 @@ public static Vec getBackVector(Pos location) {
*
* @return a new vector with random values
*/
@Contract(pure = true)
public static Vec getRandomCircleVector() {
double rnd = random.nextDouble() * 2.0D * Math.PI;
double rnd = ThreadLocalRandom.current().nextDouble() * 2.0D * Math.PI;
double x = Math.cos(rnd);
double z = Math.sin(rnd);
return new Vec(x, 0.0D, z);
Expand All @@ -63,10 +63,11 @@ public static Vec getRandomCircleVector() {
* Calculates the angle between a 2D vector and the positive x-axis in a Cartesian coordinate system.
* The angle is measured in radians and ranges from -π to π radians.
*
* @param vector the vector for which to calculate the angle
* @param vector the vector for which to calculate the angle
* @return the angle between the vector and the positive x-axis in radians
*/
@Contract(pure = true)
public static double angleToXAxis(Vec vector) {
return Math.atan2(vector.x(), vector.y());
return Math.atan2(vector.y(), vector.x());
}
}
6 changes: 4 additions & 2 deletions src/test/java/net/theevilreaper/aves/util/VectorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ void testGetRandomCircleVector() {
}

@Test
void angleToXAxis() {
assertNotSame(0.7853981633974483, Vectors.angleToXAxis(Vec.ZERO));
void testAngleToXAxis() {
assertEquals(0.0, Vectors.angleToXAxis(new Vec(1, 0, 0)), 1e-6, "Vector along positive X axis should have angle 0");
assertEquals(Math.PI / 2, Vectors.angleToXAxis(new Vec(0, 1, 0)), 1e-6, "Vector along positive Y axis should have angle PI/2");
assertEquals(Math.PI / 4, Vectors.angleToXAxis(new Vec(1, 1, 0)), 1e-6, "Vector (1, 1) should have angle PI/4");
}
}
Loading