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 int
     
    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 float[]
     
    private static final int
     
    private static final int
     
    private static final double
     
    static final float
     
    static final float
     
    private static final long
     
    private static final long
     
    private static final long
     
    private static final long
     
    static final Vector3f
     
    static final Vector3f
     
    static final Vector3f
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Mth()
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static float
    abs(float v)
     
    static int
    abs(int v)
    Returns the unsigned value of an int.
    static double
    absMax(double a, double b)
    Returns the maximum of the absolute value of two numbers.
    static float
    absMax(float a, float b)
     
    static int
    absMax(int a, int b)
     
    static float
    approach(float current, float target, float increment)
    Changes value by stepSize towards the limit and returns the result.
    static float
    approachDegrees(float current, float target, float increment)
    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 from, int to, IntPredicate condition)
     
    static float
    catmullrom(float alpha, float p0, float p1, float p2, float p3)
     
    static int
    ceil(double v)
     
    static int
    ceil(float v)
     
    static int
    ceillog2(int input)
    Uses a B(2, 5) De Bruijn sequence and a lookup table to efficiently calculate the log-base-two of the given value.
    static long
    ceilLong(double v)
     
    static int
    chessboardDistance(int x0, int z0, int x1, int z1)
     
    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 factor, double min, double max)
    Method for linear interpolation of doubles.
    static float
    clampedLerp(float factor, float min, float max)
    Method for linear interpolation of floats.
    static double
    clampedMap(double value, double fromMin, double fromMax, double toMin, double toMax)
     
    static float
    clampedMap(float value, float fromMin, float fromMax, float toMin, float toMax)
     
    static float
    cos(double i)
     
    static UUID
     
    static float
    cube(float x)
     
    static float
    degreesDifference(float fromAngle, float toAngle)
    Returns the difference between two angles in degrees.
    static float
    degreesDifferenceAbs(float angleA, float angleB)
    Returns the absolute of the difference between two angles in degrees.
    static boolean
    equal(double a, double b)
     
    static boolean
    equal(float a, float b)
     
    static float
    fastInvCubeRoot(float x)
     
    static double
    fastInvSqrt(double x)
    Deprecated.
    static int
    floor(double v)
    Returns the greatest integer less than or equal to the double argument.
    static int
    floor(float v)
    Returns the greatest integer less than or equal to the float argument.
    static int
    floorDiv(int a, int b)
     
    static double
    frac(double num)
    Gets the decimal portion of the given double.
    static float
    frac(float num)
     
    static int
    getInt(String input, int def)
    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 value, double min, double max)
     
    static float
    inverseLerp(float value, float min, float max)
     
    static double
    invSqrt(double x)
     
    static float
    invSqrt(float x)
     
    static boolean
    isMultipleOf(int dividend, int divisor)
     
    static boolean
    isPowerOfTwo(int input)
    Is the given value a power of two?
    static double
    length(double x, double y)
     
    static double
    length(double x, double y, double z)
     
    static float
    length(float x, float y)
     
    static double
    lengthSquared(double x, double y)
     
    static double
    lengthSquared(double x, double y, double z)
     
    static float
    lengthSquared(float x, float y, float z)
     
    static double
    lerp(double alpha1, double p0, double p1)
    Method for linear interpolation of doubles
    static Vec3
    lerp(double alpha, Vec3 p1, Vec3 p2)
     
    static float
    lerp(float alpha1, float p0, float p1)
    Method for linear interpolation of floats
    static double
    lerp2(double alpha1, double alpha2, double x00, double x10, double x01, double x11)
     
    static double
    lerp3(double alpha1, double alpha2, double alpha3, double x000, double x100, double x010, double x110, double x001, double x101, double x011, double x111)
     
    static int
    lerpDiscrete(float alpha1, int p0, int p1)
     
    static int
    lerpInt(float alpha1, int p0, int p1)
     
    static long
    lfloor(double v)
    Long version of floor()
    static int
    log2(int input)
    Efficiently calculates the floor of the base-2 log of an integer value.
    static double
    map(double value, double fromMin, double fromMax, double toMin, double toMax)
     
    static float
    map(float value, float fromMin, float fromMax, float toMin, float toMax)
     
    static int
    mulAndTruncate(org.apache.commons.lang3.math.Fraction fraction, int factor)
     
    static int
    murmurHash3Mixer(int hash)
     
    static double
    nextDouble(RandomSource random, double min, double max)
     
    static float
    nextFloat(RandomSource random, float min, float max)
     
    static int
    nextInt(RandomSource random, int minInclusive, int maxInclusive)
     
    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 origin, int lowerBound, int upperBound)
     
    static IntStream
    outFromOrigin(int origin, int lowerBound, int upperBound, int stepSize)
     
    static byte
    packDegrees(float angle)
     
    static int
    positiveCeilDiv(int input, int divisor)
    Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.
    static double
    positiveModulo(double input, double mod)
     
    static float
    positiveModulo(float input, float mod)
     
    static int
    positiveModulo(int input, int mod)
     
    static int
    quantize(double value, int quantizeResolution)
    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 min, float maxExclusive)
     
    static int
    randomBetweenInclusive(RandomSource random, int min, int maxInclusive)
     
    static boolean
    rayIntersectsAABB(Vec3 rayStart, Vec3 rayDir, AABB aabb)
     
    static float
    rotateIfNecessary(float baseAngle, float targetAngle, float maxAngleDiff)
    Takes a rotation and compares it to another rotation.
     
    static double
    rotLerp(double a, double from, double to)
     
    static float
    rotLerp(float a, float from, float to)
    Linearly interpolates an angle between the start between the start and end values given as degrees.
    static float
    rotLerpRad(float a, float from, float to)
     
    static int
    roundToward(int input, int multiple)
    Rounds the given value up to a multiple of factor.
    static int
    sign(double number)
     
    static float
    sin(double i)
     
    static int
    Returns the input value rounded up to the next highest power of two.
    static int
    smallestSquareSide(int itemCount)
     
    static double
    smoothstep(double x)
     
    static double
     
    static float
    sqrt(float x)
     
    static double
    square(double x)
     
    static float
    square(float x)
     
    static int
    square(int x)
     
    static long
    square(long x)
     
    static float
    triangleWave(float index, float period)
     
    static float
    unpackDegrees(byte rot)
     
    static double
    wobble(double coord)
     
    static double
    wrapDegrees(double angle)
    The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.
    static float
    wrapDegrees(float angle)
    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)
    static float
    wrapDegrees(long angle)
     

    Methods inherited from class 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
    • Y_AXIS

      public static final Vector3f Y_AXIS
    • X_AXIS

      public static final Vector3f X_AXIS
    • Z_AXIS

      public static final Vector3f Z_AXIS
    • SIN_QUANTIZATION

      private static final int SIN_QUANTIZATION
      See Also:
    • SIN_MASK

      private static final int SIN_MASK
      See Also:
    • COS_OFFSET

      private static final int COS_OFFSET
      See Also:
    • SIN_SCALE

      private static final double SIN_SCALE
      See Also:
    • SIN

      private static final float[] SIN
    • 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(double i)
    • cos

      public static float cos(double i)
    • sqrt

      public static float sqrt(float x)
    • floor

      public static int floor(float v)
      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 v)
      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 v)
      Long version of floor()
    • abs

      public static float abs(float v)
    • abs

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

      public static int ceil(float v)
    • ceil

      public static int ceil(double v)
    • ceilLong

      public static long ceilLong(double v)
    • 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 factor, double min, double max)
      Method for linear interpolation of doubles.
      Parameters:
      factor - Start value for the lerp.
      min - End value for the lerp.
      max - 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 factor, float min, float max)
      Method for linear interpolation of floats.
      Parameters:
      factor - Start value for the lerp.
      min - End value for the lerp.
      max - 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 int absMax(int a, int b)
    • absMax

      public static float absMax(float a, float b)
    • absMax

      public static double absMax(double a, double b)
      Returns the maximum of the absolute value of two numbers.
      Returns:
      the maximum of the absolute value of two numbers
    • chessboardDistance

      public static int chessboardDistance(int x0, int z0, int x1, int z1)
    • floorDiv

      public static int floorDiv(int a, int b)
    • nextInt

      public static int nextInt(RandomSource random, int minInclusive, int maxInclusive)
    • nextFloat

      public static float nextFloat(RandomSource random, float min, float max)
    • nextDouble

      public static double nextDouble(RandomSource random, double min, double max)
    • equal

      public static boolean equal(float a, float b)
    • equal

      public static boolean equal(double a, double b)
    • positiveModulo

      public static int positiveModulo(int input, int mod)
    • positiveModulo

      public static float positiveModulo(float input, float mod)
    • positiveModulo

      public static double positiveModulo(double input, double mod)
    • isMultipleOf

      public static boolean isMultipleOf(int dividend, int divisor)
    • packDegrees

      public static byte packDegrees(float angle)
    • unpackDegrees

      public static float unpackDegrees(byte rot)
    • 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(long angle)
    • wrapDegrees

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

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

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

      public static float degreesDifferenceAbs(float angleA, float angleB)
      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 baseAngle, float targetAngle, float maxAngleDiff)
      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 current, float target, float increment)
      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 current, float target, float increment)
      Changes the angle by stepSize towards the limit in the direction where the distance is smaller.
    • getInt

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

      public static int smallestEncompassingPowerOfTwo(int input)
      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
    • smallestSquareSide

      public static int smallestSquareSide(int itemCount)
    • isPowerOfTwo

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

      public static int ceillog2(int input)
      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 input)
      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.
    • frac

      public static float frac(float num)
    • frac

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

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

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

      public static UUID createInsecureUUID(RandomSource random)
    • inverseLerp

      public static double inverseLerp(double value, double min, double max)
    • inverseLerp

      public static float inverseLerp(float value, float min, float max)
    • rayIntersectsAABB

      public static boolean rayIntersectsAABB(Vec3 rayStart, Vec3 rayDir, AABB aabb)
    • atan2

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

      public static float invSqrt(float x)
    • invSqrt

      public static double invSqrt(double x)
    • fastInvSqrt

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

      public static float fastInvCubeRoot(float x)
    • 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 hash)
    • binarySearch

      public static int binarySearch(int from, int to, IntPredicate condition)
    • lerpInt

      public static int lerpInt(float alpha1, int p0, int p1)
    • lerpDiscrete

      public static int lerpDiscrete(float alpha1, int p0, int p1)
    • lerp

      public static float lerp(float alpha1, float p0, float p1)
      Method for linear interpolation of floats
      Parameters:
      alpha1 - 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)
      p0 - Start value for the lerp
      p1 - End value for the lerp
    • lerp

      public static Vec3 lerp(double alpha, Vec3 p1, Vec3 p2)
    • lerp

      public static double lerp(double alpha1, double p0, double p1)
      Method for linear interpolation of doubles
      Parameters:
      alpha1 - 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)
      p0 - Start value for the lerp
      p1 - End value for the lerp
    • lerp2

      public static double lerp2(double alpha1, double alpha2, double x00, double x10, double x01, double x11)
    • lerp3

      public static double lerp3(double alpha1, double alpha2, double alpha3, double x000, double x100, double x010, double x110, double x001, double x101, double x011, double x111)
    • catmullrom

      public static float catmullrom(float alpha, float p0, float p1, float p2, float p3)
    • smoothstep

      public static double smoothstep(double x)
    • smoothstepDerivative

      public static double smoothstepDerivative(double x)
    • sign

      public static int sign(double number)
    • rotLerp

      public static float rotLerp(float a, float from, float to)
      Linearly interpolates an angle between the start between the start and end values given as degrees.
      Parameters:
      a - 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 a, double from, double to)
    • rotLerpRad

      public static float rotLerpRad(float a, float from, float to)
    • triangleWave

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

      public static float square(float x)
    • cube

      public static float cube(float x)
    • square

      public static double square(double x)
    • square

      public static int square(int x)
    • square

      public static long square(long x)
    • clampedMap

      public static double clampedMap(double value, double fromMin, double fromMax, double toMin, double toMax)
    • clampedMap

      public static float clampedMap(float value, float fromMin, float fromMax, float toMin, float toMax)
    • map

      public static double map(double value, double fromMin, double fromMax, double toMin, double toMax)
    • map

      public static float map(float value, float fromMin, float fromMax, float toMin, float toMax)
    • wobble

      public static double wobble(double coord)
    • roundToward

      public static int roundToward(int input, int multiple)
      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 input, int divisor)
      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 min, int maxInclusive)
    • randomBetween

      public static float randomBetween(RandomSource random, float min, 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 x, double y)
    • length

      public static double length(double x, double y)
    • length

      public static float length(float x, float y)
    • lengthSquared

      public static double lengthSquared(double x, double y, double z)
    • length

      public static double length(double x, double y, double z)
    • lengthSquared

      public static float lengthSquared(float x, float y, float z)
    • quantize

      public static int quantize(double value, int quantizeResolution)
      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 origin, int lowerBound, int upperBound)
    • outFromOrigin

      public static IntStream outFromOrigin(int origin, int lowerBound, int upperBound, int stepSize)
    • rotationAroundAxis

      public static Quaternionf rotationAroundAxis(Vector3f axis, Quaternionf rotation, Quaternionf result)
    • mulAndTruncate

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