pex-math

js-standard-style

Array-based vector, quaternion and matrix math for PEX

Usage

// require any of the modules
var mat4 = require('pex-math/mat4')

// all of them are namespaces of functions
var identityMatrix = mat4.create()
// => [1, 0, 0, 0,
//     0, 1, 0, 0,
//     0, 0, 1, 0,
//     0, 0, 0, 1]

API

vec2

var vec2 = require('pex-math/vec2')

vec2.create()

Returns a new vec2 at 0, 0, 0.

vec2.set(v, w)

Sets a vector to another vector.

Returns v.

vec2.copy(v)

Returns a copy of a vector.

Returns a new instance of v.

vec2.equals(v, w)

Compares two vectors.

Returns a bool.

vec2.add(v, w)

Add a vector with another.

Returns v after addition.

vec2.sub(v, w)

Subtracts a vector with another.

Returns v after subtraction.

vec2.scale(v, s)

Scales a vector by a number.

Returns v scaled.

vec2.dot(v, w)

Calculates the dot product of two vectors.

Returns v after calculation.

vec2.length(v)

Calculates the length of a vector.

Returns the length of v.

vec2.lengthSq(v)

Calculates the squared length of a vector.

Returns the squared length of v.

vec2.normalize(v)

Normalises a vector.

Returns v normalised.

vec2.distance(v, w)

Calculates the distance between two vectors.

Returns the distance between v and w.

vec2.distanceSq(v, w)

Calculates the squared distance between two vectors.

Returns the squared distance between v and w.

vec2.limit(v, s)

Limits a vector to a length.

Returns v scaled.

vec2.lerp(v, w, t)

Linearly interpolates between two vectors.

Returns v lerped.

vec2.toString(v, precision)

Prints a vector to a string.

Returns a String.

vec3

var vec3 = require('pex-math/vec3')

vec3.create()

Returns a new vec3 at 0, 0, 0.

vec3.set(v, w)

Sets a vector to another vector.

Returns v.

vec3.copy(v)

Returns a copy of a vector.

Returns a new instance of v.

vec3.equals(v, w)

Compares two vectors.

Returns a bool.

vec3.add(v, w)

Add a vector with another.

Returns v after addition.

vec3.addScaled(v, w, s)

Add a vector with another scaled vector.

Returns v after addition.

vec3.sub(v, w)

Subtracts a vector with another.

Returns v after subtraction.

vec3.scale(v, s)

Scales a vector by a number.

Returns v scaled.

vec3.multMat4(v, m)

Multiplies a vector by a matrix.

Returns v after multiplication.

vec3.multQuat(v, q)

Multiplies a vector by a quaternion.

Returns v after multiplication.

vec3.dot(v, w)

Calculates the dot product of two vectors.

Returns v after calculation.

vec3.cross(v, w)

Calculates the cross product of two vectors.

Returns v after calculation.

vec3.length(v)

Calculates the length of a vector.

Returns the length of v.

vec3.lengthSq(v)

Calculates the squared length of a vector.

Returns the squared length of v.

vec3.normalize(v)

Normalises a vector.

Returns v normalised.

vec3.distance(v, w)

Calculates the distance between two vectors.

Returns the distance between v and w.

vec3.distanceSq(v, w)

Calculates the squared distance between two vectors.

Returns the squared distance between v and w.

vec3.limit(v, s)

Limits a vector to a length.

Returns v scaled.

vec3.lerp(v, w, t)

Linearly interpolates between two vectors.

Returns v lerped.

vec3.toString(v, p)

Prints a vector to a string.

Returns a String.

vec4

var vec4 = require('pex-math/vec4')

vec4.create()

Returns a new vec4 at 0, 0, 0.

vec4.equals(v, w)

Compares two vectors.

Returns a bool.

vec4.set(v, w)

Sets a vector to another vector.

Returns v.

vec4.fromVec3(v, v3)

Create a vec4 from vec3.

Returns v4.

vec4.multMat4(v, m)

Multiplies a vector with a matrix.

Returns v.

vec4.copy(v)

Returns a copy of a vector.

Returns a new instance of v.

vec4.scale(v, s)

Scales a vector by a number.

Returns v scaled.

mat2x3

var mat2x3 = require('pex-math/mat2x3')

mat2x3.create()

Returns a 2x3 identity matrix, a short form for a 3x3 matrix with the last row ignored.

Row major memory layout:

 0   1   2
 3   4   5

Equivalent to the column major OpenGL spec:

  0   3
  1   4
  2   5

 m00 m10
 m01 m11
 m02 m12

Returns a new mat2x3.

mat2x3.set(m, n)

Sets a matrix from another matrix.

Returns m.

mat2x3.identity(m)

Sets a matrix to the identity matrix.

Returns m as its identity.

mat2x3.mult(m, n)

Multiplies two matrices.

Returns m multiplied by n.

mat2x3.translate(m, v)

Translates a matrix by a vector.

Returns m translated.

mat2x3.rotate(m, r)

Rotates a matrix by an angle.

Returns m rotated.

mat2x3.scale(m, v)

Scales a matrix by a vector.

Returns m scaled.

mat3

var mat3 = require('pex-math/mat3')

mat3.create()

Returns a 3x3 identity matrix.

Row major memory layout:

 0   1   2
 3   4   5
 6   7   8

Equivalent to the column major OpenGL spec:

  0   3   6
  1   4   7
  2   5   8

 m00 m10 m20
 m01 m11 m21
 m02 m12 m22

Returns a new mat3.

mat3.set(m, n)

