[name]
Implementation of a
quaternion. This is used for rotating things without encountering the dreaded
gimbal lock issue, amongst other advantages.
Example
var quaternion = new THREE.Quaternion();
quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
var vector = new THREE.Vector3( 1, 0, 0 );
vector.applyQuaternion( quaternion );
Constructor
[name]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] )
x - x coordinate
y - y coordinate
z - z coordinate
w - w coordinate
Properties
[property:Float x]
[property:Float y]
[property:Float z]
[property:Float w]
Methods
[method:Quaternion set]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] ) [page:Quaternion this]
Sets values of this quaternion.
[method:Quaternion copy]( [page:Quaternion q] ) [page:Quaternion this]
Copies values of *q* to this quaternion.
[method:Quaternion fromArray]( [page:Array array], [page:Integer offset] ) [page:Quaternion this]
array -- Array of format (x, y, z, w) used to construct the quaternion.
offset -- An optional offset into the array.
Sets this quaternion's component values from an array.
[method:Quaternion setFromEuler]( [page:Euler euler] ) [page:Quaternion this]
Sets this quaternion from rotation specified by Euler angle.
[method:Quaternion setFromAxisAngle]( [page:Vector3 axis], [page:Float angle] ) [page:Quaternion this]
Sets this quaternion from rotation specified by axis and angle.
Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm].
*Axis* is asumed to be normalized, *angle* is in radians.
[method:Quaternion setFromRotationMatrix]( [page:Matrix4 m] ) [page:Quaternion this]
Sets this quaternion from rotation component of *m*.
Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm].
[method:Quaternion setFromUnitVectors]( [page:Vector3 vFrom], [page:Vector3 vTo] ) [page:Quaternion this]
Sets this quaternion to the rotation required to rotate direction vector *vFrom* to direction vector *vTo*.
Adapted from [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors].
*vFrom* and *vTo* are assumed to be normalized.
[method:Quaternion inverse]() [page:Quaternion this]
Inverts this quaternion.
[method:Float length]() [page:Quaternion this]
Computes length of this quaternion.
[method:Quaternion normalize]() [page:Quaternion this]
Normalizes this quaternion.
[method:Quaternion multiply]( [page:Quaternion q] ) [page:Quaternion this]
Multiplies this quaternion by *q*.
[method:Quaternion premultiply]( [page:Quaternion q] ) [page:Quaternion this]
Pre-multiplies this quaternion by *q*.
[method:Quaternion multiplyQuaternions]( [page:Quaternion a], [page:Quaternion b] ) [page:Quaternion this]
Sets this quaternion to *a x b*
Adapted from [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm].
[method:Quaternion multiplyVector3]( [page:Vector3 vector], [page:Vector3 dest] ) [page:Quaternion this]
Rotates *vector* by this quaternion into *dest*.
If *dest* is not specified, result goes to *vec*.
[method:Float lengthSq]() [page:Quaternion this]
Calculates the squared length of the quaternion.
[method:Quaternion conjugate]() [page:Quaternion this]
Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
represents the same rotation in the opposite direction about the rotational axis.
[method:Quaternion slerp]( [page:Quaternion quaternionB], [page:float t] ) [page:Quaternion this]
quaternionB -- The other quaternion rotation
t -- Normalized 0 to 1 interpolation factor
Handles the spherical linear interpolation between quaternions. *t* represents the amount of rotation
between this quaternion (where *t* is 0) and quaternionB (where *t* is 1). This quaternion is set to
the result. Also see the static version of the *slerp* below.
// rotate a mesh towards a target quaternion
mesh.quaternion.slerp( endQuaternion, 0.01 );
[method:Boolean equals]( [page:Quaternion v] ) [page:Quaternion this]
v -- Quaternion that this quaternion will be compared to.
Compares each component of *v* to each component of this quaternion to determine if they
represent the same rotation.
[method:Quaternion clone]() [page:Quaternion this]
Clones this quaternion.
[method:Array toArray]( [page:Array array], [page:Integer offset] ) [page:Quaternion this]
array -- An optional array to store the quaternion.
offset -- An optional offset into the output array.
Returns the numerical elements of this quaternion in an array of format (x, y, z, w).
Static Methods
[method:Quaternion slerp]( [page:Quaternion qStart], [page:Quaternion qEnd], [page:Quaternion qTarget], [page:Float t] )
qStart -- The starting quaternion (where *t* is 0)
qEnd -- The ending quaternion (where *t* is 1)
qTarget -- The target quaternion that gets set with the result
t -- Normalized 0 to 1 interpolation factor
Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
// Code setup
var startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
var endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
var t = 0;
// Update a mesh's rotation in the loop
t = ( t + 0.01 ) % 1; // constant angular momentum
THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
[method:Quaternion slerpFlat]( [page:Array dst], [page:Integer dstOffset], [page:Array src0], [page:Integer srcOffset0], [page:Array src1], [page:Integer srcOffset1], [page:Float t] )
dst -- The output array.
dstOffset -- An offset into the output array.
src0 -- The source array of the starting quaternion.
srcOffset0 -- An offset into the array *src0*.
src1 -- The source array of the target quatnerion.
srcOffset1 -- An offset into the array *src1*.
t -- Interpolation factor 0 at start, 1 at end.
Like the static *slerp* method above, but operates directly on flat arrays of numbers.
Source
[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]