Skip to content
Quaternion

[Basic Type](../groups/Basic Type.Basic Type.md) / Quaternion

Quaternion Class ​

Quaternion


A unit quaternion used to represent 3D rotation.

Quaternions, similar to Transform, achieve a matrix representation of rotation. Unlike Transform, which stores rotations, scale, and cut, quaternions store only rotations.

Quaternions can be parameterized using axis angle pairs or Euler angles. Due to their compactness and the way they are stored in memory, some operations are more efficient for handling floating-point errors.

Table of contents ​

Properties ​

w: number
w weight.
x: number
x weight.
y: number
y weight.
z: number
z weight.
halfToRad: any
Obtain a half radian coefficient

Accessors ​

conjugated(): Quaternion
Return the conjugate quaternion of the current quaternion, which is equivalent to finding the inverse of the unit quaternion but more efficient
inverted(): Quaternion
Return the inverse of the current quaternion
length(): number
Find the length of quaternions
normalized(): Quaternion
Return a new quaternion after normalizing the current quaternion
sqrLength(): number
Find the square of quaternion length
identity(): Quaternion
Get a unit quaternion

Methods ​

clone(): Quaternion other
Clone the current quaternion.
equals(a: Quaternion, b: Quaternion, epsilon?: number): boolean other
Approximate equivalence judgment of quaternions to exclude floating-point errors
fromRotation(v: Rotation, outer?: Quaternion): Quaternion other
Read rotation angle data and create or write a quaternion
getAxisX(outer?: Vector): Vector other
Returns the coordinate system X axis vector that defines this quaternion
getAxisY(outer?: Vector): Vector other
Returns the Y axis vector of the coordinate system that defines this quaternion
getAxisZ(outer?: Vector): Vector other
Return the Z-axis vector of the coordinate system that defines this quaternion
getEuler(outerZ?: boolean, outer?: Vector): Vector other
Calculate Euler angles based on quaternions
getEulerAngles(): Vector other
Return the Euler angle (x-y-z) converted from the current quaternion.
normalize(a: Quaternion, outer?: Quaternion): Quaternion other
Normalized Quaternion
set(other): Quaternion other
Set the current quaternion to be equal to the specified quaternion.
setEulerAngles(euler: Vector): void other
Set Euler angle
strictEquals(a: Quaternion, b: Quaternion): boolean other
Quaternion equivalence judgment
toRotation(): Rotation other
Get the rotation data of a current quaternion
toString(): string other
Output as a string
calculateW(a: Quaternion, outer?: Quaternion): Quaternion other
Calculate the w component based on the xyz component, which is normalized by default
conjugate(a: Quaternion, outer?: Quaternion): Quaternion other
Finding conjugate quaternions is equivalent to finding the inverse of unit quaternions, but more efficient
dot(a: Quaternion, b: Quaternion): number other
Quaternion point product (quantity product)
equals(a: Quaternion, b: Quaternion, epsilon?: number): boolean other
Approximate equivalence judgment of quaternions to exclude floating-point errors
fromAngleZ(z: number, outer?: Quaternion): Quaternion other
Calculate quaternions based on 2D angles (0,0,z)
fromAxes(xAxis: Vector, yAxis: Vector, zAxis: Vector, outer?: Quaternion): Quaternion other
Calculate the quaternion according to the orientation of the coordinate axis. By default, the three vectors are normalized and vertical to each other
fromAxisAngle(axis: Vector, rad: number, outer?: Quaternion): Quaternion other
Calculate quaternion based on rotation axis and rotation radian
fromMatrix3x3(m: Matrix3x3, outer?: Quaternion): Quaternion other
Calculate quaternions based on three-dimensional matrix information, assuming the input matrix does not contain scaling information
fromRotation(v: Rotation, outer?: Quaternion): Quaternion other
Read rotation angle data and create or write a quaternion
fromViewUp(view: Vector, up?: Vector, outer?: Quaternion): Quaternion other
Calculates quaternions based on the front and up directions of the viewport
getAxis(q: Quaternion, outer?: Vector): Vector other
Obtain the rotation axis of quaternions
getAxisAngle(q: Quaternion): number other
Obtain the rotation radius of quaternions
invert(a: Quaternion, outer?: Quaternion): Quaternion other
Quaternion inversion
lerp(a: Quaternion, b: Quaternion, t: number, outer?: Quaternion): Quaternion other
Element wise linear interpolation: A+t * (B - A)
multiply(a: Quaternion, b: Quaternion, outer?: Quaternion): Quaternion other
Quaternion multiplication
multiplyVector(a: Vector, q: Quaternion, outer?: Vector): Vector other
Multiplying a quaternion by a three-dimensional vector (usually used to rotate a vector through quaternions)
normalize(a: Quaternion, outer?: Quaternion): Quaternion other
Normalized Quaternion
rotateAround(rot: Quaternion, axis: Vector, rad: number, outer?: Quaternion): Quaternion other
Rotate quaternion around a specified axis in world space
rotateAroundLocal(rot: Quaternion, axis: Vector, rad: number, outer?: Quaternion): Quaternion other
Rotate a quaternion around a specified axis in local space
rotateX(a: Quaternion, rad: number, outer?: Quaternion): Quaternion other
Rotate around the X-axis to specify quaternions
rotateY(a: Quaternion, rad: number, outer?: Quaternion): Quaternion other
Rotate the specified quaternion around the Y axis
rotateZ(a: Quaternion, rad: number, outer?: Quaternion): Quaternion other
Rotate the specified quaternion about the Z axis
rotationTo(a: Vector, b: Vector, outer?: Quaternion): Quaternion other
Set the quaternion as the shortest path rotation between two vectors a and b, with both vectors normalized by default
slerp(a: Quaternion, b: Quaternion, t: number, outer?: Quaternion): Quaternion other
Quaternion spherical interpolation
sqlerp(a: Quaternion, b: Quaternion, c: Quaternion, d: Quaternion, t: number, outer?: Quaternion): Quaternion other
Quaternion spherical interpolation with two control points
strictEquals(a: Quaternion, b: Quaternion): boolean other
Quaternion equivalence judgment
toMatrix3x3(q: Quaternion, outer?: Matrix3x3): Matrix3x3 other
Calculate matrix based on quaternion rotation information

