MathUtils

MathUtils

A collection of useful math-related functions, constants and helpers.
Only used to define the class. Should never be instantiated.

Constructor

new MathUtils()

Members

(static) DEG_TO_RAD :number

Example

Converts 75 degrees to radians

var rot = 75 * MathUtils.DEG_TO_RAD;

(static) EPSILON :number

Example

Uses EPSILON to approximate floating point equality

if (Math.abs(a - b) > MathUtils.EPSILON) {
    // not equal
}

(static) HALF_PI :number

Example
// uses HALF_PI and converts it to degrees
var rot = MathUtils.HALF_PI * MathUtils.RAD_TO_DEG; // rot == 90

(static) RAD_TO_DEG :number

Example

Converts Math.PI to 180 degrees

var rot = Math.PI  MathUtils.RAD_TO_DEG;

(static) randomSeed :number

Example
// sets random seed to use with MathUtils.fastRandom()
MathUtils.randomSeed = 1337;

(static) TWO_PI :number

Example

Uses TWO_PI and converts it to degrees

var rot = MathUtils.TWO_PI * RAD_TO_DEG; // rot == 360

Methods

(static) barycentricInterpolation(t1, t2, t3, p) → {Vector3}

Computes the height of a point located inside a triangle. Height is assumed to bound to the Z axis.

Parameters:
Name Type Description
t1 Vector3

First point of the triangle

t2 Vector3

Second point of the triangle

t3 Vector3

Third point of the triangle

p Vector3

The point for which to compute the height

(static) cartesianToSpherical(x, y, z, store)

Converts a point from Cartesian coordinates to Spherical radian coordinates (using positive Y as up) and stores the results in the store var.

Parameters:
Name Type Description
x number

x axis coordinate

y number

y axis coordinate

z number

z axis coordinate

store Vector3

storage to return the computed Spherical coordinates

Example
var sphericalCoord = new Vector3();
var pos = entity.transformComponent.transform.translation.
MathUtils.cartesianToSpherical(pos.x, pos.y, pos.z, sphericalCoord);

(static) clamp(value, min, max) → {number}

Clamps a value to a given interval. The interval is defined by min and max where min should be smaller than max. If min is greater
than max, the two parameters are reversed.

Parameters:
Name Type Description
value number

Input value.

min number

Lower bound of interval (inclusive).

max number

Upper bound of interval (inclusive).

Example
var a = -1;
a = MathUtils.clamp(a, 0, 9); // a == 0

(static) closeTo(v1, v2, toleranceopt) → {boolean}

Returns true if the 2 values supplied are approximately the same

Parameters:
Name Type Attributes Default Description
v1 number

first value to compare

v2 number

second value to compare

tolerance number <optional>
0.001

tolerance for comparison

(static) degFromRad(radians) → {number}

Converts an angle from radians to degrees.

Parameters:
Name Type Description
radians number

Angle in radians.

Example

Converts Math.PI to 180 degrees

var a = MathUtils.degFromRad(Math.PI);

(static) epsilonEquals(a, b, epsilon) → {boolean}

Calculate if two number are equals based on the value of epsilon

Parameters:
Name Type Description
a number

The first number

b number

The first number

epsilon number

Ratio representing the maximum difference below which the numbers will be considered equal (e.g. 0.1 allows for 10% difference).

(static) fastRandom() → {number}

Rough random generation with seeding. Set random seed through MathUtils.randomSeed = {new seed value}

(static) floatToHalfFloat(fval) → {number}

Converts a float to half-float representation.

Parameters:
Name Type Description
fval number

The float value to convert

(static) getTriangleNormal(p1x, p1y, p1z, p2x, p2y, p2z, p3x, p3y, p3z) → {Array.<number>}

Computes the normal of a given triangle

Parameters:
Name Type Description
p1x number

x coordinate of the first point of the triangle

p1y number

y coordinate of the first point of the triangle

p1z number

z coordinate of the first point of the triangle

p2x number

x coordinate of the second point of the triangle

p2y number

y coordinate of the second point of the triangle

p2z number

z coordinate of the second point of the triangle

p3x number

x coordinate of the third point of the triangle

p3y number

y coordinate of the third point of the triangle

p3z number

z coordinate of the third point of the triangle

(static) inverseLerp(value, start, end) → {number}

Calculates the linear parameter t that produces the interpolant value within the range [start, end]

Parameters:
Name Type Description
value number

The value to find the interpolator for.

start number

