Skip to content
MathUtil

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 VectorUsage: start position
StartDirection VectorUsage: Starting direction
TargetLocation VectorUsage: target position
Angle numberUsage: Detection angle range: [-360360] type: Floating point number

Returns

booleantrue, 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)

ts
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 numberUsage: numeric value a range: unlimited data size type: floating point number
min numberUsage: minimum value range: unlimited data size min<max type: floating point number
max numberUsage: maximum range: unlimited data size min<max type: floating point number

Returns

numberCalculation 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.

ts
@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 numberUsage: Value to be calculated a range: Data size not limited type: Floating point number

Returns

numberCos 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.

ts
@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 numberUsage: Degree range: No limit on data size type: Floating point number

Returns

numberRadian 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.

ts
@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 numberUsage: Value x range: No limit on data size type: Floating point number
y numberUsage: numeric value y range: unlimited data size type: floating point number

Returns

numberThe 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.

ts
@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 numberUsage: numeric value a range: unlimited data size type: floating point number
b numberUsage: numeric value b range: unlimited data size type: floating point number
alpha numberUsage: Interpolation
range: [0,1] type: Floating point number

Returns

numberCalculation 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.

ts
@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 numberUsage: radians
range: unlimited data size type: floating-point number

Returns

numberdegrees

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 π.

ts
@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 numberUsage: minimum value range: unlimited data size type: floating point number
max numberUsage: maximum range: unlimited data size type: floating point number

Returns

numberFloating 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.

ts
@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 numberUsage: minimum value range: unlimited data size type: integer
max numberUsage: maximum range: unlimited data size type: integer

Returns

numberInteger 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.

ts
@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 numberUsage: Value to be calculated a range: Data size not limited type: Floating point number

Returns

numberSin 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.

ts
@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 numberUsage: Value to be calculated a range: Data size not limited type: Floating point number

Returns

numberTan 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.

ts
@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}`);
    }
}