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 floatstatic final floatprivate static final doubleprivate static final intstatic final floatprivate static final intprivate static final int[]Though it looks like an array, this is really more like a mapping.private static final doublestatic final floatstatic final floatprivate static final RandomSourceprivate static final float[]private static final floatstatic final floatstatic final floatprivate static final longprivate static final longprivate static final longprivate static final longstatic final Vector3fstatic final Vector3fstatic final Vector3f -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floatabs(float pValue) static intabs(int pValue) Returns the unsigned value of an int.static doubleabsMax(double pX, double pY) Returns the maximum of the absolute value of two numbers.static floatapproach(float pValue, float pLimit, float pStepSize) Changes value by stepSize towards the limit and returns the result.static floatapproachDegrees(float pAngle, float pLimit, float pStepSize) Changes the angle by stepSize towards the limit in the direction where the distance is smaller.static doubleatan2(double pY, double pX) static intbinarySearch(int pMin, int pMax, IntPredicate pIsTargetBeforeOrAt) static floatcatmullrom(float pDelta, float pControlPoint1, float pControlPoint2, float pControlPoint3, float pControlPoint4) static intceil(double pValue) static intceil(float pValue) static intceillog2(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 longceilLong(double pValue) static doubleclamp(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 floatclamp(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 intclamp(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 longclamp(long pValue, long pMin, long pMax) static doubleclampedLerp(double pStart, double pEnd, double pDelta) Method for linear interpolation of doubles.static floatclampedLerp(float pStart, float pEnd, float pDelta) Method for linear interpolation of floats.static doubleclampedMap(double pInput, double pInputMin, double pInputMax, double pOutputMin, double pOutputMax) static floatclampedMap(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) static floatcos(float pValue) cos looked up in the sin table with the appropriate offsetstatic UUIDstatic UUIDcreateInsecureUUID(RandomSource pRandom) static floatdegreesDifference(float pStart, float pEnd) Returns the difference between two angles in degrees.static floatdegreesDifferenceAbs(float pStart, float pEnd) Returns the absolute of the difference between two angles in degrees.static floateaseInOutSine(float pValue) static booleanequal(double pX, double pY) static booleanequal(float pX, float pY) static floatfastInvCubeRoot(float pNumber) static doublefastInvSqrt(double pNumber) Deprecated.static intfloor(double pValue) Returns the greatest integer less than or equal to the double argument.static intfloor(float pValue) Returns the greatest integer less than or equal to the float argument.static intfloorDiv(int pDividend, int pDivisor) static doublefrac(double pNumber) Gets the decimal portion of the given double.static floatfrac(float pNumber) static intParses the string as an integer or returns the second parameter if it fails.static longgetSeed(int pX, int pY, int pZ) Deprecated.static longDeprecated.static inthsvToArgb(float pHue, float pSaturation, float pValue, int pAlpha) static inthsvToRgb(float pHue, float pSaturation, float pValue) static doubleinverseLerp(double pDelta, double pStart, double pEnd) static floatinverseLerp(float pDelta, float pStart, float pEnd) static doubleinvSqrt(double pNumber) static floatinvSqrt(float pNumber) static booleanisMultipleOf(int pNumber, int pMultiple) static booleanisPowerOfTwo(int pValue) Is the given value a power of two?static doublelength(double pXDistance, double pYDistance) static doublelength(double pXDistance, double pYDistance, double pZDistance) static floatlength(float pXDistance, float pYDistance) static doublelengthSquared(double pXDistance, double pYDistance) static doublelengthSquared(double pXDistance, double pYDistance, double pZDistance) static floatlengthSquared(float pXDistance, float pYDistance, float pZDistance) static doublelerp(double pDelta, double pStart, double pEnd) Method for linear interpolation of doublesstatic Vec3static floatlerp(float pDelta, float pStart, float pEnd) Method for linear interpolation of floatsstatic doublelerp2(double pDelta1, double pDelta2, double pStart1, double pEnd1, double pStart2, double pEnd2) static doublelerp3(double pDelta1, double pDelta2, double pDelta3, double pStart1, double pEnd1, double pStart2, double pEnd2, double pStart3, double pEnd3, double pStart4, double pEnd4) static intlerpDiscrete(float pDelta, int pStart, int pEnd) static intlerpInt(float pDelta, int pStart, int pEnd) static longlfloor(double pValue) Long version of floor()static intlog2(int pValue) Efficiently calculates the floor of the base-2 log of an integer value.static doublemap(double pInput, double pInputMin, double pInputMax, double pOutputMin, double pOutputMax) static floatmap(float pInput, float pInputMin, float pInputMax, float pOutputMin, float pOutputMax) static intmulAndTruncate(org.apache.commons.lang3.math.Fraction pFraction, int pFactor) static intmurmurHash3Mixer(int pInput) static doublenextDouble(RandomSource pRandom, double pMinimum, double pMaximum) static floatnextFloat(RandomSource pRandom, float pMinimum, float pMaximum) static intnextInt(RandomSource pRandom, int pMinimum, int pMaximum) static floatnormal(RandomSource pRandom, float pMean, float pDeviation) Generates a value from a normal distribution with the given mean and deviation.static IntStreamoutFromOrigin(int pInput, int pLowerBound, int pUpperBound) static IntStreamoutFromOrigin(int pInput, int pLowerBound, int pUpperBound, int pSteps) static bytepackDegrees(float pDegrees) static intpositiveCeilDiv(int pX, int pY) Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.static doublepositiveModulo(double pNumerator, double pDenominator) static floatpositiveModulo(float pNumerator, float pDenominator) static intpositiveModulo(int pX, int pY) static intquantize(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 floatrandomBetween(RandomSource pRandom, float pMinInclusive, float pMaxExclusive) static intrandomBetweenInclusive(RandomSource pRandom, int pMinInclusive, int pMaxInclusive) static booleanrayIntersectsAABB(Vec3 pStart, Vec3 pEnd, AABB pBoundingBox) static floatrotateIfNecessary(float pRotationToAdjust, float pActualRotation, float pMaxDifference) Takes a rotation and compares it to another rotation.static QuaternionfrotationAroundAxis(Vector3f pAxis, Quaternionf pCameraOrientation, Quaternionf pOutput) static doublerotLerp(double pDelta, double pStart, double pEnd) static floatrotLerp(float pDelta, float pStart, float pEnd) Linearly interpolates an angle between the start between the start and end values given as degrees.static floatrotLerpRad(float pDelta, float pStart, float pEnd) static introundToward(int pValue, int pFactor) Rounds the given value up to a multiple of factor.static intsign(double pX) static floatsin(float pValue) sin looked up in a tablestatic intsmallestEncompassingPowerOfTwo(int pValue) Returns the input value rounded up to the next highest power of two.static intsmallestSquareSide(int pItemCount) static doublesmoothstep(double pInput) static doublesmoothstepDerivative(double pInput) static floatsqrt(float pValue) static doublesquare(double pValue) static floatsquare(float pValue) static intsquare(int pValue) static longsquare(long pValue) static floattriangleWave(float pInput, float pPeriod) static floatunpackDegrees(byte pDegrees) static doublewobble(double pInput) static doublewrapDegrees(double pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static floatwrapDegrees(float pValue) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static intwrapDegrees(int pAngle) Adjust the angle so that its value is in the range [-180;180)static floatwrapDegrees(long pAngle)
-
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
-
X_AXIS
-
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) -
ceilLong
public static long ceilLong(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) -
packDegrees
public static byte packDegrees(float pDegrees) -
unpackDegrees
public static float unpackDegrees(byte pDegrees) -
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(long pAngle) -
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
-
smallestSquareSide
public static int smallestSquareSide(int pItemCount) -
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. -
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
-
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) -
rotLerpRad
public static float rotLerpRad(float pDelta, float pStart, float 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 pOutputMin, 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) -
length
public static float length(float pXDistance, float 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 Quaternionf rotationAroundAxis(Vector3f pAxis, Quaternionf pCameraOrientation, Quaternionf pOutput) -
mulAndTruncate
public static int mulAndTruncate(org.apache.commons.lang3.math.Fraction pFraction, int pFactor) -
easeInOutSine
public static float easeInOutSine(float pValue)
-