Quaternion.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. Implementation of a [link:http://en.wikipedia.org/wiki/Quaternion quaternion].
  14. This is used for [link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation describing rotations]
  15. more concisely compared to 3x3 matrices, amongst other
  16. advantages.
  17. </p>
  18. <h2>Example</h2>
  19. <code>
  20. var quaternion = new THREE.Quaternion();
  21. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  22. var vector = new THREE.Vector3( 1, 0, 0 );
  23. vector.applyQuaternion( quaternion );
  24. </code>
  25. <h2>Constructor</h2>
  26. <h3>[name]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  27. <p>
  28. [page:Float x] - x coordinate<br />
  29. [page:Float y] - y coordinate<br />
  30. [page:Float z] - z coordinate<br />
  31. [page:Float w] - w coordinate
  32. </p>
  33. <h2>Properties</h2>
  34. <h3>[property:Float x]</h3>
  35. <h3>[property:Float y]</h3>
  36. <h3>[property:Float z]</h3>
  37. <h3>[property:Float w]</h3>
  38. <h2>Methods</h2>
  39. <h3>[method:Float angleTo]( [param:Quaternion q] )</h3>
  40. <p>
  41. Returns the angle between this quaternion and quaternion [page:Quaternion q] in radians.
  42. </p>
  43. <h3>[method:Quaternion clone]()</h3>
  44. <p>
  45. Creates a new Quaternion with identical [page:.x x], [page:.y y],
  46. [page:.z z] and [page:.w w] properties to this one.
  47. </p>
  48. <h3>[method:Quaternion conjugate]()</h3>
  49. <p>
  50. Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
  51. represents the same rotation in the opposite direction about the rotational axis.
  52. </p>
  53. <h3>[method:Quaternion copy]( [param:Quaternion q] )</h3>
  54. <p>
  55. Copies the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  56. of [page:Quaternion q] into this quaternion.
  57. </p>
  58. <h3>[method:Boolean equals]( [param:Quaternion v] )</h3>
  59. <p>
  60. [page:Quaternion v] - Quaternion that this quaternion will be compared to.<br /><br />
  61. Compares the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties of
  62. [page:Quaternion v] to the equivalent properties of this quaternion to determine if they
  63. represent the same rotation.
  64. </p>
  65. <h3>[method:Float dot]( [param:Quaternion v] )</h3>
  66. <p>
  67. Calculates the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of
  68. quaternions [page:Quaternion v] and this one.
  69. </p>
  70. <h3>[method:Quaternion fromArray]( [param:Array array], [param:Integer offset] )</h3>
  71. <p>
  72. [page:Array array] - array of format (x, y, z, w) used to construct the quaternion.<br />
  73. [page:Integer offset] - (optional) an offset into the array.<br /><br />
  74. Sets this quaternion's [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  75. from an array.
  76. </p>
  77. <h3>[method:Quaternion inverse]()</h3>
  78. <p>
  79. Inverts this quaternion - calculates the [page:.conjugate conjugate]. The quaternion is assumed to have unit length.
  80. </p>
  81. <h3>[method:Float length]()</h3>
  82. <p>Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  83. (straight-line length) of this quaternion, considered as a 4 dimensional vector.</p>
  84. <h3>[method:Float lengthSq]()</h3>
  85. <p>
  86. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  87. (straight-line length) of this quaternion, considered as a 4 dimensional
  88. vector. This can be useful if you are comparing the lengths of two quaternions,
  89. as this is a slightly more efficient calculation than [page:.length length]().
  90. </p>
  91. <h3>[method:Quaternion normalize]()</h3>
  92. <p>
  93. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes] this quaternion - that is,
  94. calculated the quaternion that performs the same rotation as this one, but has [page:.length length]
  95. equal to *1*.
  96. </p>
  97. <h3>[method:Quaternion multiply]( [param:Quaternion q] )</h3>
  98. <p>Multiplies this quaternion by [page:Quaternion q].</p>
  99. <h3>[method:Quaternion multiplyQuaternions]( [param:Quaternion a], [param:Quaternion b] )</h3>
  100. <p>
  101. Sets this quaternion to [page:Quaternion a] x [page:Quaternion b].<br />
  102. Adapted from the method outlined [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm here].
  103. </p>
  104. <h3>[method:Quaternion premultiply]( [param:Quaternion q] )</h3>
  105. <p>Pre-multiplies this quaternion by [page:Quaternion q].</p>
  106. <h3>[method:Quaternion rotateTowards]( [param:Quaternion q], [param:Float step] )</h3>
  107. <p>
  108. [page:Quaternion q] - The target quaternion.<br />
  109. [page:float step] - The angular step in radians.<br /><br />
  110. Rotates this quaternion by a given angular step to the defined quaternion *q*.
  111. The method ensures that the final quaternion will not overshoot *q*.
  112. </p>
  113. <h3>[method:Quaternion slerp]( [param:Quaternion qb], [param:float t] )</h3>
  114. <p>
  115. [page:Quaternion qb] - The other quaternion rotation<br />
  116. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  117. Handles the spherical linear interpolation between quaternions. [page:float t] represents the
  118. amount of rotation between this quaternion (where [page:float t] is 0) and [page:Quaternion qb] (where
  119. [page:float t] is 1). This quaternion is set to the result. Also see the static version of the
  120. *slerp* below.
  121. <code>
  122. // rotate a mesh towards a target quaternion
  123. mesh.quaternion.slerp( endQuaternion, 0.01 );
  124. </code>
  125. </p>
  126. <h3>[method:Quaternion set]( [param:Float x], [param:Float y], [param:Float z], [param:Float w] )</h3>
  127. <p>Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion.</p>
  128. <h3>[method:Quaternion setFromAxisAngle]( [param:Vector3 axis], [param:Float angle] )</h3>
  129. <p>
  130. Sets this quaternion from rotation specified by [page:Vector3 axis] and [page:Float angle].<br />
  131. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm here].<br />
  132. *Axis* is assumed to be normalized, *angle* is in radians.
  133. </p>
  134. <h3>[method:Quaternion setFromEuler]( [param:Euler euler] )</h3>
  135. <p>Sets this quaternion from the rotation specified by [page:Euler] angle.</p>
  136. <h3>[method:Quaternion setFromRotationMatrix]( [param:Matrix4 m] )</h3>
  137. <p>
  138. Sets this quaternion from rotation component of [page:Matrix4 m].<br />
  139. Adapted from the method [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm here].
  140. </p>
  141. <h3>[method:Quaternion setFromUnitVectors]( [param:Vector3 vFrom], [param:Vector3 vTo] )</h3>
  142. <p>
  143. Sets this quaternion to the rotation required to rotate direction vector [page:Vector3 vFrom] to
  144. direction vector [page:Vector3 vTo].<br />
  145. Adapted from the method [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors here].<br />
  146. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  147. </p>
  148. <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
  149. <p>
  150. [page:Array array] - An optional array to store the quaternion. If not specified, a new array will be created.<br/>
  151. [page:Integer offset] - (optional) if specified, the result will be copied
  152. into this [page:Array].<br /><br />
  153. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
  154. </p>
  155. <h2>Static Methods</h2>
  156. <p>
  157. Static methods (as opposed to instance methods) are designed to be called directly from the class,
  158. rather than from a specific instance. So to use the static version of, call it like so:
  159. <code>
  160. THREE.Quaternion.slerp( qStart, qEnd, qTarget, t );
  161. </code>
  162. By contrast, to call the 'normal' or instanced slerp method, you would do the following:
  163. <code>
  164. //instantiate a quaternion with default values
  165. var q = new THREE.Quaternion();
  166. //call the instanced slerp method
  167. q.slerp( qb, t )
  168. </code>
  169. </p>
  170. <h3>[method:Quaternion slerp]( [param:Quaternion qStart], [param:Quaternion qEnd], [param:Quaternion qTarget], [param:Float t] )</h3>
  171. <p>
  172. [page:Quaternion qStart] - The starting quaternion (where [page:Float t] is 0)<br />
  173. [page:Quaternion qEnd] - The ending quaternion (where [page:Float t] is 1)<br />
  174. [page:Quaternion qTarget] - The target quaternion that gets set with the result<br />
  175. [page:float t] - interpolation factor in the closed interval [0, 1].<br /><br />
  176. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  177. <code>
  178. // Code setup
  179. var startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  180. var endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  181. var t = 0;
  182. // Update a mesh's rotation in the loop
  183. t = ( t + 0.01 ) % 1; // constant angular momentum
  184. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  185. </code>
  186. </p>
  187. <h3>[method:null slerpFlat]( [param:Array dst], [param:Integer dstOffset], [param:Array src0], [param:Integer srcOffset0], [param:Array src1], [param:Integer srcOffset1], [param:Float t] )</h3>
  188. <p>
  189. [page:Array dst] - The output array.<br />
  190. [page:Integer dstOffset] - An offset into the output array.<br />
  191. [page:Array src0] - The source array of the starting quaternion.<br />
  192. [page:Integer srcOffset0] - An offset into the array *src0*.<br />
  193. [page:Array src1] - The source array of the target quatnerion.<br />
  194. [page:Integer srcOffset1] - An offset into the array *src1*.<br />
  195. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  196. </p>
  197. <p>
  198. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  199. </p>
  200. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  201. <h2>Source</h2>
  202. <p>
  203. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  204. </p>
  205. </body>
  206. </html>