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 org.joml.Vector3fstatic final org.joml.Vector3fstatic final org.joml.Vector3f -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic floatabs(float value) static intabs(int value) Returns the unsigned value of an int.static doubleabsMax(double x, double y) Returns the maximum of the absolute value of two numbers.static floatapproach(float value, float limit, float stepSize) Changes value by stepSize towards the limit and returns the result.static floatapproachDegrees(float angle, float limit, float stepSize) Changes the angle by stepSize towards the limit in the direction where the distance is smaller.static doubleatan2(double y, double x) static intbinarySearch(int min, int max, IntPredicate isTargetBeforeOrAt) static floatcatmullrom(float delta, float controlPoint1, float controlPoint2, float controlPoint3, float controlPoint4) static intceil(double value) static intceil(float value) static intceillog2(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 doubleclamp(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 floatclamp(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 intclamp(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 longclamp(long value, long min, long max) static doubleclampedLerp(double start, double end, double delta) Method for linear interpolation of doubles.static floatclampedLerp(float start, float end, float delta) Method for linear interpolation of floats.static doubleclampedMap(double input, double inputMin, double inputMax, double ouputMin, double outputMax) static floatclampedMap(float input, float inputMin, float inputMax, float outputMin, float outputMax) static intcolor(float r, float g, float b) Makes an integer color from the given red, green, and blue float valuesstatic floatcos(float value) cos looked up in the sin table with the appropriate offsetstatic UUIDstatic UUIDcreateInsecureUUID(RandomSource random) static floatdegreesDifference(float start, float end) Returns the difference between two angles in degrees.static floatdegreesDifferenceAbs(float start, float end) Returns the absolute of the difference between two angles in degrees.static booleanequal(double x, double y) static booleanequal(float x, float y) static floatfastInvCubeRoot(float number) static doublefastInvSqrt(double number) Deprecated.static intfloor(double value) Returns the greatest integer less than or equal to the double argument.static intfloor(float value) Returns the greatest integer less than or equal to the float argument.static intfloorDiv(int dividend, int divisor) static doublefrac(double number) Gets the decimal portion of the given double.static floatfrac(float number) static intParses the string as an integer or returns the second parameter if it fails.static longgetSeed(int x, int y, int z) Deprecated.static longDeprecated.static inthsvToArgb(float hue, float saturation, float value, int alpha) static inthsvToRgb(float hue, float saturation, float value) static doubleinverseLerp(double delta, double start, double end) static floatinverseLerp(float delta, float start, float end) static doubleinvSqrt(double number) static floatinvSqrt(float number) static booleanisMultipleOf(int number, int multiple) static booleanisPowerOfTwo(int value) Is the given value a power of two? (1, 2, 4, 8, 16, ...)static doublelength(double xDistance, double yDistance) static doublelength(double xDistance, double yDistance, double zDistance) static doublelengthSquared(double xDistance, double yDistance) static doublelengthSquared(double xDistance, double yDistance, double zDistance) static floatlengthSquared(float xDistance, float yDistance, float zDistance) static doublelerp(double delta, double start, double end) Method for linear interpolation of doublesstatic floatlerp(float delta, float start, float end) Method for linear interpolation of floatsstatic doublelerp2(double delta1, double delta2, double start1, double end1, double start2, double end2) static doublelerp3(double delta1, double delta2, double delta3, double start1, double end1, double start2, double end2, double start3, double end3, double start4, double end4) static intlerpDiscrete(float delta, int start, int end) static intlerpInt(float delta, int start, int end) static longlfloor(double value) Long version of floor()static intlog2(int value) Efficiently calculates the floor of the base-2 log of an integer value.static doublemap(double input, double inputMin, double inputMax, double outputMin, double outputMax) static floatmap(float input, float inputMin, float inputMax, float outputMin, float outputMax) static intmulAndTruncate(org.apache.commons.lang3.math.Fraction fraction, int factor) static intmurmurHash3Mixer(int input) static doublenextDouble(RandomSource random, double minimum, double maximum) static floatnextFloat(RandomSource random, float minimum, float maximum) static intnextInt(RandomSource random, int minimum, int maximum) static floatnormal(RandomSource random, float mean, float deviation) Generates a value from a normal distribution with the given mean and deviation.static IntStreamoutFromOrigin(int input, int lowerBound, int upperBound) static IntStreamoutFromOrigin(int input, int lowerBound, int upperBound, int steps) static intpositiveCeilDiv(int x, int y) Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.static doublepositiveModulo(double numerator, double denominator) static floatpositiveModulo(float numerator, float denominator) static intpositiveModulo(int x, int y) static intquantize(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 floatrandomBetween(RandomSource random, float minInclusive, float maxExclusive) static intrandomBetweenInclusive(RandomSource random, int minInclusive, int maxInclusive) static booleanrayIntersectsAABB(Vec3 start, Vec3 end, AABB boundingBox) static floatrotateIfNecessary(float rotationToAdjust, float actualRotation, float maxDifference) Takes a rotation and compares it to another rotation.static org.joml.QuaternionfrotationAroundAxis(org.joml.Vector3f axis, org.joml.Quaternionf cameraOrentation, org.joml.Quaternionf output) static doublerotLerp(double delta, double start, double end) static floatrotLerp(float delta, float start, float end) Linearly interpolates an angle between the start between the start and end values given as degrees.static introundToward(int value, int factor) Rounds the given value up to a multiple of factor.static intsign(double x) static floatsin(float value) sin looked up in a tablestatic intsmallestEncompassingPowerOfTwo(int value) Returns the input value rounded up to the next highest power of two.static doublesmoothstep(double input) static doublesmoothstepDerivative(double input) static floatsqrt(float value) static doublesquare(double value) static floatsquare(float value) static intsquare(int value) static longsquare(long value) static floattriangleWave(float input, float period) static doublewobble(double input) static doublewrapDegrees(double value) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static floatwrapDegrees(float value) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static intwrapDegrees(int angle) 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 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
-
nextFloat
-
nextDouble
-
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
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. -
getSeed
Deprecated. -
createInsecureUUID
-
createInsecureUUID
-
inverseLerp
public static double inverseLerp(double delta, double start, double end) -
inverseLerp
public static float inverseLerp(float delta, float start, float end) -
rayIntersectsAABB
-
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.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
-
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 lerpend- 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 lerpend- 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
-
randomBetween
-
normal
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
-
outFromOrigin
-
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)
-