Class Mth
java.lang.Object
net.minecraft.util.Mth
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final double[]private static final intprivate 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 float[]private static final intprivate static final intprivate static final doublestatic 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 v) static intabs(int v) Returns the unsigned value of an int.static doubleabsMax(double a, double b) Returns the maximum of the absolute value of two numbers.static floatabsMax(float a, float b) static intabsMax(int a, int b) static floatapproach(float current, float target, float increment) Changes value by stepSize towards the limit and returns the result.static floatapproachDegrees(float current, float target, float increment) 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 from, int to, IntPredicate condition) static floatcatmullrom(float alpha, float p0, float p1, float p2, float p3) static intceil(double v) static intceil(float v) static intceillog2(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 longceilLong(double v) static intchessboardDistance(int x0, int z0, int x1, int z1) 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 factor, double min, double max) Method for linear interpolation of doubles.static floatclampedLerp(float factor, float min, float max) Method for linear interpolation of floats.static doubleclampedMap(double value, double fromMin, double fromMax, double toMin, double toMax) static floatclampedMap(float value, float fromMin, float fromMax, float toMin, float toMax) static floatcos(double i) static UUIDcreateInsecureUUID(RandomSource random) static floatcube(float x) static floatdegreesDifference(float fromAngle, float toAngle) Returns the difference between two angles in degrees.static floatdegreesDifferenceAbs(float angleA, float angleB) Returns the absolute of the difference between two angles in degrees.static booleanequal(double a, double b) static booleanequal(float a, float b) static floatfastInvCubeRoot(float x) static doublefastInvSqrt(double x) Deprecated.static intfloor(double v) Returns the greatest integer less than or equal to the double argument.static intfloor(float v) Returns the greatest integer less than or equal to the float argument.static intfloorDiv(int a, int b) static doublefrac(double num) Gets the decimal portion of the given double.static floatfrac(float num) 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 value, double min, double max) static floatinverseLerp(float value, float min, float max) static doubleinvSqrt(double x) static floatinvSqrt(float x) static booleanisMultipleOf(int dividend, int divisor) static booleanisPowerOfTwo(int input) Is the given value a power of two?static doublelength(double x, double y) static doublelength(double x, double y, double z) static floatlength(float x, float y) static doublelengthSquared(double x, double y) static doublelengthSquared(double x, double y, double z) static floatlengthSquared(float x, float y, float z) static doublelerp(double alpha1, double p0, double p1) Method for linear interpolation of doublesstatic Vec3static floatlerp(float alpha1, float p0, float p1) Method for linear interpolation of floatsstatic doublelerp2(double alpha1, double alpha2, double x00, double x10, double x01, double x11) static doublelerp3(double alpha1, double alpha2, double alpha3, double x000, double x100, double x010, double x110, double x001, double x101, double x011, double x111) static intlerpDiscrete(float alpha1, int p0, int p1) static intlerpInt(float alpha1, int p0, int p1) static longlfloor(double v) Long version of floor()static intlog2(int input) Efficiently calculates the floor of the base-2 log of an integer value.static doublemap(double value, double fromMin, double fromMax, double toMin, double toMax) static floatmap(float value, float fromMin, float fromMax, float toMin, float toMax) static intmulAndTruncate(org.apache.commons.lang3.math.Fraction fraction, int factor) static intmurmurHash3Mixer(int hash) static doublenextDouble(RandomSource random, double min, double max) static floatnextFloat(RandomSource random, float min, float max) static intnextInt(RandomSource random, int minInclusive, int maxInclusive) static floatnormal(RandomSource random, float mean, float deviation) Generates a value from a normal distribution with the given mean and deviation.static IntStreamoutFromOrigin(int origin, int lowerBound, int upperBound) static IntStreamoutFromOrigin(int origin, int lowerBound, int upperBound, int stepSize) static bytepackDegrees(float angle) static intpositiveCeilDiv(int input, int divisor) Returns the smallest (closest to negative infinity) int value that is greater than or equal to the algebraic quotient.static doublepositiveModulo(double input, double mod) static floatpositiveModulo(float input, float mod) static intpositiveModulo(int input, int mod) static intquantize(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 floatrandomBetween(RandomSource random, float min, float maxExclusive) static intrandomBetweenInclusive(RandomSource random, int min, int maxInclusive) static booleanrayIntersectsAABB(Vec3 rayStart, Vec3 rayDir, AABB aabb) static floatrotateIfNecessary(float baseAngle, float targetAngle, float maxAngleDiff) Takes a rotation and compares it to another rotation.static QuaternionfrotationAroundAxis(Vector3f axis, Quaternionf rotation, Quaternionf result) static doublerotLerp(double a, double from, double to) static floatrotLerp(float a, float from, float to) Linearly interpolates an angle between the start between the start and end values given as degrees.static floatrotLerpRad(float a, float from, float to) static introundToward(int input, int multiple) Rounds the given value up to a multiple of factor.static intsign(double number) static floatsin(double i) static intsmallestEncompassingPowerOfTwo(int input) Returns the input value rounded up to the next highest power of two.static intsmallestSquareSide(int itemCount) static doublesmoothstep(double x) static doublesmoothstepDerivative(double x) static floatsqrt(float x) static doublesquare(double x) static floatsquare(float x) static intsquare(int x) static longsquare(long x) static floattriangleWave(float index, float period) static floatunpackDegrees(byte rot) static doublewobble(double coord) static doublewrapDegrees(double angle) The angle is reduced to an angle between -180 and +180 by mod, and a 360 check.static floatwrapDegrees(float angle) 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)static floatwrapDegrees(long angle)
-
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
-
X_AXIS
-
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_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(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
-
nextFloat
-
nextDouble
-
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
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. -
getSeed
Deprecated. -
createInsecureUUID
-
inverseLerp
public static double inverseLerp(double value, double min, double max) -
inverseLerp
public static float inverseLerp(float value, float min, float max) -
rayIntersectsAABB
-
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.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
-
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 lerpp1- End value for the lerp
-
lerp
-
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 lerpp1- 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
-
randomBetween
-
normal
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
-
outFromOrigin
-
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)
-