MathUtils

An object with several math utility functions.

Functions

.angleToRange_0_2Pi(angle : Float) → Float

Converts the angular value to range [0, 2π).

v3d.MathUtils.angleToRange_0_2Pi(0.5 * Math.PI) // → 1.570796 = 1/2 π v3d.MathUtils.angleToRange_0_2Pi(-0.5 * Math.PI) // → 4.712388 = 3/2 π v3d.MathUtils.angleToRange_0_2Pi(2 * Math.PI) // → 0 v3d.MathUtils.angleToRange_0_2Pi(3 * Math.PI) // → 3.141592 = π v3d.MathUtils.angleToRange_0_2Pi(10 * Math.PI) // → 0

.angleToRange_mPi_Pi(angle : Float) → Float

Converts the angular value to range [-π, π).

.clamp(value : Float, min : Float, max : Float) → Float

Clamps the value to be between min and max.

.clampAngle(angle : Float, minAngle : Float, maxAngle : Float) → Float

Clamps the angular value to be between minAngle and maxAngle.

.degToRad(degrees : Float) → Float

Converts degrees to radians.

.euclideanModulo(n : Integer, m : Integer) → Integer

n, m — integers.

Computes the Euclidean modulo of m % n, that is:

((n % m) + m) % m

.expAverage(valueFrom : Float, valueTo : Float, sampleTime : Float, time : Float) → Float

Perform exponential smoothing of the float value.

.expAverageClamp(valueFrom : Float, valueTo : Float, sampleTime : Float, time : Float, clampDelta : Float) → Float

Perform exponential smoothing of the float value clamping small values less than clampDelta.

.generateUUID() → UUID

Generate a UUID (universally unique identifier).

.isPowerOfTwo(n : Integer) → Boolean

Return true if n is a power of 2.

.inverseLerp(x : Float, y : Float, value : Float) → Float

Returns the percentage in the closed interval [0, 1] of the given value between the start and end point.

.hashString(str : String) → Integer

Calculate 32-bit hash value for the given string.

.lerp(x : Float, y : Float, t : Float) → Float

Returns a value linearly interpolated from two known points based on the given interval: t = 0 will return x and t = 1 will return y.

.damp(x : Float, y : Float, lambda : Float, dt : Float) → Float

Smoothly interpolate a number from x toward y in a spring-like manner using the dt to maintain frame rate independent movement. For details, see here.

.mapLinear(x : Float, a1 : Float, a2 : Float, b1 : Float, b2 : Float) → Float

Linear mapping of x from range [a1, a2] to range [b1, b2].

.pingpong(x : Float, length : Float) → Float

Returns a value that alternates between 0 and length : Float.

.ceilPowerOfTwo(n : Float) → Integer

Returns the smallest power of 2 that is greater than or equal to n.

.floorPowerOfTwo(n : Float) → Integer

Returns the largest power of 2 that is less than or equal to n.

.radToDeg(radians : Float) → Float

Converts radians to degrees.

.randFloat(low : Float, high : Float) → Float

Returns random float in the interval [low, high].

.randFloatSpread(range : Float) → Float

Returns random float in the interval [- range / 2, range / 2].

.randInt(low : Integer, high : Integer) → Integer

Random integer in the interval [low, high].

.seededRandom(seed : Integer) → Float

Deterministic pseudo-random float in the interval [0, 1]. The integer seed is optional.

.smoothstep(x : Float, min : Float, max : Float) → Float

Returns a value between 0-1 that represents the percentage that x has moved between min and max, but smoothed or slowed down the closer X is to the min and max.

See Smoothstep for details.

.smootherstep(x : Float, min : Float, max : Float) → Float

Returns a value between 0-1. A variation on smoothstep that has zero 1st and 2nd order derivatives at x=0 and x=1.

.setQuaternionFromProperEuler(q : Quaternion, a : Float, b : Float, c : Float, order : String)

Sets quaternion q from the intrinsic Proper Euler Angles defined by angles a, b, and c, and order order.

Rotations are applied to the axes in the order specified by order: rotation by angle a is applied first, then by angle b, then by angle c. Angles are in radians.

Source

For more info on how to obtain the source code of this module see this page.