Math reference
Operators
Arithmetic
Modulo
The modulo operator (represented as %) is used to divide into a number and get the remainder.
Math functions
All of the functions in the javascript Math object are available. You don’t have to type Math.abs(-2), you can just type abs(-2). For example:
These functions will also work component-wise with vectors.
Trigonometry uses degrees
All the top-level trigonometry functions use degrees, not radians. For example:
If you’d like to use radians, you can use e.g. Math.sin instead of sin.
Additional functions
mix(a, b, t) blends between a and b as t goes from 0 to 1.
clamp(x, low, high) constrains x between low and high
angularDistance(a, b) will return the smallest absolute difference between angles a and b. It will be in the range 0 to 180.
modulo(x, base) is like the modulo % operator except if x is negative it will return a positive result.
moduloDistance(a, b, base) returns the smallest absolute difference between a and b in modulo base space. The result will be constrained to 0 <= difference <= base / 2. It’s a generalization of angularDistance.
Random
Although you can use Math.random() the result will jitter as it generates a new random value every time.
Instead, we recommend using random() which won’t jitter.
randomInt is like random but will only return integers
There are also functions for generating random vectors.
Seeded random generator
If you need to generate the same sequence of random numbers, generated by a seed, you can do so by creating a new RandomGenerator(seed).
The random generator has as methods all of the above random functions.