• new Quaternion(v)

Parameters ​

v QuaternionUsage: Four Elements

• new Quaternion(x, y, z, w)

Parameters ​

x numberx Component default: 0 range: [-1,1] type: Floating point number
y numbery Component default: 0 range: [-1,1] type: Floating point number
z numberz Component default: 0 range: [-1,1] type: Floating point number
w numberw Component default: 1 range: [-1,1] type: Floating point number

Properties ​

w ​

• w: number

w weight.


x ​

• x: number

x weight.


y ​

• y: number

y weight.


z ​

• z: number

z weight.


halfToRad ​

▪ Static Private halfToRad: any

Obtain a half radian coefficient

Accessors ​

conjugated ​

• get conjugated(): Quaternion

Return the conjugate quaternion of the current quaternion, which is equivalent to finding the inverse of the unit quaternion but more efficient

Returns ​

Quaternion

inverted ​

• get inverted(): Quaternion

Return the inverse of the current quaternion

Returns ​

Quaternion

length ​

• get length(): number

Find the length of quaternions

Returns ​

number

normalized ​

• get normalized(): Quaternion

Return a new quaternion after normalizing the current quaternion

Returns ​

Quaternion

sqrLength ​

• get sqrLength(): number

Find the square of quaternion length

Returns ​

number

identity ​

• Static get identity(): Quaternion

Get a unit quaternion

Precautions

Quaternion(0,0,0,1)。

Returns ​

Quaternion

Methods ​

clone ​

• clone(): Quaternion other

Clone the current quaternion.

Returns ​

QuaternionQuaternion

equals ​

• equals(other, epsilon?): boolean other

Determine whether the current quaternion is equal to the specified vector within the error range.

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
epsilon? numberUsage: Minimum error
default: 1. e-7
range: It is recommended to pass in a value less than 1. Type: Floating point number

Returns ​

booleanReturn the result of whether it is equal or not

fromRotation ​

• fromRotation(v): void other

Read a rotation data from the current quaternion

Parameters ​

v RotationUsage: read string data
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

getAxisX ​

• getAxisX(outer?): Vector other

Returns the coordinate system X axis vector that defines this quaternion

Parameters ​

outer? VectorUsage: Vector object for receiving results default: null

Returns ​

VectorThe X-axis vector of the coordinate system of this quaternion

getAxisY ​

• getAxisY(outer?): Vector other

Returns the Y axis vector of the coordinate system that defines this quaternion

Parameters ​

outer? VectorUsage: Vector object for receiving results default: null

Returns ​