Sets a matrix from another matrix.

Returns m.

mat3.identity(m)

Sets a matrix to the identity matrix.

Returns m as its identity.

mat3.mult(m, n)

Multiplies two matrices.

Returns m multiplied by n.

mat3.equals(m, n)

Compares two matrices.

Returns a bool.

mat3.fromMat2x3(m, m2x3)

Sets a 3x3 matrix from a 2x3 matrix.

Returns m3.

mat3.fromMat4(m, m4)

Sets a 3x3 matrix to a 4x4 matrix.

Returns m3.

mat3.fromQuat(m, q)

Sets matrix to a quaternion.

Returns m.

mat4

var mat4 = require('pex-math/mat4')

mat4.create()

Returns a 4x4 identity matrix.

Row major memory layout:

 0   1   2   3
 4   5   6   7
 8   9  10  11
12  13  14  15

Equivalent to the column major OpenGL spec:

 0   4   8  12
 1   5   9  13
 2   6  10  14
 3   7  11  15

 m00 m10 m20 m30
 m01 m11 m21 m31
 m02 m12 m22 m32
 m03 m13 m23 m33

Returns a new mat4.

mat4.set(m, n)

Sets a matrix from another matrix.

Returns the newly set a.

mat4.equals(m, n)

Compares two matrices.

Returns a bool.

mat4.copy(m)

Returns a copy of a matrix.

Returns a new instance of m.

mat4.mult(m, n)

Multiplies two matrices.

Returns m multiplied by n.

mat4.invert(m)

Inverts a matrix.

Returns m inverted.

mat4.tranpose(m)

Transposes a matrix.

Returns m transposed.

mat4.identity(m)

Sets a matrix to the identity matrix.

Returns m as its identity.

mat4.scale(m, v)

Scales a matrix by a vector.

Returns m scaled.

mat4.translate(m, v)

Translates a matrix by a vector.

Returns m translated.

mat4.rotate(m, r, v)

Rotates a matrix by an angle at an axis.

Returns m rotated.

mat4.frustum(m, left, right, bottom, top, near, far)

Create a frustum matrix.

Returns m.

mat4.perspective(m, fovy, aspectRatio, near, far)

Create a perspective matrix.

Returns m.

mat4.ortho(m, left, right, bottom, top, near, far)

Create a orthographic matrix.

Returns m.

mat4.lookAt(m, from, to, up)

Calculates a lookAt matrix from a position, target and up vectors.

Returns m.

mat4.fromQuat(m, q)

Sets matrix to a quaternion.

Returns m.

mat4.fromMat3(m, m3)

Sets a 4x4 matrix to a 3x3 matrix.

Returns m.

mat4.fromTranslationRotationScale(m, t, r, s)

Sets matrix to the TRS matrix.

Returns m.

quat

var quat = require('pex-math/quat')

quat.create()

Create a new quaternion.

quat.equals(q, p)

Compares two quaternions.

Returns bool.

quat.identity(q)

Sets a quaternion to the identity quaternion.

Returns q.

quat.copy(q)

Returns a copy of a quaternion.

Returns a new instance of q.

quat.set(q, p)

Sets one quaternion to another.

Returns q.

quat.mult(q, p)

Multiplies one quaternion by another.

Returns q after multiplication.

quat.invert(q)

Inverts a quaternion.

Returns q inverted.

quat.conjugate(q)

Conjugates a quaternion.

Returns q conjugates.

quat.length(q)

Calculates the length of a quaternion.

Returns the length of q.

quat.normalize(q)

Normalizes a quaternion.

Returns q normalized.

quat.dot(q, p)

Calculates the dot product of two quaternions.

Returns the dot product of q and p.

quat.fromEuler(q, euler)

Set euler angles to a quaternion. Assumes XYZ rotation order.

Returns q.

quat.fromAxisAngle(q, axis, angle)

Set the angle at an axis of a quaternion.

Returns q.

quat.fromAxes(q, xAxis, yAxis, zAxis)

Sets quaternion from orthonormal base xyz.

Returns q.

quat.fromMat3(q, m)

Sets a quaternion to a 3x3 matrix.

Returns q.

quat.fromMat4(q, m)

Sets a quaternion to a 4x4 matrix.

Returns q.

quat.slerp(q, p, t)

Spherical linear interpolation between two quaternions.

Returns q.

quat.fromTo(q, from, to, up)

TODO

Returns q.

euler

vec3 array of [x, y, z] rotation [yaw, pitch, roll] in radians.

var euler = require('pex-math/euler')

euler.create()

Create a new euler angles [0, 0, 0].

euler.fromQuat(e, q)

Creates euler angles from quaterion. Assumes XYZ order of rotations.

Returns e.

utils

utils.lerp(a, b, t)

Linear interpolation between two numbers.

Returns a Number.

utils.clamp(a, min, max)

Clamps a number between two numbers.

Returns a Number.

utils.smoothstep(a, min, max)

Returns a Number.

utils.map(a, inStart, inEnd, outStart, outEnd)

Maps a number from one range to another.

Returns a Number.

toRadians(degrees)

Transforms degrees into radians.

Returns a Number.

toDegrees(radians)

Transforms radians into degrees.

Returns a Number.

fraction(a)

Returns the fractional part of a number.

Returns a Number.

sign(a)

Returns the sign of a number.

Returns a Number.

sign(a)

Returns the sign of a number.

Returns a Number.

isPowerOfTwo(a)

Returns the sign of a number.

Returns a Number.

nextPowerOfTwo(a)

Returns the sign of a number.

Returns a Number.