Package dev.boze.api.utility
Class MathHelper
java.lang.Object
dev.boze.api.utility.MathHelper
MathHelper provides comprehensive mathematical utilities.
This class consolidates useful mathematical operations,
including rotation calculations, vector operations, interpolation, and collision math.
All methods are optimized for performance and handle edge cases gracefully.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleangleDifference(double angle1, double angle2) Calculates the smallest angle difference between two angles.static doublebringCloser(double value, double goal, double increment) Gradually brings a value closer to a goal by a specified increment.static float[]calculateRotation(net.minecraft.util.math.Vec3d eyes, net.minecraft.util.math.Vec3d target) Calculates the yaw and pitch rotation needed to face a target position.static doubleclamp(double value, double min, double max) Clamps a double value to a specified range.static intclamp(int value, int min, int max) Clamps an integer value to a specified range.static net.minecraft.util.math.Vec3dclampToBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Clamps a point to stay within a box's boundaries.static net.minecraft.util.math.Vec3dclosestPointToBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Finds the closest point on a box's surface to a given point.static net.minecraft.util.math.Vec3dcrossProduct(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the cross product of two vectors.static doubledegreesToRadians(double degrees) Converts degrees to radians.static doubledistance(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the distance between two points.static doubledotProduct(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the dot product of two vectors.static doublefastCos(double radians) Fast cosine calculation optimized for x86 processors.static doublefastSin(double radians) Fast sine calculation optimized for x86 processors.static net.minecraft.util.math.Vec3dfindClosestPointOnBox(net.minecraft.util.math.Box box, net.minecraft.util.math.Vec3d point) Finds the closest point on a box's surface to a given point.static net.minecraft.util.math.Vec3dgetBestAimPoint(net.minecraft.util.math.Box box) Calculates the optimal aim point for a box.static net.minecraft.util.math.Vec3dgetDirectionalSpeed(double speed) Calculates directional speed from player input.static net.minecraft.util.math.Vec3dgetRotationVector(float yaw, float pitch) Converts yaw and pitch angles to a normalized direction vector.static booleanisPointInBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Checks if a point is inside a box.static doublelerp(double delta, double start, double end) Linear interpolation between two double values.static net.minecraft.util.math.Vec3dlerp(double delta, net.minecraft.util.math.Vec3d start, net.minecraft.util.math.Vec3d end) Linear interpolation between two vectors.static floatlerp(float delta, float start, float end) Linear interpolation between two float values.static net.minecraft.util.math.Vec3dnormalize(net.minecraft.util.math.Vec3d vector) Normalizes a vector to unit length.static floatnormalizeAngle(float angle) Normalizes an angle to the range -180 to 180 degrees.static doubleradiansToDegrees(double radians) Converts radians to degrees.static net.minecraft.util.math.Vec3dyawToVector(float yaw, double speed) Converts yaw angle and speed to a movement vector.
-
Constructor Details
-
MathHelper
public MathHelper()
-
-
Method Details
-
calculateRotation
public static float[] calculateRotation(net.minecraft.util.math.Vec3d eyes, net.minecraft.util.math.Vec3d target) Calculates the yaw and pitch rotation needed to face a target position. Returns yaw and pitch angles in degrees required to point from eyes position to target position. Yaw is normalized to -180 to 180 range.- Parameters:
eyes- The eye position to calculate fromtarget- The target position to face- Returns:
- float array containing [yaw, pitch] in degrees
-
getRotationVector
public static net.minecraft.util.math.Vec3d getRotationVector(float yaw, float pitch) Converts yaw and pitch angles to a normalized direction vector. Creates a unit vector pointing in the direction specified by the yaw and pitch angles.- Parameters:
yaw- Yaw angle in degreespitch- Pitch angle in degrees- Returns:
- Normalized direction vector
-
normalizeAngle
public static float normalizeAngle(float angle) Normalizes an angle to the range -180 to 180 degrees. Ensures angle values stay within the standard Minecraft rotation range.- Parameters:
angle- Angle in degrees- Returns:
- Normalized angle in range -180 to 180
-
yawToVector
public static net.minecraft.util.math.Vec3d yawToVector(float yaw, double speed) Converts yaw angle and speed to a movement vector. Calculates the X and Z components of movement based on yaw direction and speed. This is commonly used for directional movement in speed/packetfly modules.- Parameters:
yaw- Yaw angle in degreesspeed- Movement speed- Returns:
- Vec3d with X and Z movement components (Y is always 0)
-
getDirectionalSpeed
public static net.minecraft.util.math.Vec3d getDirectionalSpeed(double speed) Calculates directional speed from player input. Takes the player's current input (WASD keys) and converts it to a movement vector based on the player's facing direction.- Parameters:
speed- Base movement speed- Returns:
- Movement vector based on player input and facing direction
-
lerp
public static double lerp(double delta, double start, double end) Linear interpolation between two double values.- Parameters:
delta- Interpolation factor (0.0 = start, 1.0 = end)start- Starting valueend- Ending value- Returns:
- Interpolated value
-
lerp
public static float lerp(float delta, float start, float end) Linear interpolation between two float values.- Parameters:
delta- Interpolation factor (0.0 = start, 1.0 = end)start- Starting valueend- Ending value- Returns:
- Interpolated value
-
lerp
public static net.minecraft.util.math.Vec3d lerp(double delta, net.minecraft.util.math.Vec3d start, net.minecraft.util.math.Vec3d end) Linear interpolation between two vectors.- Parameters:
delta- Interpolation factor (0.0 = start, 1.0 = end)start- Starting vectorend- Ending vector- Returns:
- Interpolated vector
-
bringCloser
public static double bringCloser(double value, double goal, double increment) Gradually brings a value closer to a goal by a specified increment. This is useful for smooth transitions and animations where you want to approach a target value incrementally.- Parameters:
value- Current valuegoal- Target valueincrement- Maximum change per call- Returns:
- New value closer to goal
-
clamp
public static double clamp(double value, double min, double max) Clamps a double value to a specified range.- Parameters:
value- Value to clampmin- Minimum allowed valuemax- Maximum allowed value- Returns:
- Clamped value within [min, max]
-
clamp
public static int clamp(int value, int min, int max) Clamps an integer value to a specified range.- Parameters:
value- Value to clampmin- Minimum allowed valuemax- Maximum allowed value- Returns:
- Clamped value within [min, max]
-
clampToBox
public static net.minecraft.util.math.Vec3d clampToBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Clamps a point to stay within a box's boundaries.- Parameters:
point- Point to clampbox- Bounding box- Returns:
- Point clamped to box boundaries
-
closestPointToBox
public static net.minecraft.util.math.Vec3d closestPointToBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Finds the closest point on a box's surface to a given point.- Parameters:
point- Reference pointbox- Target box- Returns:
- Closest point on the box surface
-
normalize
public static net.minecraft.util.math.Vec3d normalize(net.minecraft.util.math.Vec3d vector) Normalizes a vector to unit length.- Parameters:
vector- Vector to normalize- Returns:
- Normalized vector, or zero vector if input is zero-length
-
dotProduct
public static double dotProduct(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the dot product of two vectors.- Parameters:
a- First vectorb- Second vector- Returns:
- Dot product result
-
crossProduct
public static net.minecraft.util.math.Vec3d crossProduct(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the cross product of two vectors.- Parameters:
a- First vectorb- Second vector- Returns:
- Cross product result vector
-
distance
public static double distance(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b) Calculates the distance between two points.- Parameters:
a- First pointb- Second point- Returns:
- Euclidean distance
-
degreesToRadians
public static double degreesToRadians(double degrees) Converts degrees to radians.- Parameters:
degrees- Angle in degrees- Returns:
- Angle in radians
-
radiansToDegrees
public static double radiansToDegrees(double radians) Converts radians to degrees.- Parameters:
radians- Angle in radians- Returns:
- Angle in degrees
-
angleDifference
public static double angleDifference(double angle1, double angle2) Calculates the smallest angle difference between two angles. Returns a value between -180 and 180 degrees representing the shortest rotation needed to go from angle1 to angle2.- Parameters:
angle1- First angle in degreesangle2- Second angle in degrees- Returns:
- Smallest angle difference in degrees
-
findClosestPointOnBox
public static net.minecraft.util.math.Vec3d findClosestPointOnBox(net.minecraft.util.math.Box box, net.minecraft.util.math.Vec3d point) Finds the closest point on a box's surface to a given point.- Parameters:
box- Target boxpoint- Reference point- Returns:
- Closest point on the box surface
-
getBestAimPoint
public static net.minecraft.util.math.Vec3d getBestAimPoint(net.minecraft.util.math.Box box) Calculates the optimal aim point for a box. Finds the best point to aim at on a box, considering the player's current position and line of sight.- Parameters:
box- Target box- Returns:
- Optimal aim point on the box
-
isPointInBox
public static boolean isPointInBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box) Checks if a point is inside a box.- Parameters:
point- Point to testbox- Box to test against- Returns:
- true if point is inside the box
-
fastSin
public static double fastSin(double radians) Fast sine calculation optimized for x86 processors. Uses angle reduction to keep values within the safe range for better performance and precision on x86 architecture.- Parameters:
radians- Angle in radians- Returns:
- Sine of the angle
-
fastCos
public static double fastCos(double radians) Fast cosine calculation optimized for x86 processors. Uses angle reduction to keep values within the safe range for better performance and precision on x86 architecture.- Parameters:
radians- Angle in radians- Returns:
- Cosine of the angle
-