Class MathHelper

java.lang.Object
dev.boze.api.utility.MathHelper

public class MathHelper extends Object
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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static double
    angleDifference(double angle1, double angle2)
    Calculates the smallest angle difference between two angles.
    static double
    bringCloser(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 double
    clamp(double value, double min, double max)
    Clamps a double value to a specified range.
    static int
    clamp(int value, int min, int max)
    Clamps an integer value to a specified range.
    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.
    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.
    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.
    static double
    degreesToRadians(double degrees)
    Converts degrees to radians.
    static double
    distance(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b)
    Calculates the distance between two points.
    static double
    dotProduct(net.minecraft.util.math.Vec3d a, net.minecraft.util.math.Vec3d b)
    Calculates the dot product of two vectors.
    static double
    fastCos(double radians)
    Fast cosine calculation optimized for x86 processors.
    static double
    fastSin(double radians)
    Fast sine calculation optimized for x86 processors.
    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.
    static net.minecraft.util.math.Vec3d
    getBestAimPoint(net.minecraft.util.math.Box box)
    Calculates the optimal aim point for a box.
    static net.minecraft.util.math.Vec3d
    getDirectionalSpeed(double speed)
    Calculates directional speed from player input.
    static net.minecraft.util.math.Vec3d
    getRotationVector(float yaw, float pitch)
    Converts yaw and pitch angles to a normalized direction vector.
    static boolean
    isPointInBox(net.minecraft.util.math.Vec3d point, net.minecraft.util.math.Box box)
    Checks if a point is inside a box.
    static double
    lerp(double delta, double start, double end)
    Linear interpolation between two double values.
    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.
    static float
    lerp(float delta, float start, float end)
    Linear interpolation between two float values.
    static net.minecraft.util.math.Vec3d
    normalize(net.minecraft.util.math.Vec3d vector)
    Normalizes a vector to unit length.
    static float
    normalizeAngle(float angle)
    Normalizes an angle to the range -180 to 180 degrees.
    static double
    radiansToDegrees(double radians)
    Converts radians to degrees.
    static net.minecraft.util.math.Vec3d
    yawToVector(float yaw, double speed)
    Converts yaw angle and speed to a movement vector.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 from
      target - 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 degrees
      pitch - 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 degrees
      speed - 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 value
      end - 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 value
      end - 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 vector
      end - 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 value
      goal - Target value
      increment - 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 clamp
      min - Minimum allowed value
      max - 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 clamp
      min - Minimum allowed value
      max - 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 clamp
      box - 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 point
      box - 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 vector
      b - 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 vector
      b - 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 point
      b - 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 degrees
      angle2 - 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 box
      point - 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 test
      box - 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