The start value.

end number

The end value.

(static) isPowerOfTwo(value) → {boolean}

Checks if a value is power of two

Parameters:
Name Type Description
value number

Number to check for power of two

(static) lerp(factor, start, end) → {number}

Linearly interpolates between two values. Extrapolates if factor is smaller than zero or greater than one.

Parameters:
Name Type Description
factor number

Factor of interpolation.

start number

Start value.

end number

End value.

Example
// earlier in code (outside of the update loop)
var x = 0;
// inside the update loop
x = MathUtils.lerp(tpf, x, 5);

(static) logicalXOR(a, b) → {number}

return the logical Exclusive OR of two numbers.

Parameters:
Name Type Description
a number

The first number

b number

The second number

(static) moduloPositive(value, size) → {number}

Calculates the positive modulo. Two numbers are congruent modulo a given number if they give the same remainder when divided by that number.

Parameters:
Name Type Description
value number

value for which to compute the modulo

size number

the modulo divisor

(static) nearestPowerOfTwo(value) → {number}

Gets the nearest higher power of two for a value

Parameters:
Name Type Description
value number

Number to get the nearest power of two from

(static) radFromDeg(degrees) → {number}

Converts an angle from degrees to radians.

Parameters:
Name Type Description
degrees number

Angle in degrees.

Example

Converts 70 degrees to a radian

var a = MathUtils.radFromDeg(70);

(static) radialClamp(value, min, max) → {number}

Clamps an angle to a given interval. The interval is defined by min and max. If min is larger than max, the clamp will wrap around.

Parameters:
Name Type Description
value number

Input value.

min number

Lower bound of interval (inclusive).

max number

Upper bound of interval (inclusive).

Example
var a = -1;
a = MathUtils.radialClamp(a, 0, 9); // a == 0

(static) randomInRange(min, max) → {number}

Random number within a range [min,max)

Parameters:
Name Type Description
min number

inclusive min

max number

exclusive max

(static) scurve3(x) → {number}

Computes a value on the c1-continuous cubic s-curve "y = -2x^3 + 3x^2".

Parameters:
Name Type Description
x number

Input value in the range between zero and one.

(static) scurve5(x) → {number}

Computes a value on the c2-continuous quintic s-curve "y = 6x^5 - 15x^4 + 10x^3".

Parameters:
Name Type Description
x number

Input value in the range between zero and one.

(static) sign(value) → {number}

Returns the sign of the supplied parameter

Parameters:
Name Type Description
value number

(static) smoothDamp(current, target, store, deltaTime, smoothTimeopt, maxSpeedopt) → {number}

Gradually changes a value towards a desired goal over time. The value is smoothed by some spring-damper like function, which will never overshoot. The function can be used to smooth any kind of value, positions, colors, scalars.

Parameters:
Name Type Attributes Default Description
current number

The current position.

target number

The position we are trying to reach.

store Vector2

An object to store the current position and velocity in.

deltaTime number

The time since the last call to this function.

smoothTime number <optional>
0.3

Approximately the time it will take to reach the target. A smaller value will reach the target faster.

maxSpeed number <optional>
1e7

Optionally allows you to clamp the maximum speed.

(static) smoothstep(edge0, edge1, x) → {number}

Performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1.
This is useful in cases where a threshold function with a smooth transition is desired.

Parameters:
Name Type Description
edge0 number

Specifies the value of the lower edge of the Hermite function.

edge1 number

Specifies the value of the upper edge of the Hermite function.

x number

Specifies the source value for interpolation.

(static) sphericalToCartesian(radius, azimuth, polar, store)

Converts Spherical coordinates in radians to a Vector3 Cartesian point (using positive Y as up) and stores the results in the store var.

Parameters:
Name Type Description
radius number

distance

azimuth number

heading

polar number

elevation

store Vector3

storage to return the computed Cartesian coordinates

Example
var distance = 5;
var heading = 180;
var elevation = 30;
var position = new Vector3();
MathUtils.sphericalToCartesian(distance, heading, elevation, position);

(static) triangleArea(t1, t2, t3) → {number}

Computes the area of a 2D triangle

Parameters:
Name Type Description
t1 Vector2

First point of the triangle

t2 Vector2

Second point of the triangle

t3 Vector2

Third point of the triangle

(static) warnNaN(object, property)

Defines a property on an object that throws an exception when NaN is being assigned to it.
Useful for debugging.

Parameters:
Name Type Description
object
property