nupp.math.quat

Three-dimensional rotation operations over (x, y, z, w) number quadruples.

Module contents

Functions

FunctionKindDescription
addfunctionAdds two rotations componentwise.
basisfunctionReturns a unit rotation's three-by-three matrix in column-major order.
conjugatefunctionReturns the rotation undoing a unit one.
dotfunctionReturns the four-component dot product, the cosine of half the angle between two unit rotations.
fromAxisAnglefunctionBuilds a rotation of an angle about an axis.
fromTofunctionReturns the shortest rotation carrying one direction onto another.
identityfunctionReturns the rotation that turns nothing.
inversefunctionReturns the rotation undoing any non-zero one, or the identity for zero.
lengthfunctionReturns the length, which is one for a unit rotation.
lengthSquaredfunctionReturns the squared length, which is one for a unit rotation.
multiplyfunctionComposes two rotations, applying the second one first.
nlerpfunctionInterpolates between two unit rotations the short way around, blending components rather than the arc.
normalizefunctionReturns a unit rotation in the same direction, or the identity for zero.
rotatefunctionTurns a vector by a unit rotation.
scalefunctionMultiplies every component by a factor.
slerpfunctionInterpolates between two unit rotations the short way around at a constant angular speed.
subfunctionSubtracts the second rotation from the first componentwise.
toAxisAnglefunctionRecovers the axis and angle of a unit rotation.
toMatrixfunctionWrites a unit rotation as a four-by-four column-major matrix without translation.

Functions#

addfunction#

local add: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number
): (number, number, number, number)

Adds two rotations componentwise.

Arguments

NameTypeDescription
axnumber

the first rotation's x component

aynumber

the first rotation's y component

aznumber

the first rotation's z component

awnumber

the first rotation's w component

bxnumber

the second rotation's x component

bynumber

the second rotation's y component

bznumber

the second rotation's z component

bwnumber

the second rotation's w component

Returns

TypeDescription
number

the sum's x component

number

the sum's y component

number

the sum's z component

number

the sum's w component

basisfunction#

local basis: function(
    x: number,
    y: number,
    z: number,
    w: number
): (number, number, number, number, number, number, number, number, number)

Returns a unit rotation's three-by-three matrix in column-major order.

Answered rather than written, for the caller folding a rotation into something larger. toMatrix is the form for storage.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the first column's x component

number

the first column's y component

number

the first column's z component

number

the second column's x component

number

the second column's y component

number

the second column's z component

number

the third column's x component

number

the third column's y component

number

the third column's z component

conjugatefunction#

local conjugate: function(x: number, y: number, z: number, w: number): (number, number, number, number)

Returns the rotation undoing a unit one.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the conjugate's x component

number

the conjugate's y component

number

the conjugate's z component

number

the conjugate's w component

dotfunction#

local dot: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number
): number

Returns the four-component dot product, the cosine of half the angle between two unit rotations.

Arguments

NameTypeDescription
axnumber

the first rotation's x component

aynumber

the first rotation's y component

aznumber

the first rotation's z component

awnumber

the first rotation's w component

bxnumber

the second rotation's x component

bynumber

the second rotation's y component

bznumber

the second rotation's z component

bwnumber

the second rotation's w component

Returns

TypeDescription
number

the dot product

fromAxisAnglefunction#

local fromAxisAngle: function(x: number, y: number, z: number, radians: number): (number, number, number, number)

Builds a rotation of an angle about an axis.

The axis is normalized here, so it need not arrive as a unit vector.

Arguments

NameTypeDescription
xnumber

the axis's x component

ynumber

the axis's y component

znumber

the axis's z component

radiansnumber

the turn about the axis, counter-clockwise looking down it

Returns

TypeDescription
number

the rotation's x component, or zero for a zero axis

number

the rotation's y component, or zero for a zero axis

number

the rotation's z component, or zero for a zero axis

number

the rotation's w component, or one for a zero axis

fromTofunction#

local fromTo: function(
    ax: number,
    ay: number,
    az: number,
    bx: number,
    by: number,
    bz: number
): (number, number, number, number)

Returns the shortest rotation carrying one direction onto another.

Neither direction has to be a unit vector. Opposed directions have no shortest rotation, so one half turn across them is chosen.

Arguments

NameTypeDescription
axnumber

the starting direction's x component

aynumber

the starting direction's y component

aznumber

the starting direction's z component

bxnumber

the target direction's x component

bynumber

the target direction's y component

bznumber

the target direction's z component

Returns

TypeDescription
number

the rotation's x component, or zero for a zero direction

number

the rotation's y component, or zero for a zero direction

number

the rotation's z component, or zero for a zero direction

number

the rotation's w component, or one for a zero direction

identityfunction#

local identity: function(): (number, number, number, number)

Returns the rotation that turns nothing.

Returns

TypeDescription
number

the identity x component, zero

number

the identity y component, zero

number

the identity z component, zero

number

the identity w component, one

inversefunction#

local inverse: function(x: number, y: number, z: number, w: number): (number, number, number, number)

Returns the rotation undoing any non-zero one, or the identity for zero.

conjugate is this for a unit rotation and is cheaper; this one is also correct for a rotation whose length has drifted from one.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the inverse's x component, or zero for a zero rotation

number

the inverse's y component, or zero for a zero rotation

number

the inverse's z component, or zero for a zero rotation

number

the inverse's w component, or one for a zero rotation

lengthfunction#

local length: function(x: number, y: number, z: number, w: number): number

Returns the length, which is one for a unit rotation.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the length

lengthSquaredfunction#

local lengthSquared: function(x: number, y: number, z: number, w: number): number

Returns the squared length, which is one for a unit rotation.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the squared length

