Package net.minecraft.util
Class Mth
java.lang.Object
net.minecraft.util.Mth
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic float
abs
(float pValue) static int
abs
(int pValue) Returns the unsigned value of an int.static double
absMax
(double pX, double pY) Returns the maximum of the absolute value of two numbers.static float
approach
(float pValue, float pLimit, float pStepSize) Changes value by stepSize towards the limit and returns the result.static float
approachDegrees
(float pAngle, float pLimit, float pStepSize) Changes the angle by stepSize towards the limit in the direction where the distance is smaller.static double
atan2
(double pY, double pX) static int
binarySearch
(int pMin, int pMax, IntPredicate pIsTargetBeforeOrAt) static float
catmullrom
(float pDelta, float pControlPoint1, float pControlPoint2, float pControlPoint3, float pControlPoint4) static int
ceil
(double pValue) static int
ceil
(float pValue) static int
ceillog2
(int pValue) 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 pValue, double pMin, double pMax) 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 pValue, float pMin, float pMax) 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 pValue, int pMin, int pMax) 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 pValue, long pMin, long pMax) static double
clampedLerp
(double pStart, double pEnd, double pDelta) Method for linear interpolation of doubles.static float
clampedLerp
(float pStart, float pEnd, float pDelta) Method for linear interpolation of floats.static double
clampedMap
(double pInput, double pInputMin, double pInputMax, double pOuputMin, double pOutputMax) static float
clampedMap
(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) static int
color
(float pR, float pG, float pB) Makes an integer color from the given red, green, and blue float valuesstatic float
cos
(float pValue) cos looked up in the sin table with the appropriate offsetstatic UUID
static UUID
createInsecureUUID
(RandomSource pRandom) static float
degreesDifference
(float pStart, float pEnd) Returns the difference between two angles in degrees.static float
degreesDifferenceAbs
(float pStart, float pEnd) Returns the absolute of the difference between two angles in degrees.static boolean
equal
(double pX, double pY) static boolean
equal
(float pX, float pY) static float
fastInvCubeRoot
(float pNumber) static double
fastInvSqrt
(double pNumber) Deprecated.static int
floor
(double pValue) Returns the greatest integer less than or equal to the double argument.static int
floor
(float pValue) Returns the greatest integer less than or equal to the float argument.static int
floorDiv
(int pDividend, int pDivisor) static double
frac
(double pNumber) Gets the decimal portion of the given double.static float
frac
(float pNumber) static int
Parses the string as an integer or returns the second parameter if it fails.static long
getSeed
(int pX, int pY, int pZ) Deprecated.static long
Deprecated.static int
hsvToArgb
(float pHue, float pSaturation, float pValue, int pAlpha) static int
hsvToRgb
(float pHue, float pSaturation, float pValue) static double
inverseLerp
(double pDelta, double pStart, double pEnd) static float
inverseLerp
(float pDelta, float pStart, float pEnd) static double
invSqrt
(double pNumber) static float
invSqrt
(float pNumber) static boolean
isMultipleOf
(int pNumber, int pMultiple) static boolean
isPowerOfTwo
(int pValue) Is the given value a power of two?static double
length
(double pXDistance, double pYDistance) static double
length
(double pXDistance, double pYDistance, double pZDistance) static double
lengthSquared
(double pXDistance, double pYDistance) static double
lengthSquared
(double pXDistance, double pYDistance, double pZDistance) static float
lengthSquared
(float pXDistance, float pYDistance, float pZDistance) static double
lerp
(double pDelta, double pStart, double pEnd) Method for linear interpolation of doublesstatic float
lerp
(float pDelta, float pStart, float pEnd) Method for linear interpolation of floatsstatic double
lerp2
(double pDelta1, double pDelta2, double pStart1, double pEnd1, double pStart2, double pEnd2) static double
lerp3
(double pDelta1, double pDelta2, double pDelta3, double pStart1, double pEnd1, double pStart2, double pEnd2, double pStart3, double pEnd3, double pStart4, double pEnd4) static int
lerpDiscrete
(float pDelta, int pStart, int pEnd) static int
lerpInt
(float pDelta, int pStart, int pEnd) static long
lfloor
(double pValue) Long version of floor()static int
log2
(int pValue) Efficiently calculates the floor of the base-2 log of an integer value.static double
map
(double pInput, double pInputMin, double pInputMax, double pOutputMin, double pOutputMax) static float
map
(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) static int
mulAndTruncate
(org.apache.commons.lang3.math.Fraction p_339645_, int p_339636_) static int
murmurHash3Mixer
(int pInput) static double
nextDouble
(RandomSource pRandom, double pMinimum, double pMaximum) static float
nextFloat
(RandomSource pRandom, float pMinimum, float pMaximum) static int
nextInt
(RandomSource pRandom, int pMinimum, int pMaximum) static float
normal
(RandomSource pRandom, float pMean, float pDeviation) Generates a value from a normal distribution with the given mean and deviation.static IntStream
outFromOrigin
(int pInput, int pLowerBound, int pUpperBound) static IntStream
outFromOrigin
(int pInput, int pLowerBound, int pUpperBound, int pSteps) static int
positiveCeilDiv
(int pX, int pY) Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.static double
positiveModulo
(double pNumerator, double pDenominator) static float
positiveModulo
(float pNumerator, float pDenominator) static int
positiveModulo
(int pX, int pY) static int
quantize
(double pValue, int pFactor) 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 pRandom, float pMinInclusive, float pMaxExclusive) static int
randomBetweenInclusive
(RandomSource pRandom, int pMinInclusive, int pMaxInclusive) static boolean
rayIntersectsAABB
(Vec3 pStart, Vec3 pEnd, AABB pBoundingBox) static float
rotateIfNecessary
(float pRotationToAdjust, float pActualRotation, float pMaxDifference) Takes a rotation and compares it to another rotation.static org.joml.Quaternionf
rotationAroundAxis
(org.joml.Vector3f p_304469_, org.joml.Quaternionf p_304737_, org.joml.Quaternionf p_304773_) static double
rotLerp
(double pDelta, double pStart, double pEnd) static float
rotLerp
(float pDelta, float pStart, float pEnd) Linearly interpolates an angle between the start between the start and end values given as degrees.static int
roundToward
(int pValue, int pFactor) Rounds the given value up to a multiple of factor.static int
sign
(double pX) static float
sin
(float pValue) sin looked up in a tablestatic int
smallestEncompassingPowerOfTwo
(int pValue) Returns the input value rounded up to the next highest power of two.static double
smoothstep
(double pInput) static double
smoothstepDerivative
(double pInput) static float
sqrt
(float pValue) static double
square
(double pValue) static float
square
(float pValue) static int
square
(int pValue) static long
square
(long pValue) static float
triangleWave
(float pInput, float pPeriod) static double
wobble
(double pInput) static double
wrapDegrees
(double pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static float
wrapDegrees
(float pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static int
wrapDegrees
(int pAngle) Adjust the angle so that its value is in the range [-180;180)
-
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
-
MULTIPLY_DE_BRUIJN_BIT_POSITION
private static final int[] MULTIPLY_DE_BRUIJN_BIT_POSITIONThough 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 pValue) sin looked up in a table -
cos
public static float cos(float pValue) cos looked up in the sin table with the appropriate offset -
sqrt
public static float sqrt(float pValue) -
floor
public static int floor(float pValue) 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 pValue) 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 pValue) Long version of floor() -
abs
public static float abs(float pValue) -
abs
public static int abs(int pValue) Returns the unsigned value of an int.- Returns:
- the unsigned value of an int
-
ceil
public static int ceil(float pValue) -
ceil
public static int ceil(double pValue) -
clamp
public static int clamp(int pValue, int pMin, int pMax) 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:
pValue
- The value that is clamped.pMin
- The lower bound for the clamp.pMax
- 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 pValue, long pMin, long pMax) -
clamp
public static float clamp(float pValue, float pMin, float pMax) 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:
pValue
- The value that is clamped.pMin
- The lower bound for the clamp.pMax
- 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 pValue, double pMin, double pMax) 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:
pValue
- The value that is clamped.pMin
- The lower bound for the clamp.pMax
- 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 pStart, double pEnd, double pDelta) Method for linear interpolation of doubles.- Parameters:
pStart
- Start value for the lerp.pEnd
- End value for the lerp.pDelta
- 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 pStart, float pEnd, float pDelta) Method for linear interpolation of floats.- Parameters:
pStart
- Start value for the lerp.pEnd
- End value for the lerp.pDelta
- 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 pX, double pY) 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 pDividend, int pDivisor) -
nextInt
-
nextFloat
-
nextDouble
-
equal
public static boolean equal(float pX, float pY) -
equal
public static boolean equal(double pX, double pY) -
positiveModulo
public static int positiveModulo(int pX, int pY) -
positiveModulo
public static float positiveModulo(float pNumerator, float pDenominator) -
positiveModulo
public static double positiveModulo(double pNumerator, double pDenominator) -
isMultipleOf
public static boolean isMultipleOf(int pNumber, int pMultiple) -
wrapDegrees
public static int wrapDegrees(int pAngle) Adjust the angle so that its value is in the range [-180;180) -
wrapDegrees
public static float wrapDegrees(float pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check. -
wrapDegrees
public static double wrapDegrees(double pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check. -
degreesDifference
public static float degreesDifference(float pStart, float pEnd) Returns the difference between two angles in degrees.- Returns:
- the difference between two angles in degrees
-
degreesDifferenceAbs
public static float degreesDifferenceAbs(float pStart, float pEnd) 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 pRotationToAdjust, float pActualRotation, float pMaxDifference) 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 pValue, float pLimit, float pStepSize) 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 pAngle, float pLimit, float pStepSize) Changes the angle by stepSize towards the limit in the direction where the distance is smaller. -
getInt
Parses the string as an integer or returns the second parameter if it fails. -
smallestEncompassingPowerOfTwo
public static int smallestEncompassingPowerOfTwo(int pValue) 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 pValue) Is the given value a power of two? (1, 2, 4, 8, 16, ...) -
ceillog2
public static int ceillog2(int pValue) 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 pValue) 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 pR, float pG, float pB) Makes an integer color from the given red, green, and blue float values -
frac
public static float frac(float pNumber) -
frac
public static double frac(double pNumber) Gets the decimal portion of the given double. For instance,frac(5.5)
returns.5
. -
getSeed
Deprecated. -
getSeed
Deprecated. -
createInsecureUUID
-
createInsecureUUID
-
inverseLerp
public static double inverseLerp(double pDelta, double pStart, double pEnd) -
inverseLerp
public static float inverseLerp(float pDelta, float pStart, float pEnd) -
rayIntersectsAABB
-
atan2
public static double atan2(double pY, double pX) -
invSqrt
public static float invSqrt(float pNumber) -
invSqrt
public static double invSqrt(double pNumber) -
fastInvSqrt
Deprecated.Computes 1/sqrt(n) using the fast inverse square root with a constant of 0x5FE6EB50C7B537AA. -
fastInvCubeRoot
public static float fastInvCubeRoot(float pNumber) -
hsvToRgb
public static int hsvToRgb(float pHue, float pSaturation, float pValue) -
hsvToArgb
public static int hsvToArgb(float pHue, float pSaturation, float pValue, int pAlpha) -
murmurHash3Mixer
public static int murmurHash3Mixer(int pInput) -
binarySearch
-
lerpInt
public static int lerpInt(float pDelta, int pStart, int pEnd) -
lerpDiscrete
public static int lerpDiscrete(float pDelta, int pStart, int pEnd) -
lerp
public static float lerp(float pDelta, float pStart, float pEnd) Method for linear interpolation of floats- Parameters:
pDelta
- 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)pStart
- Start value for the lerppEnd
- End value for the lerp
-
lerp
public static double lerp(double pDelta, double pStart, double pEnd) Method for linear interpolation of doubles- Parameters:
pDelta
- 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)pStart
- Start value for the lerppEnd
- End value for the lerp
-
lerp2
public static double lerp2(double pDelta1, double pDelta2, double pStart1, double pEnd1, double pStart2, double pEnd2) -
lerp3
public static double lerp3(double pDelta1, double pDelta2, double pDelta3, double pStart1, double pEnd1, double pStart2, double pEnd2, double pStart3, double pEnd3, double pStart4, double pEnd4) -
catmullrom
public static float catmullrom(float pDelta, float pControlPoint1, float pControlPoint2, float pControlPoint3, float pControlPoint4) -
smoothstep
public static double smoothstep(double pInput) -
smoothstepDerivative
public static double smoothstepDerivative(double pInput) -
sign
public static int sign(double pX) -
rotLerp
public static float rotLerp(float pDelta, float pStart, float pEnd) Linearly interpolates an angle between the start between the start and end values given as degrees.- Parameters:
pDelta
- 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 pDelta, double pStart, double pEnd) -
triangleWave
public static float triangleWave(float pInput, float pPeriod) -
square
public static float square(float pValue) -
square
public static double square(double pValue) -
square
public static int square(int pValue) -
square
public static long square(long pValue) -
clampedMap
public static double clampedMap(double pInput, double pInputMin, double pInputMax, double pOuputMin, double pOutputMax) -
clampedMap
public static float clampedMap(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) -
map
public static double map(double pInput, double pInputMin, double pInputMax, double pOutputMin, double pOutputMax) -
map
public static float map(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) -
wobble
public static double wobble(double pInput) -
roundToward
public static int roundToward(int pValue, int pFactor) 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 pX, int pY) 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 pRandom, int pMinInclusive, int pMaxInclusive) -
randomBetween
-
normal
Generates a value from a normal distribution with the given mean and deviation. -
lengthSquared
public static double lengthSquared(double pXDistance, double pYDistance) -
length
public static double length(double pXDistance, double pYDistance) -
lengthSquared
public static double lengthSquared(double pXDistance, double pYDistance, double pZDistance) -
length
public static double length(double pXDistance, double pYDistance, double pZDistance) -
lengthSquared
public static float lengthSquared(float pXDistance, float pYDistance, float pZDistance) -
quantize
public static int quantize(double pValue, int pFactor) Gets the value closest to zero that is not closer to zero than the given value and is a multiple of the factor. -
outFromOrigin
-
outFromOrigin
-
rotationAroundAxis
public static org.joml.Quaternionf rotationAroundAxis(org.joml.Vector3f p_304469_, org.joml.Quaternionf p_304737_, org.joml.Quaternionf p_304773_) -
mulAndTruncate
public static int mulAndTruncate(org.apache.commons.lang3.math.Fraction p_339645_, int p_339636_)
-