Utils / MathUtil
MathUtil Class
Mathematical Library Tools
Table of contents
Properties
D2R: number |
---|
Angle conversion radian parameter (Math. PI/180) |
DBL_DECIMAL_DIG: number |
The decimal precision (double) required for serializing/deserializing floating-point values |
DBL_DIG: number |
The decimal digits (double) that are retained in the round trip conversion of text and will not change due to rounding or overflow |
DBL_EPSILON: number |
1 The difference between the minimum floating-point number greater than 1. (double) |
DBL_HAS_SUBNORM: number |
Indicates whether the type support informal values: -1 is indeterminate, 0 is not support, and 1 is support. (double) |
DBL_MANT_DIG: number |
Number of significant digits (tail) (double) |
DBL_MAX: number |
The value of the maximum valid floating-point number (which is a positive number), that is, the maximum value of the floating-point number. (double) |
DBL_MAX_10_EXP: number |
They are the finite maximum positive integers (doubles) that can be represented by subtracting the integer 10 by one power |
DBL_MAX_EXP: number |
Maximum binary index (double) |
DBL_MIN: number |
Minimum normalized positive value (double) |
DBL_MIN_10_EXP: number |
The smallest negative integer (double) that can be normalized by subtracting the integer 10 by one power |
DBL_MIN_EXP: number |
The minimum negative integer (double) that can be normalized by subtracting the integer FLT_SADIX by one power |
DBL_RADIX: number |
Base (base) (double) |
DBL_TRUE_MIN: number |
Minimum positive value (double) |
EPSILON: number |
Minimum number of errors |
FLOAT_NON_FRACTIONAL: number |
All single precision floating-point numbers greater than or equal to this value have no decimals |
FLT_DECIMAL_DIG: number |
The decimal precision required for serializing/deserializing floating-point values (float) |
FLT_DIG: number |
The decimal digits (float) that are retained in the round trip conversion of text and will not change due to rounding or overflow |
FLT_EPSILON: number |
1 The difference between the minimum floating-point number greater than 1. (float) |
FLT_GUARD: number |
Is a guard bit (e.g. 0) used in the arithmetic result |
FLT_HAS_SUBNORM: number |
Indicates whether the type support informal values: -1 is indeterminate, 0 is not support, and 1 is support. |
FLT_MANT_DIG: number |
Number of significant digits (tail) (float) |
FLT_MAX: number |
The value of the maximum valid floating-point number (which is a positive number), that is, the maximum value of the floating-point number. (float) |
FLT_MAX_10_EXP: number |
After converting to decimal form, the maximum value (positive) of the exponent of the normalized floating-point number. (float) |
FLT_MAX_EXP: number |
Maximum binary index (float) |
FLT_MIN: number |
Minimum normal value (float) |
FLT_MIN_10_EXP: number |
Minimum decimal exponent (float) |
FLT_MIN_EXP: number |
Minimum binary index (float) |
FLT_NORMALIZE: number |
Instructions should always standardize floating-point numbers. |
FLT_RADIX: number |
Base (base) (float) |
FLT_TRUE_MIN: number |
Minimum positive value (float) |
HALF_PI: number |
PI/2 |
INV_PI: number |
1/PI |
R2D: number |
Arc conversion angle parameter |
SIGN_BIT: number |
Byte offset |
Methods
angleCheck(StartLocation : Vector , StartDirection : Vector , TargetLocation : Vector , Angle : number ): boolean other |
---|
Judge whether the position of the end point is within a certain angle of the position of the start point in a certain direction |
clamp(a : number , min : number , max : number ): number other |
Limit the incoming value a within the range of min and max, and discard any excess automatically |
cos(a : number ): number other |
Calculate cos value |
degreesToRadians(a : number ): number other |
Return the radian value based on the input degree |
fmod(x : number , y : number ): number other |
The remainder of dividing the value x by the value y |
lerp(a : number , b : number , alpha : number ): number other |
Linear interpolation between values a and b based on alpha |
radiansToDegrees(a : number ): number other |
Return degrees based on the input radian value |
randomFloat(min : number , max : number ): number other |
Obtain floating point numbers within a random range [including min, excluding max) |
randomInt(min : number , max : number ): number other |
Get integers within a random range [including min, excluding max) |
sin(a : number ): number other |
Calculate sin value |
tan(a : number ): number other |
Calculate tan value |
Properties
D2R
▪ Static
D2R: number
Angle conversion radian parameter (Math. PI/180)
DBL_DECIMAL_DIG
▪ Static
DBL_DECIMAL_DIG: number
The decimal precision (double) required for serializing/deserializing floating-point values
DBL_DIG
▪ Static
DBL_DIG: number
The decimal digits (double) that are retained in the round trip conversion of text and will not change due to rounding or overflow
DBL_EPSILON
▪ Static
DBL_EPSILON: number
1 The difference between the minimum floating-point number greater than 1. (double)
DBL_HAS_SUBNORM
▪ Static
DBL_HAS_SUBNORM: number
Indicates whether the type support informal values: -1 is indeterminate, 0 is not support, and 1 is support. (double)
DBL_MANT_DIG
▪ Static
DBL_MANT_DIG: number
Number of significant digits (tail) (double)
DBL_MAX
▪ Static
DBL_MAX: number
The value of the maximum valid floating-point number (which is a positive number), that is, the maximum value of the floating-point number. (double)
DBL_MAX_10_EXP
▪ Static
DBL_MAX_10_EXP: number
They are the finite maximum positive integers (doubles) that can be represented by subtracting the integer 10 by one power
DBL_MAX_EXP
▪ Static
DBL_MAX_EXP: number
Maximum binary index (double)
DBL_MIN
▪ Static
DBL_MIN: number
Minimum normalized positive value (double)
DBL_MIN_10_EXP
▪ Static
DBL_MIN_10_EXP: number
The smallest negative integer (double) that can be normalized by subtracting the integer 10 by one power
DBL_MIN_EXP
▪ Static
DBL_MIN_EXP: number
The minimum negative integer (double) that can be normalized by subtracting the integer FLT_SADIX by one power
DBL_RADIX
▪ Static
DBL_RADIX: number
Base (base) (double)
DBL_TRUE_MIN
▪ Static
DBL_TRUE_MIN: number
Minimum positive value (double)
EPSILON
▪ Static
EPSILON: number
Minimum number of errors
FLOAT_NON_FRACTIONAL
▪ Static
FLOAT_NON_FRACTIONAL: number
All single precision floating-point numbers greater than or equal to this value have no decimals
FLT_DECIMAL_DIG
▪ Static
FLT_DECIMAL_DIG: number
The decimal precision required for serializing/deserializing floating-point values (float)
FLT_DIG
▪ Static
FLT_DIG: number
The decimal digits (float) that are retained in the round trip conversion of text and will not change due to rounding or overflow
FLT_EPSILON
▪ Static
FLT_EPSILON: number
1 The difference between the minimum floating-point number greater than 1. (float)
FLT_GUARD
▪ Static
FLT_GUARD: number
Is a guard bit (e.g. 0) used in the arithmetic result
FLT_HAS_SUBNORM
▪ Static
FLT_HAS_SUBNORM: number
Indicates whether the type support informal values: -1 is indeterminate, 0 is not support, and 1 is support.
FLT_MANT_DIG
▪ Static
FLT_MANT_DIG: number
Number of significant digits (tail) (float)
FLT_MAX
▪ Static
FLT_MAX: number
The value of the maximum valid floating-point number (which is a positive number), that is, the maximum value of the floating-point number. (float)
FLT_MAX_10_EXP
▪ Static
FLT_MAX_10_EXP: number
After converting to decimal form, the maximum value (positive) of the exponent of the normalized floating-point number. (float)
FLT_MAX_EXP
▪ Static
FLT_MAX_EXP: number
Maximum binary index (float)
FLT_MIN
▪ Static
FLT_MIN: number
Minimum normal value (float)
FLT_MIN_10_EXP
▪ Static
FLT_MIN_10_EXP: number
Minimum decimal exponent (float)
FLT_MIN_EXP
▪ Static
FLT_MIN_EXP: number
Minimum binary index (float)
FLT_NORMALIZE
▪ Static
FLT_NORMALIZE: number
Instructions should always standardize floating-point numbers.
FLT_RADIX
▪ Static
FLT_RADIX: number
Base (base) (float)
FLT_TRUE_MIN
▪ Static
FLT_TRUE_MIN: number
Minimum positive value (float)
HALF_PI
▪ Static
HALF_PI: number
PI/2
INV_PI
▪ Static
INV_PI: number
1/PI
R2D
▪ Static
R2D: number
Arc conversion angle parameter
SIGN_BIT
▪ Static
SIGN_BIT: number
Byte offset
Methods
angleCheck
• Static
angleCheck(StartLocation
, StartDirection
, TargetLocation
, Angle
): boolean
other
Judge whether the position of the end point is within a certain angle of the position of the start point in a certain direction
Parameters
StartLocation Vector | Usage: start position |
---|---|
StartDirection Vector | Usage: Starting direction |
TargetLocation Vector | Usage: target position |
Angle number | Usage: Detection angle range: [-360360] type: Floating point number |
Returns
boolean | true, Within the angle range |
---|
Example usage: The basic process of detecting a 60 degree angle in the X direction of the origin is as follows, assuming the target position is (100, 20, 20)
if(angleCheck(new mw.Vector(0,0,0), new mw.Vector(1,0,0), new mw.Vector(100,20,20), 60)) {
// Within the scope
}
else {
// Not within the scope
}
if(angleCheck(new mw.Vector(0,0,0), new mw.Vector(1,0,0), new mw.Vector(100,20,20), 60)) {
// Within the scope
}
else {
// Not within the scope
}
clamp
• Static
clamp(a
, min
, max
): number
other
Limit the incoming value a within the range of min and max, and discard any excess automatically
Parameters
a number | Usage: numeric value a range: unlimited data size type: floating point number |
---|---|
min number | Usage: minimum value range: unlimited data size min<max type: floating point number |
max number | Usage: maximum range: unlimited data size min<max type: floating point number |
Returns
number | Calculation results |
---|
Example usage: Create a script named MathExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the log will output a maximum value of 10 from 20.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let clamp = MathUtil.clamp(20, 0, 10);
console.log(`clamp: ${clamp}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let clamp = MathUtil.clamp(20, 0, 10);
console.log(`clamp: ${clamp}`);
}
}
cos
• Static
cos(a
): number
other
Calculate cos value
Parameters
a number | Usage: Value to be calculated a range: Data size not limited type: Floating point number |
---|
Returns
number | Cos value |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output a cos value of -1.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output cos value in radians
private async test(): Promise<void> {
let cos = MathUtil.cos(Math.PI);
console.log(`cos: ${cos}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output cos value in radians
private async test(): Promise<void> {
let cos = MathUtil.cos(Math.PI);
console.log(`cos: ${cos}`);
}
}
degreesToRadians
• Static
degreesToRadians(a
): number
other
Return the radian value based on the input degree
Parameters
a number | Usage: Degree range: No limit on data size type: Floating point number |
---|
Returns
number | Radian value |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output the radian value of 180 degrees.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let degree = 180;
let radians = MathUtil.degreesToRadians(degree);
console.log(`degreesToRadians: ${radians}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let degree = 180;
let radians = MathUtil.degreesToRadians(degree);
console.log(`degreesToRadians: ${radians}`);
}
}
fmod
• Static
fmod(x
, y
): number
other
The remainder of dividing the value x by the value y
Parameters
x number | Usage: Value x range: No limit on data size type: Floating point number |
---|---|
y number | Usage: numeric value y range: unlimited data size type: floating point number |
Returns
number | The remainder of dividing the value x by the value y |
---|
Example usage: Create a script called MathExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the log will output the remainder of 20 divided by 3.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let fmod = MathUtil.fmod(20, 3);
console.log(`fmod: ${fmod}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let fmod = MathUtil.fmod(20, 3);
console.log(`fmod: ${fmod}`);
}
}
lerp
• Static
lerp(a
, b
, alpha
): number
other
Linear interpolation between values a and b based on alpha
Parameters
a number | Usage: numeric value a range: unlimited data size type: floating point number |
---|---|
b number | Usage: numeric value b range: unlimited data size type: floating point number |
alpha number | Usage: Interpolation range: [0,1] type: Floating point number |
Returns
number | Calculation results |
---|
Precautions
alpha=0 100% of the time value a and 100% of the time value b when alpha=1
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output the intermediate value between 1 and 10.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let lerp = MathUtil.lerp(1, 10, 0.5);
console.log(`lerp: ${lerp}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let lerp = MathUtil.lerp(1, 10, 0.5);
console.log(`lerp: ${lerp}`);
}
}
radiansToDegrees
• Static
radiansToDegrees(a
): number
other
Return degrees based on the input radian value
Parameters
a number | Usage: radians range: unlimited data size type: floating-point number |
---|
Returns
number | degrees |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output the degree of π.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let radian = Math.PI;
let degrees = MathUtil.radiansToDegrees(radian);
console.log(`radiansToDegrees: ${degrees}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let radian = Math.PI;
let degrees = MathUtil.radiansToDegrees(radian);
console.log(`radiansToDegrees: ${degrees}`);
}
}
randomFloat
• Static
randomFloat(min
, max
): number
other
Obtain floating point numbers within a random range [including min, excluding max)
Parameters
min number | Usage: minimum value range: unlimited data size type: floating point number |
---|---|
max number | Usage: maximum range: unlimited data size type: floating point number |
Returns
number | Floating point numbers within a random range |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output random floating point numbers between 1 and 10.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let randomFloat = MathUtil.randomFloat(1, 10);
console.log(`randomFloat: ${randomFloat}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let randomFloat = MathUtil.randomFloat(1, 10);
console.log(`randomFloat: ${randomFloat}`);
}
}
randomInt
• Static
randomInt(min
, max
): number
other
Get integers within a random range [including min, excluding max)
Parameters
min number | Usage: minimum value range: unlimited data size type: integer |
---|---|
max number | Usage: maximum range: unlimited data size type: integer |
Returns
number | Integer within a random range |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output a random integer between 1 and 10.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let randomInt = MathUtil.randomInt(1, 10);
console.log(`randomInt: ${randomInt}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
private async test(): Promise<void> {
let randomInt = MathUtil.randomInt(1, 10);
console.log(`randomInt: ${randomInt}`);
}
}
sin
• Static
sin(a
): number
other
Calculate sin value
Parameters
a number | Usage: Value to be calculated a range: Data size not limited type: Floating point number |
---|
Returns
number | Sin value |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and the log will output a sin value of 1.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output sin value in radians
private async test(): Promise<void> {
let sin = MathUtil.sin(Math.PI / 2);
console.log(`sin: ${sin}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output sin value in radians
private async test(): Promise<void> {
let sin = MathUtil.sin(Math.PI / 2);
console.log(`sin: ${sin}`);
}
}
tan
• Static
tan(a
): number
other
Calculate tan value
Parameters
a number | Usage: Value to be calculated a range: Data size not limited type: Floating point number |
---|
Returns
number | Tan value |
---|
Usage example: create a script named MathExample, place it in the object bar, open the script, modify the original content as follows, save and run the game, and log will output Math The tan value of PI/4.
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output tan value in radians
private async test(): Promise<void> {
let tan = MathUtil.tan(Math.PI / 4);
console.log(`tan: ${tan}`);
}
}
@Component
export default class MathExample extends Script {
protected onStart(): void {
if (!SystemUtil.isClient()) return;
this.test();
}
//Output tan value in radians
private async test(): Promise<void> {
let tan = MathUtil.tan(Math.PI / 4);
console.log(`tan: ${tan}`);
}
}