Skip to content

editorGlobalsQuaternionUtils

Class: QuaternionUtils

Because everybody hates quaternions

Hierarchy

  • QuaternionUtils

Index

Methods

Methods

Static angleBetween

angleBetween(q1: ReadonlyQuaternion, q2: ReadonlyQuaternion): number

Defined in src/util/QuaternionUtils.ts:17

Calculates the angle between two quaternions

Parameters:

Name Type
q1 ReadonlyQuaternion
q2 ReadonlyQuaternion

Returns: number

the angle between two quaternions, in radians


Static calculateRelativeRotation

calculateRelativeRotation(from: Quaternion, to: Quaternion): Quaternion

Defined in src/util/QuaternionUtils.ts:45

Calculates the relative rotation from one to another quaternion

Parameters:

Name Type Description
from Quaternion -
to Quaternion -

Returns: Quaternion

normalized relative Rotation


Static calculateRotationAngle

calculateRotationAngle(quaternion: Quaternion): number

Defined in src/util/QuaternionUtils.ts:193

Parameters:

Name Type
quaternion Quaternion

Returns: number

overall angle of the rotation


Static calculateRotationAxis

calculateRotationAxis(quaternion: Quaternion): Vector3

Defined in src/util/QuaternionUtils.ts:198

Parameters:

Name Type
quaternion Quaternion

Returns: Vector3


Static combineRotations

combineRotations(firstRotation: Quaternion, secondRotation: Quaternion, target?: Quaternion): Quaternion

Defined in src/util/QuaternionUtils.ts:80

Combines two rotations - the object will be rotated around the first rotation, and then around the second rotation

Parameters:

Name Type Description
firstRotation Quaternion -
secondRotation Quaternion -
target? Quaternion -

Returns: Quaternion

combined Rotation


Static formatHumanReadable

formatHumanReadable(quaternion: Quaternion): string

Defined in src/util/QuaternionUtils.ts:28

Parameters:

Name Type
quaternion Quaternion

Returns: string

the quaternion formulated as euler angles in a human readable way


Static fromEulerAngles

fromEulerAngles(angles: object, target: Quaternion‹›): Quaternion

Defined in src/util/QuaternionUtils.ts:184

Parameters:

angles: object

Name Type
x number
y number
z number

Default value target: Quaternion‹›= new Quaternion()

Returns: Quaternion


Static fromTwoDirections

fromTwoDirections(fromDirectionA: ReadonlyVector3, fromDirectionB: ReadonlyVector3, toDirectionA: Vector3, toDirectionB: ReadonlyVector3): Quaternion

Defined in src/util/QuaternionUtils.ts:168

Create a quaternion rotating fromDirectionA/B onto toDirectionA/B by combining a quaternion rotating A onto each other and a quaternion rotating B around A onto each other.

Parameters:

Name Type
fromDirectionA ReadonlyVector3
fromDirectionB ReadonlyVector3
toDirectionA Vector3
toDirectionB ReadonlyVector3

Returns: Quaternion


Static fromUnitVectors

fromUnitVectors(vFrom: Vector3, vTo: Vector3, target: Quaternion): Quaternion

Defined in src/util/QuaternionUtils.ts:220

Sets this quaternion to the rotation required to rotate direction vector vFrom to direction vector vTo. vFrom and vTo are assumed to be normalized.

This method is a quick fix for the original ThreeJS Quaternion.setFromUnitVectors function which unfortunately was to inaccurate as it rounded with a higher epsilon value than Number.EPSILON. For more information check out this link to the issue and this link to the code.

TODO: Method gets deprecated with ThreeJS version 127 which includes the fix.

Parameters:

Name Type Default Description
vFrom Vector3 - from direction
vTo Vector3 - to direction
target Quaternion new Quaternion() -

Returns: Quaternion

the rotation quaternion


Static fromVectorsAroundNormal

fromVectorsAroundNormal(source: ReadonlyVector3, target: ReadonlyVector3, normal: Vector3, targetQuaternion: Quaternion‹›): Quaternion

Defined in src/util/QuaternionUtils.ts:139

Creates a quaternion that rotates the source vector onto the target vector, but constrains the rotation to be around the specified normal vector. Useful if you want a fromUnitVectors, but want to be sure the rotation lies in a specified plane.

Parameters:

Name Type Default Description
source ReadonlyVector3 - the source vector
target ReadonlyVector3 - the target vector
normal Vector3 - the normal vector which specifies the plane the rotation lies in
targetQuaternion Quaternion‹› new Quaternion() -

Returns: Quaternion


Static innerProduct

innerProduct(q1: Quaternion, q2: Quaternion): number

Defined in src/util/QuaternionUtils.ts:126

Calculates the inner product of two quaternions

Parameters:

Name Type
q1 Quaternion
q2 Quaternion

Returns: number

the inner product


Static nearlyEqual

nearlyEqual(q1: Quaternion, q2: Quaternion, angleTolerance: number): boolean

Defined in src/util/QuaternionUtils.ts:101

Checks whether two quaternions result in nearly the same rotation by testing the angle difference between rotated vectors (why vectors? cause e.g. a 360° rotation does still make them nearly equal, what should be true for most cases)

Parameters:

Name Type Default Description
q1 Quaternion - first quaternion
q2 Quaternion - second quaternion
angleTolerance number 0.001 maximum allowed angle difference, in radians

Returns: boolean

true, if both quaternions are the same within tolerances, false, if not


Static swingTwistDecomposition

swingTwistDecomposition(rotation: Quaternion, direction: Vector3, targetSwing: Quaternion, targetTwist: Quaternion): object

Defined in src/util/QuaternionUtils.ts:256

Decompose the rotation on to 2 parts. 1. Twist - rotation around the "direction" vector 2. Swing - rotation around axis that is perpendicular to "direction" vector The rotation can be composed back by rotation = swing * twist

has singularity in case of swing_rotation close to 180 degrees rotation. if the input quaternion is of non-unit length, the outputs are non-unit as well otherwise, outputs are both unit

Source: https://stackoverflow.com/a/22401169

Parameters:

Name Type Default
rotation Quaternion -
direction Vector3 -
targetSwing Quaternion new Quaternion()
targetTwist Quaternion new Quaternion()

Returns: object

  • swing: Quaternion‹› = targetSwing

  • twist: Quaternion‹› = targetTwist