VectorThe Y-axis vector of the coordinate system of this quaternion

getAxisZ ​

• getAxisZ(outer?): Vector other

Return the Z-axis vector of the coordinate system that defines this quaternion

Parameters ​

outer? VectorUsage: Vector object for receiving results default: null

Returns ​

VectorThe coordinate system Z axis vector of this quaternion

getEuler ​

• getEuler(outerZ?, outer?): Vector other

Calculate Euler angles based on quaternions

Parameters ​

outerZ? booleanUsage: Change the range of z values to [-180, -90] U [90180] default: false
outer? VectorUsage: Vector object for receiving results default: null

Returns ​

VectorEuler's angle. The return angle x, y is within the range of [-180, 180], z default is within the range of [-90, 90], and the rotation order is YZX

getEulerAngles ​

• getEulerAngles(): Vector other

Return the Euler angle (x-y-z) converted from the current quaternion.

Returns ​

VectorEuler angle

normalize ​

• normalize(): void other

Normalize the current quaternion

Parameters ​

a QuaternionUsage: Quaternion a
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

set ​

• set(other): Quaternion other

Set the current quaternion to be equal to the specified quaternion.

Parameters ​

other QuaternionUsage: Quaternion data

Returns ​

Quaternionthis

• set(x?, y?, z?, w?): Quaternion other

Set the current quaternion to specify the element value.

Parameters ​

x? numberUsage: x-component of quaternion default: 0 range: [-1,1] type: floating-point number
y? numberUsage: y component of quaternion default: 0 range: [-1,1] type: floating-point number
z? numberUsage: z component of quaternion default: 0 range: [-1,1] type: floating-point number
w? numberUsage: w component of quaternion default: 0 range: [-1,1] type: floating-point number

Returns ​

QuaternionCurrent quaternion

setEulerAngles ​

• setEulerAngles(euler): void other

Set Euler angle

Parameters ​

euler VectorUsage: Incoming Euler angles

strictEquals ​

• strictEquals(other): boolean other

Determine whether the current quaternion is equal to the specified quaternion.

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b

Returns ​

booleanReturn the result of whether it is equal or not

toRotation ​

• toRotation(): Rotation other

Get the rotation data of a current quaternion

Returns ​

RotationRotating data

toString ​

• toString(): string other

Output as a string

Returns ​

stringQuaternion numerical string

calculateW ​

• Static calculateW(a, outer?): Quaternion other

Calculate the w component based on the xyz component, which is normalized by default

Parameters ​

a QuaternionUsage: Target Quaternion
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

conjugate ​

• Static conjugate(a, outer?): Quaternion other

Finding conjugate quaternions is equivalent to finding the inverse of unit quaternions, but more efficient

Parameters ​

a QuaternionUsage: Quaternion a
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

dot ​

• Static dot(a, b): number other

Quaternion point product (quantity product)

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b

Returns ​

numberReturn a result count

equals ​

• Static equals(a, b, epsilon?): boolean other

Approximate equivalence judgment of quaternions to exclude floating-point errors

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
epsilon? numberUsage: Minimum error
default: 1. e-7
range: It is recommended to pass in a value less than 1. Type: Floating point number

Returns ​

booleanReturn the result of whether it is equal or not

fromAngleZ ​

• Static fromAngleZ(z, outer?): Quaternion other

Calculate quaternions based on 2D angles (0,0,z)

Parameters ​

z numberUsage: The angle value of rotation around z< Range: unlimited type: floating point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

fromAxes ​

• Static fromAxes(xAxis, yAxis, zAxis, outer?): Quaternion other

Calculate the quaternion according to the orientation of the coordinate axis. By default, the three vectors are normalized and vertical to each other

Parameters ​

xAxis VectorUsage: vector value of x axis
yAxis VectorUsage: vector value of y axis
zAxis VectorUsage: vector value of z axis
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

fromAxisAngle ​

• Static fromAxisAngle(axis, rad, outer?): Quaternion other

Calculate quaternion based on rotation axis and rotation radian

Parameters ​

axis VectorUsage: rotate axis
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

fromMatrix3x3 ​

• Static fromMatrix3x3(m, outer?): Quaternion other

Calculate quaternions based on three-dimensional matrix information, assuming the input matrix does not contain scaling information

Parameters ​

m Matrix3x3Usage: 3D matrix
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

fromRotation ​

• Static fromRotation(v, outer?): Quaternion other