multiplyfunction#

local multiply: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number
): (number, number, number, number)

Composes two rotations, applying the second one first.

The order matches column-major matrix multiplication, so the matrix of multiply(a, b) is the matrix of a times the matrix of b.

Arguments

NameTypeDescription
axnumber

the second-applied rotation's x component

aynumber

the second-applied rotation's y component

aznumber

the second-applied rotation's z component

awnumber

the second-applied rotation's w component

bxnumber

the first-applied rotation's x component

bynumber

the first-applied rotation's y component

bznumber

the first-applied rotation's z component

bwnumber

the first-applied rotation's w component

Returns

TypeDescription
number

the composition's x component

number

the composition's y component

number

the composition's z component

number

the composition's w component

nlerpfunction#

local nlerp: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number,
    t: number
): (number, number, number, number)

Interpolates between two unit rotations the short way around, blending components rather than the arc.

Cheaper than slerp and not constant in angular speed. If the short route ends at -b, that endpoint form is kept through t == 1 so the four components stay continuous.

Arguments

NameTypeDescription
axnumber

the starting rotation's x component

aynumber

the starting rotation's y component

aznumber

the starting rotation's z component

awnumber

the starting rotation's w component

bxnumber

the ending rotation's x component

bynumber

the ending rotation's y component

bznumber

the ending rotation's z component

bwnumber

the ending rotation's w component

tnumber

the interpolation factor

Returns

TypeDescription
number

the interpolated x component

number

the interpolated y component

number

the interpolated z component

number

the interpolated w component

normalizefunction#

local normalize: function(x: number, y: number, z: number, w: number): (number, number, number, number)

Returns a unit rotation in the same direction, or the identity for zero.

The sign is left alone: q and -q are the same rotation, and which one arrived is not canonicalized away.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the normalized x component, or zero for a zero rotation

number

the normalized y component, or zero for a zero rotation

number

the normalized z component, or zero for a zero rotation

number

the normalized w component, or one for a zero rotation

rotatefunction#

local rotate: function(
    qx: number,
    qy: number,
    qz: number,
    qw: number,
    x: number,
    y: number,
    z: number
): (number, number, number)

Turns a vector by a unit rotation.

Cheaper than building a matrix for a handful of vectors and dearer for many, where basis amortizes its nine products across every vector after the first.

Arguments

NameTypeDescription
qxnumber

the rotation's x component

qynumber

the rotation's y component

qznumber

the rotation's z component

qwnumber

the rotation's w component

xnumber

the vector's x component

ynumber

the vector's y component

znumber

the vector's z component

Returns

TypeDescription
number

the turned x component

number

the turned y component

number

the turned z component

scalefunction#

local scale: function(x: number, y: number, z: number, w: number, factor: number): (number, number, number, number)

Multiplies every component by a factor.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

factornumber

the scaling factor

Returns

TypeDescription
number

the scaled x component

number

the scaled y component

number

the scaled z component

number

the scaled w component

slerpfunction#

local slerp: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number,
    t: number
): (number, number, number, number)

Interpolates between two unit rotations the short way around at a constant angular speed.

Nearly parallel endpoints fall back to nlerp, whose answer they agree on where the arc the spherical form divides by has vanished. If the short route ends at -b, that endpoint form is kept through t == 1 so the four components stay continuous.

Arguments

NameTypeDescription
axnumber

the starting rotation's x component

aynumber

the starting rotation's y component

aznumber

the starting rotation's z component

awnumber

the starting rotation's w component

bxnumber

the ending rotation's x component

bynumber

the ending rotation's y component

bznumber

the ending rotation's z component

bwnumber

the ending rotation's w component

tnumber

the interpolation factor

Returns

TypeDescription
number

the interpolated x component

number

the interpolated y component

number

the interpolated z component

number

the interpolated w component

subfunction#

local sub: function(
    ax: number,
    ay: number,
    az: number,
    aw: number,
    bx: number,
    by: number,
    bz: number,
    bw: number
): (number, number, number, number)

Subtracts the second rotation from the first componentwise.

Arguments

NameTypeDescription
axnumber

the first rotation's x component

aynumber

the first rotation's y component

aznumber

the first rotation's z component

awnumber

the first rotation's w component

bxnumber

the second rotation's x component

bynumber

the second rotation's y component

bznumber

the second rotation's z component

bwnumber

the second rotation's w component

Returns

TypeDescription
number

the difference's x component

number

the difference's y component

number

the difference's z component

number

the difference's w component

toAxisAnglefunction#

local toAxisAngle: function(x: number, y: number, z: number, w: number): (number, number, number, number)

Recovers the axis and angle of a unit rotation.

The angle lands in [0, 2pi] rather than being folded into [0, pi], so q and -q answer the two opposite ways around the same rotation.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

Returns

TypeDescription
number

the axis's x component, or one for the identity

number

the axis's y component, or zero for the identity

number

the axis's z component, or zero for the identity

number

the turn about the axis in radians, zero for positive identity or 2pi for negative identity

toMatrixfunction#

local toMatrix: function(x: number, y: number, z: number, w: number, out: {number}, offset: integer): nil

Writes a unit rotation as a four-by-four column-major matrix without translation.

The sixteen numbers start at offset, which indexes the first of them. The layout is the one a GPU expects, so an uploaded destination is written in place rather than repacked.

Arguments

NameTypeDescription
xnumber

the rotation's x component

ynumber

the rotation's y component

znumber

the rotation's z component

wnumber

the rotation's w component

out{number}

the destination the sixteen numbers are written to

offsetinteger

the index the first number is written at

Returns

TypeDescription
nil

The operation returns no value.