Class Mth

java.lang.Object
net.minecraft.util.Mth

public class Mth extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final double[]
     
    private static final double[]
     
    static final float
     
    static final float
     
    private static final double
     
    private static final int
     
    static final float
     
    private static final int
     
    private static final int[]
    Though it looks like an array, this is really more like a mapping.
    private static final double
     
    static final float
     
    static final float
     
    private static final RandomSource
     
    private static final float[]
     
    private static final float
     
    static final float
     
    static final float
     
    private static final long
     
    private static final long
     
    private static final long
     
    private static final long
     
    static final org.joml.Vector3f
     
    static final org.joml.Vector3f
     
    static final org.joml.Vector3f
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mth()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    abs(float value)
     
    static int
    abs(int value)
    Returns the unsigned value of an int.
    static double
    absMax(double x, double y)
    Returns the maximum of the absolute value of two numbers.
    static float
    approach(float value, float limit, float stepSize)
    Changes value by stepSize towards the limit and returns the result.
    static float
    approachDegrees(float angle, float limit, float stepSize)
    Changes the angle by stepSize towards the limit in the direction where the distance is smaller.
    static double
    atan2(double y, double x)
     
    static int
    binarySearch(int min, int max, IntPredicate isTargetBeforeOrAt)
     
    static float
    catmullrom(float delta, float controlPoint1, float controlPoint2, float controlPoint3, float controlPoint4)
     
    static int
    ceil(double value)
     
    static int
    ceil(float value)
     
    static int
    ceillog2(int value)
    Uses a B(2, 5) De Bruijn sequence and a lookup table to efficiently calculate the log-base-two of the given value.
    static double
    clamp(double value, double min, double max)
    Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound.
    static float
    clamp(float value, float min, float max)
    Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound.
    static int
    clamp(int value, int min, int max)
    Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound.
    static long
    clamp(long value, long min, long max)
     
    static double
    clampedLerp(double start, double end, double delta)
    Method for linear interpolation of doubles.
    static float
    clampedLerp(float start, float end, float delta)
    Method for linear interpolation of floats.
    static double
    clampedMap(double input, double inputMin, double inputMax, double ouputMin, double outputMax)
     
    static float
    clampedMap(float input, float inputMin, float inputMax, float outputMin, float outputMax)
     
    static int
    color(float r, float g, float b)
    Makes an integer color from the given red, green, and blue float values
    static float
    cos(float value)
    cos looked up in the sin table with the appropriate offset
    static UUID
     
    static UUID
     
    static float
    degreesDifference(float start, float end)
    Returns the difference between two angles in degrees.
    static float
    degreesDifferenceAbs(float start, float end)
    Returns the absolute of the difference between two angles in degrees.
    static boolean
    equal(double x, double y)
     
    static boolean
    equal(float x, float y)
     
    static float
    fastInvCubeRoot(float number)
     
    static double
    fastInvSqrt(double number)
    Deprecated.
    static int
    floor(double value)
    Returns the greatest integer less than or equal to the double argument.
    static int
    floor(float value)
    Returns the greatest integer less than or equal to the float argument.
    static int
    floorDiv(int dividend, int divisor)
     
    static double
    frac(double number)
    Gets the decimal portion of the given double.
    static float
    frac(float number)
     
    static int
    getInt(String value, int defaultValue)
    Parses the string as an integer or returns the second parameter if it fails.
    static long
    getSeed(int x, int y, int z)
    Deprecated.
    static long
    Deprecated.
    static int
    hsvToArgb(float hue, float saturation, float value, int alpha)
     
    static int
    hsvToRgb(float hue, float saturation, float value)
     
    static double
    inverseLerp(double delta, double start, double end)
     
    static float
    inverseLerp(float delta, float start, float end)
     
    static double
    invSqrt(double number)
     
    static float
    invSqrt(float number)
     
    static boolean
    isMultipleOf(int number, int multiple)
     
    static boolean
    isPowerOfTwo(int value)
    Is the given value a power of two? (1, 2, 4, 8, 16, ...)
    static double
    length(double xDistance, double yDistance)
     
    static double
    length(double xDistance, double yDistance, double zDistance)
     
    static double
    lengthSquared(double xDistance, double yDistance)
     
    static double
    lengthSquared(double xDistance, double yDistance, double zDistance)
     
    static float
    lengthSquared(float xDistance, float yDistance, float zDistance)
     
    static double
    lerp(double delta, double start, double end)
    Method for linear interpolation of doubles
    static float
    lerp(float delta, float start, float end)
    Method for linear interpolation of floats
    static double
    lerp2(double delta1, double delta2, double start1, double end1, double start2, double end2)
     
    static double
    lerp3(double delta1, double delta2, double delta3, double start1, double end1, double start2, double end2, double start3, double end3, double start4, double end4)
     
    static int
    lerpDiscrete(float delta, int start, int end)
     
    static int
    lerpInt(float delta, int start, int end)
     
    static long
    lfloor(double value)
    Long version of floor()
    static int
    log2(int value)
    Efficiently calculates the floor of the base-2 log of an integer value.
    static double
    map(double input, double inputMin, double inputMax, double outputMin, double outputMax)
     
    static float
    map(float input, float inputMin, float inputMax, float outputMin, float outputMax)
     
    static int
    mulAndTruncate(org.apache.commons.lang3.math.Fraction fraction, int factor)
     
    static int
    murmurHash3Mixer(int input)
     
    static double
    nextDouble(RandomSource random, double minimum, double maximum)
     
    static float
    nextFloat(RandomSource random, float minimum, float maximum)
     
    static int
    nextInt(RandomSource random, int minimum, int maximum)
     
    static float
    normal(RandomSource random, float mean, float deviation)
    Generates a value from a normal distribution with the given mean and deviation.
    static IntStream
    outFromOrigin(int input, int lowerBound, int upperBound)
     
    static IntStream
    outFromOrigin(int input, int lowerBound, int upperBound, int steps)
     
    static int
    positiveCeilDiv(int x, int y)
    Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
    static double
    positiveModulo(double numerator, double denominator)
     
    static float
    positiveModulo(float numerator, float denominator)
     
    static int
    positiveModulo(int x, int y)
     
    static int
    quantize(double value, int factor)
    Gets the value closest to zero that is not closer to zero than the given value and is a multiple of the factor.
    static float
    randomBetween(RandomSource random, float minInclusive, float maxExclusive)
     
    static int
    randomBetweenInclusive(RandomSource random, int minInclusive, int maxInclusive)
     
    static boolean
    rayIntersectsAABB(Vec3 start, Vec3 end, AABB boundingBox)
     
    static float
    rotateIfNecessary(float rotationToAdjust, float actualRotation, float maxDifference)
    Takes a rotation and compares it to another rotation.
    static org.joml.Quaternionf
    rotationAroundAxis(org.joml.Vector3f axis, org.joml.Quaternionf cameraOrentation, org.joml.Quaternionf output)
     
    static double
    rotLerp(double delta, double start, double end)
     
    static float
    rotLerp(float delta, float start, float end)
    Linearly interpolates an angle between the start between the start and end values given as degrees.
    static int
    roundToward(int value, int factor)
    Rounds the given value up to a multiple of factor.
    static int
    sign(double x)
     
    static float
    sin(float value)
    sin looked up in a table
    static int
    Returns the input value rounded up to the next highest power of two.
    static double
    smoothstep(double input)
     
    static double
    smoothstepDerivative(double input)
     
    static float
    sqrt(float value)
     
    static double
    square(double value)
     
    static float
    square(float value)
     
    static int
    square(int value)
     
    static long
    square(long value)
     
    static float
    triangleWave(float input, float period)
     
    static double
    wobble(double input)
     
    static double
    wrapDegrees(double value)
    The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.
    static float
    wrapDegrees(float value)
    The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.
    static int
    wrapDegrees(int angle)
    Adjust the angle so that its value is in the range [-180;180)

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UUID_VERSION

      private static final long UUID_VERSION
      See Also:
    • UUID_VERSION_TYPE_4

      private static final long UUID_VERSION_TYPE_4
      See Also:
    • UUID_VARIANT

      private static final long UUID_VARIANT
      See Also:
    • UUID_VARIANT_2

      private static final long UUID_VARIANT_2
      See Also:
    • PI

      public static final float PI
      See Also:
    • HALF_PI

      public static final float HALF_PI
      See Also:
    • TWO_PI

      public static final float TWO_PI
      See Also:
    • DEG_TO_RAD

      public static final float DEG_TO_RAD
      See Also:
    • RAD_TO_DEG

      public static final float RAD_TO_DEG
      See Also:
    • EPSILON

      public static final float EPSILON
      See Also:
    • SQRT_OF_TWO

      public static final float SQRT_OF_TWO
    • SIN_SCALE

      private static final float SIN_SCALE
      See Also:
    • Y_AXIS

      public static final org.joml.Vector3f Y_AXIS
    • X_AXIS

      public static final org.joml.Vector3f X_AXIS
    • Z_AXIS

      public static final org.joml.Vector3f Z_AXIS
    • SIN

      private static final float[] SIN
    • RANDOM

      private static final RandomSource RANDOM
    • MULTIPLY_DE_BRUIJN_BIT_POSITION

      private static final int[] MULTIPLY_DE_BRUIJN_BIT_POSITION
      Though it looks like an array, this is really more like a mapping. Key (index of this array) is the upper 5 bits of the result of multiplying a 32-bit unsigned integer by the B(2, 5) De Bruijn sequence 0x077CB531. Value (value stored in the array) is the unique index (from the right) of the leftmo
    • ONE_SIXTH

      private static final double ONE_SIXTH
      See Also:
    • FRAC_EXP

      private static final int FRAC_EXP
      See Also:
    • LUT_SIZE

      private static final int LUT_SIZE
      See Also:
    • FRAC_BIAS

      private static final double FRAC_BIAS
    • ASIN_TAB

      private static final double[] ASIN_TAB
    • COS_TAB

      private static final double[] COS_TAB
  • Constructor Details

    • Mth

      public Mth()
  • Method Details

    • sin

      public static float sin(float value)
      sin looked up in a table
    • cos

      public static float cos(float value)
      cos looked up in the sin table with the appropriate offset
    • sqrt

      public static float sqrt(float value)
    • floor

      public static int floor(float value)
      Returns the greatest integer less than or equal to the float argument.
      Returns:
      the greatest integer less than or equal to the float argument
    • floor

      public static int floor(double value)
      Returns the greatest integer less than or equal to the double argument.
      Returns:
      the greatest integer less than or equal to the double argument
    • lfloor

      public static long lfloor(double value)
      Long version of floor()
    • abs

      public static float abs(float value)
    • abs

      public static int abs(int value)
      Returns the unsigned value of an int.
      Returns:
      the unsigned value of an int
    • ceil

      public static int ceil(float value)
    • ceil

      public static int ceil(double value)
    • clamp

      public static int clamp(int value, int min, int max)
      Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound. If the value is greater than the upper bound, returns the upper bound.
      Parameters:
      value - The value that is clamped.
      min - The lower bound for the clamp.
      max - The upper bound for the clamp.
      Returns:
      the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound
    • clamp

      public static long clamp(long value, long min, long max)
    • clamp

      public static float clamp(float value, float min, float max)
      Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound. If the value is greater than the upper bound, returns the upper bound.
      Parameters:
      value - The value that is clamped.
      min - The lower bound for the clamp.
      max - The upper bound for the clamp.
      Returns:
      the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound
    • clamp

      public static double clamp(double value, double min, double max)
      Returns the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound. If the value is greater than the upper bound, returns the upper bound.
      Parameters:
      value - The value that is clamped.
      min - The lower bound for the clamp.
      max - The upper bound for the clamp.
      Returns:
      the given value if between the lower and the upper bound. If the value is less than the lower bound, returns the lower bound
    • clampedLerp

      public static double clampedLerp(double start, double end, double delta)
      Method for linear interpolation of doubles.
      Parameters:
      start - Start value for the lerp.
      end - End value for the lerp.
      delta - A value between 0 and 1 that indicates the percentage of the lerp. (0 will give the start value and 1 will give the end value) If the value is not between 0 and 1, it is clamped.
    • clampedLerp

      public static float clampedLerp(float start, float end, float delta)
      Method for linear interpolation of floats.
      Parameters:
      start - Start value for the lerp.
      end - End value for the lerp.
      delta - A value between 0 and 1 that indicates the percentage of the lerp. (0 will give the start value and 1 will give the end value) If the value is not between 0 and 1, it is clamped.
    • absMax

      public static double absMax(double x, double y)
      Returns the maximum of the absolute value of two numbers.
      Returns:
      the maximum of the absolute value of two numbers
    • floorDiv

      public static int floorDiv(int dividend, int divisor)
    • nextInt

      public static int nextInt(RandomSource random, int minimum, int maximum)
    • nextFloat

      public static float nextFloat(RandomSource random, float minimum, float maximum)
    • nextDouble

      public static double nextDouble(RandomSource random, double minimum, double maximum)
    • equal

      public static boolean equal(float x, float y)
    • equal

      public static boolean equal(double x, double y)
    • positiveModulo

      public static int positiveModulo(int x, int y)
    • positiveModulo

      public static float positiveModulo(float numerator, float denominator)
    • positiveModulo

      public static double positiveModulo(double numerator, double denominator)
    • isMultipleOf

      public static boolean isMultipleOf(int number, int multiple)
    • wrapDegrees

      public static int wrapDegrees(int angle)
      Adjust the angle so that its value is in the range [-180;180)
    • wrapDegrees

      public static float wrapDegrees(float value)
      The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.
    • wrapDegrees

      public static double wrapDegrees(double value)
      The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.
    • degreesDifference

      public static float degreesDifference(float start, float end)
      Returns the difference between two angles in degrees.
      Returns:
      the difference between two angles in degrees
    • degreesDifferenceAbs

      public static float degreesDifferenceAbs(float start, float end)
      Returns the absolute of the difference between two angles in degrees.
      Returns:
      the absolute of the difference between two angles in degrees
    • rotateIfNecessary

      public static float rotateIfNecessary(float rotationToAdjust, float actualRotation, float maxDifference)
      Takes a rotation and compares it to another rotation. If the difference is greater than a given maximum, clamps the original rotation between to have at most the given difference to the actual rotation. This is used to match the body rotation of entities to their head rotation.
      Returns:
      The new value for the rotation that was adjusted
    • approach

      public static float approach(float value, float limit, float stepSize)
      Changes value by stepSize towards the limit and returns the result. If value is smaller than limit, the result will never be bigger than limit. If value is bigger than limit, the result will never be smaller than limit.
    • approachDegrees

      public static float approachDegrees(float angle, float limit, float stepSize)
      Changes the angle by stepSize towards the limit in the direction where the distance is smaller.
    • getInt

      public static int getInt(String value, int defaultValue)
      Parses the string as an integer or returns the second parameter if it fails.
    • smallestEncompassingPowerOfTwo

      public static int smallestEncompassingPowerOfTwo(int value)
      Returns the input value rounded up to the next highest power of two.
      Returns:
      the input value rounded up to the next highest power of two
    • isPowerOfTwo

      public static boolean isPowerOfTwo(int value)
      Is the given value a power of two? (1, 2, 4, 8, 16, ...)
    • ceillog2

      public static int ceillog2(int value)
      Uses a B(2, 5) De Bruijn sequence and a lookup table to efficiently calculate the log-base-two of the given value. Optimized for cases where the input value is a power-of-two. If the input value is not a power-of-two, then subtract 1 from the return value.
    • log2

      public static int log2(int value)
      Efficiently calculates the floor of the base-2 log of an integer value. This is effectively the index of the highest bit that is set. For example, if the number in binary is 0...100101, this will return 5.
    • color

      public static int color(float r, float g, float b)
      Makes an integer color from the given red, green, and blue float values
    • frac

      public static float frac(float number)
    • frac

      public static double frac(double number)
      Gets the decimal portion of the given double. For instance, frac(5.5) returns .5.
    • getSeed

      @Deprecated public static long getSeed(Vec3i pos)
      Deprecated.
    • getSeed

      @Deprecated public static long getSeed(int x, int y, int z)
      Deprecated.
    • createInsecureUUID

      public static UUID createInsecureUUID(RandomSource random)
    • createInsecureUUID

      public static UUID createInsecureUUID()
    • inverseLerp

      public static double inverseLerp(double delta, double start, double end)
    • inverseLerp

      public static float inverseLerp(float delta, float start, float end)
    • rayIntersectsAABB

      public static boolean rayIntersectsAABB(Vec3 start, Vec3 end, AABB boundingBox)
    • atan2

      public static double atan2(double y, double x)
    • invSqrt

      public static float invSqrt(float number)
    • invSqrt

      public static double invSqrt(double number)
    • fastInvSqrt

      @Deprecated public static double fastInvSqrt(double number)
      Deprecated.
      Computes 1/sqrt(n) using the fast inverse square root with a constant of 0x5FE6EB50C7B537AA.
    • fastInvCubeRoot

      public static float fastInvCubeRoot(float number)
    • hsvToRgb

      public static int hsvToRgb(float hue, float saturation, float value)
    • hsvToArgb

      public static int hsvToArgb(float hue, float saturation, float value, int alpha)
    • murmurHash3Mixer

      public static int murmurHash3Mixer(int input)
    • binarySearch

      public static int binarySearch(int min, int max, IntPredicate isTargetBeforeOrAt)
    • lerpInt

      public static int lerpInt(float delta, int start, int end)
    • lerpDiscrete

      public static int lerpDiscrete(float delta, int start, int end)
    • lerp

      public static float lerp(float delta, float start, float end)
      Method for linear interpolation of floats
      Parameters:
      delta - A value usually between 0 and 1 that indicates the percentage of the lerp. (0 will give the start value and 1 will give the end value)
      start - Start value for the lerp
      end - End value for the lerp
    • lerp

      public static double lerp(double delta, double start, double end)
      Method for linear interpolation of doubles
      Parameters:
      delta - A value usually between 0 and 1 that indicates the percentage of the lerp. (0 will give the start value and 1 will give the end value)
      start - Start value for the lerp
      end - End value for the lerp
    • lerp2

      public static double lerp2(double delta1, double delta2, double start1, double end1, double start2, double end2)
    • lerp3

      public static double lerp3(double delta1, double delta2, double delta3, double start1, double end1, double start2, double end2, double start3, double end3, double start4, double end4)
    • catmullrom

      public static float catmullrom(float delta, float controlPoint1, float controlPoint2, float controlPoint3, float controlPoint4)
    • smoothstep

      public static double smoothstep(double input)
    • smoothstepDerivative

      public static double smoothstepDerivative(double input)
    • sign

      public static int sign(double x)
    • rotLerp

      public static float rotLerp(float delta, float start, float end)
      Linearly interpolates an angle between the start between the start and end values given as degrees.
      Parameters:
      delta - A value between 0 and 1 that indicates the percentage of the lerp. (0 will give the start value and 1 will give the end value)
    • rotLerp

      public static double rotLerp(double delta, double start, double end)
    • triangleWave

      public static float triangleWave(float input, float period)
    • square

      public static float square(float value)
    • square

      public static double square(double value)
    • square

      public static int square(int value)
    • square

      public static long square(long value)
    • clampedMap

      public static double clampedMap(double input, double inputMin, double inputMax, double ouputMin, double outputMax)
    • clampedMap

      public static float clampedMap(float input, float inputMin, float inputMax, float outputMin, float outputMax)
    • map

      public static double map(double input, double inputMin, double inputMax, double outputMin, double outputMax)
    • map

      public static float map(float input, float inputMin, float inputMax, float outputMin, float outputMax)
    • wobble

      public static double wobble(double input)
    • roundToward

      public static int roundToward(int value, int factor)
      Rounds the given value up to a multiple of factor.
      Returns:
      The smallest integer multiple of factor that is greater than or equal to the value
    • positiveCeilDiv

      public static int positiveCeilDiv(int x, int y)
      Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
      See Also:
    • randomBetweenInclusive

      public static int randomBetweenInclusive(RandomSource random, int minInclusive, int maxInclusive)
    • randomBetween

      public static float randomBetween(RandomSource random, float minInclusive, float maxExclusive)
    • normal

      public static float normal(RandomSource random, float mean, float deviation)
      Generates a value from a normal distribution with the given mean and deviation.
    • lengthSquared

      public static double lengthSquared(double xDistance, double yDistance)
    • length

      public static double length(double xDistance, double yDistance)
    • lengthSquared

      public static double lengthSquared(double xDistance, double yDistance, double zDistance)
    • length

      public static double length(double xDistance, double yDistance, double zDistance)
    • lengthSquared

      public static float lengthSquared(float xDistance, float yDistance, float zDistance)
    • quantize

      public static int quantize(double value, int factor)
      Gets the value closest to zero that is not closer to zero than the given value and is a multiple of the factor.
    • outFromOrigin

      public static IntStream outFromOrigin(int input, int lowerBound, int upperBound)
    • outFromOrigin

      public static IntStream outFromOrigin(int input, int lowerBound, int upperBound, int steps)
    • rotationAroundAxis

      public static org.joml.Quaternionf rotationAroundAxis(org.joml.Vector3f axis, org.joml.Quaternionf cameraOrentation, org.joml.Quaternionf output)
    • mulAndTruncate

      public static int mulAndTruncate(org.apache.commons.lang3.math.Fraction fraction, int factor)