Read rotation angle data and create or write a quaternion

Parameters ​

v RotationUsage: read string data
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

fromViewUp ​

• Static fromViewUp(view, up?, outer?): Quaternion other

Calculates quaternions based on the front and up directions of the viewport

Parameters ​

view VectorUsage: front vector of viewport
up? VectorUsage: upper vector default: null
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

getAxis ​

• Static getAxis(q, outer?): Vector other

Obtain the rotation axis of quaternions

Parameters ​

q QuaternionUsage: Target Quaternion
outer? VectorUsage: Vector object written default: null

Returns ​

VectorReturn a three-dimensional vector

getAxisAngle ​

• Static getAxisAngle(q): number other

Obtain the rotation radius of quaternions

Parameters ​

q QuaternionUsage: Target Quaternion

Returns ​

numberReturn a radian value

invert ​

• Static invert(a, outer?): Quaternion other

Quaternion inversion

Parameters ​

a QuaternionUsage: Quaternion a
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

lerp ​

• Static lerp(a, b, t, outer?): Quaternion other

Element wise linear interpolation: A+t * (B - A)

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
t numberUsage: Interpolation
range: [0,1] type: Floating point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

multiply ​

• Static multiply(a, b, outer?): Quaternion other

Quaternion multiplication

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

multiplyVector ​

• Static multiplyVector(a, q, outer?): Vector other

Multiplying a quaternion by a three-dimensional vector (usually used to rotate a vector through quaternions)

Parameters ​

a VectorUsage: target vector
q QuaternionUsage: Quaternion
outer? VectorUsage: Vector object written default: null

Returns ​

VectorReturn a rotated 3D vector

normalize ​

• Static normalize(a, outer?): Quaternion other

Normalized Quaternion

Parameters ​

a QuaternionUsage: Quaternion a
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotateAround ​

• Static rotateAround(rot, axis, rad, outer?): Quaternion other

Rotate quaternion around a specified axis in world space

Parameters ​

rot QuaternionUsage: rotated quaternions
axis VectorUsage: 3D vector representing the axis of rotation
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotateAroundLocal ​

• Static rotateAroundLocal(rot, axis, rad, outer?): Quaternion other

Rotate a quaternion around a specified axis in local space

Parameters ​

rot QuaternionUsage: Quaternions that need to be transformed
axis VectorUsage: 3D vector representing the axis of rotation
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotateX ​

• Static rotateX(a, rad, outer?): Quaternion other

Rotate around the X-axis to specify quaternions

Parameters ​

a QuaternionUsage: Quaternion
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotateY ​

• Static rotateY(a, rad, outer?): Quaternion other

Rotate the specified quaternion around the Y axis

Parameters ​

a QuaternionUsage: Quaternion
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotateZ ​

• Static rotateZ(a, rad, outer?): Quaternion other

Rotate the specified quaternion about the Z axis

Parameters ​

a QuaternionUsage: Quaternion
rad numberUsage: radian value
range: unlimited type: floating-point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

rotationTo ​

• Static rotationTo(a, b, outer?): Quaternion other

Set the quaternion as the shortest path rotation between two vectors a and b, with both vectors normalized by default

Parameters ​

a VectorUsage: vector a
b VectorUsage: vector b
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

slerp ​

• Static slerp(a, b, t, outer?): Quaternion other

Quaternion spherical interpolation

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
t numberUsage: Interpolation
range: [0,1] type: Floating point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

sqlerp ​

• Static sqlerp(a, b, c, d, t, outer?): Quaternion other

Quaternion spherical interpolation with two control points

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b
c QuaternionUsage: Quaternion c
d QuaternionUsage: Quaternion d
t numberUsage: Interpolation
range: [0,1] type: Floating point number
outer? QuaternionUsage: Quaternion object for receiving results default: null

Returns ​

QuaternionReturn a quaternion

strictEquals ​

• Static strictEquals(a, b): boolean other

Quaternion equivalence judgment

Parameters ​

a QuaternionUsage: Quaternion a
b QuaternionUsage: Quaternion b

Returns ​

booleanReturn the result of whether it is equal or not

toMatrix3x3 ​

• Static toMatrix3x3(q, outer?): Matrix3x3 other

Calculate matrix based on quaternion rotation information

Parameters ​

q QuaternionUsage: Quaternion information
outer? Matrix3x3Usage: Matrix3x3 object written default: null

Returns ​

Matrix3x3Return a three-dimensional matrix