Quaternion.html 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. <div 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 rotating things]
  15. without encountering the dreaded
  16. [link:http://en.wikipedia.org/wiki/Gimbal_lock gimbal lock] issue, amongst other
  17. advantages.
  18. </div>
  19. <h2>Example</h2>
  20. <code>
  21. var quaternion = new THREE.Quaternion();
  22. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  23. var vector = new THREE.Vector3( 1, 0, 0 );
  24. vector.applyQuaternion( quaternion );
  25. </code>
  26. <h2>Constructor</h2>
  27. <h3>[name]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] )</h3>
  28. <div>
  29. [page:Float x] - x coordinate<br />
  30. [page:Float y] - y coordinate<br />
  31. [page:Float z] - z coordinate<br />
  32. [page:Float w] - w coordinate
  33. </div>
  34. <h2>Properties</h2>
  35. <h3>[property:Float x]</h3>
  36. <div>Changing this property will result in </div>
  37. <h3>[property:Float y]</h3>
  38. <div>
  39. </div>
  40. <h3>[property:Float z]</h3>
  41. <div>
  42. </div>
  43. <h3>[property:Float w]</h3>
  44. <div>
  45. </div>
  46. <h2>Methods</h2>
  47. <h3>[method:Quaternion clone]()</h3>
  48. <div>
  49. Creates a new Quaternion with identical [page:.x x], [page:.y y],
  50. [page:.z z] and [page:.w w] properties to this one.
  51. </div>
  52. <h3>[method:Quaternion conjugate]()</h3>
  53. <div>
  54. Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
  55. represents the same rotation in the opposite direction about the rotational axis.
  56. </div>
  57. <h3>[method:Quaternion copy]( [page:Quaternion q] )</h3>
  58. <div>
  59. Copies the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  60. of [page:Quaternion q] into this quaternion.
  61. </div>
  62. <h3>[method:Boolean equals]( [page:Quaternion v] )</h3>
  63. <div>
  64. [page:Quaternion v] - Quaternion that this quaternion will be compared to.<br /><br />
  65. Compares the [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties of
  66. [page:Quaternion v] to the equivalent properties of this quaternion to determine if they
  67. represent the same rotation.
  68. </div>
  69. <h3>[method:Float dot]( [page:Quaternion v] )</h3>
  70. <div>
  71. Calculate the [link:https://en.wikipedia.org/wiki/Dot_product dot product] of
  72. quaternions [page:Quaternion v] and this one.
  73. </div>
  74. <h3>[method:Quaternion fromArray]( [page:Array array], [page:Integer offset] )</h3>
  75. <div>
  76. [page:Array array] - array of format (x, y, z, w) used to construct the quaternion.<br />
  77. [page:Integer offset] - (optional) an offset into the array.<br /><br />
  78. Sets this quaternion's [page:.x x], [page:.y y], [page:.z z] and [page:.w w] properties
  79. from an array.
  80. </div>
  81. <h3>[method:Quaternion inverse]()</h3>
  82. <div>
  83. Inverts this quaternion - calculated the [page:.conjugate conjugate] and then
  84. [page:.normalize normalizes] the result.
  85. </div>
  86. <h3>[method:Float length]()</h3>
  87. <div>Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  88. (straight-line length) of this quaternion, considered as a 4 dimensional vector.</div>
  89. <h3>[method:Float lengthSq]()</h3>
  90. <div>
  91. Computes the [link:https://en.wikipedia.org/wiki/Euclidean_distance Euclidean length]
  92. (straight-line length) of this quaternion, considered as a 4 dimensional
  93. vector. This can be useful if you are comparing the lengths of two quaternions,
  94. as this is a slightly more efficient calculation than [page:.length length]().
  95. </div>
  96. <h3>[method:Quaternion normalize]()</h3>
  97. <div>
  98. [link:https://en.wikipedia.org/wiki/Normalized_vector Normalizes] this quaternion - that is,
  99. calculated the quaternion that performs the same rotation as this one, but has [page:.length length]
  100. equal to *1*.
  101. </div>
  102. <h3>[method:Quaternion multiply]( [page:Quaternion q] )</h3>
  103. <div>Multiplies this quaternion by [page:Quaternion q].</div>
  104. <h3>[method:Quaternion multiplyQuaternions]( [page:Quaternion a], [page:Quaternion b] )</h3>
  105. <div>
  106. Sets this quaternion to [page:Quaternion a] x [page:Quaternion b].<br />
  107. Adapted from [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm].
  108. </div>
  109. <h3>[method:Quaternion onChange]( [page:Function onChangeCallback] )</h3>
  110. <div>Set the [page:.onChangeCallback onChangeCallback]() method.</div>
  111. <h3>[method:Quaternion onChangeCallback]( )</h3>
  112. <div>
  113. This function is called whenever and of the following occur:
  114. <ul>
  115. <li>
  116. The [page:.x x], [page:.y y], [page:.z z] or
  117. [page:.w w] properties are changed.
  118. </li>
  119. <li>
  120. The [page:.set set](), [page:.copy copy](), [page:.clone clone](),
  121. [page:.setFromAxisAngle setFromAxisAngle](), [page:.setFromRotationMatrix setFromRotationMatrix](),
  122. [page:.conjugate conjugate](), [page:.normalize normalize](),
  123. [page:.multiplyQuaternions multiplyQuaternions](), [page:.slerp slerp]() or [page:.fromArray fromArray]()
  124. functions are called.
  125. </li>
  126. <li>
  127. [page:.setFromEuler setFromEuler]() function is called with its *update* argument set to true.
  128. </li>
  129. </ul>
  130. By default it is the empty function, however you can change it if needed using [page:.onChange onChange]( [page:Function onChangeCallback] ).
  131. </div>
  132. <h3>[method:Quaternion premultiply]( [page:Quaternion q] )</h3>
  133. <div>Pre-multiplies this quaternion by [page:Quaternion q].</div>
  134. <h3>[method:Quaternion slerp]( [page:Quaternion qb], [page:float t] )</h3>
  135. <div>
  136. [page:Quaternion qb] - The other quaternion rotation<br />
  137. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  138. Handles the spherical linear interpolation between quaternions. [page:float t] represents the
  139. amount of rotation between this quaternion (where [page:float t] is 0) and [page:Quaternion qb] (where
  140. [page:float t] is 1). This quaternion is set to the result. Also see the static version of the
  141. *slerp* below.
  142. <code>
  143. // rotate a mesh towards a target quaternion
  144. mesh.quaternion.slerp( endQuaternion, 0.01 );
  145. </code>
  146. </div>
  147. <h3>[method:Quaternion set]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] )</h3>
  148. <div>Sets [page.x x], [page.y y], [page.z z], [page.w w] properties of this quaternion.</div>
  149. <h3>[method:Quaternion setFromAxisAngle]( [page:Vector3 axis], [page:Float angle] )</h3>
  150. <div>
  151. Sets this quaternion from rotation specified by [page:Vector3 axis] and [page:Float angle].<br />
  152. Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm].<br />
  153. *Axis* is asumed to be normalized, *angle* is in radians.
  154. </div>
  155. <h3>[method:Quaternion setFromEuler]( [page:Euler euler] )</h3>
  156. <div>Sets this quaternion from the rotation specified by [page:Euler] angle.</div>
  157. <h3>[method:Quaternion setFromRotationMatrix]( [page:Matrix4 m] )</h3>
  158. <div>
  159. Sets this quaternion from rotation component of [page:Matrix4 m].<br />
  160. Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm].
  161. </div>
  162. <h3>[method:Quaternion setFromUnitVectors]( [page:Vector3 vFrom], [page:Vector3 vTo] )</h3>
  163. <div>
  164. Sets this quaternion to the rotation required to rotate direction vector [page:Vector3 vFrom] to
  165. direction vector [page:Vector3 vTo].<br />
  166. Adapted from [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors].<br />
  167. [page:Vector3 vFrom] and [page:Vector3 vTo] are assumed to be normalized.
  168. </div>
  169. <h3>[method:Array toArray]( [page:Array array], [page:Integer offset] )</h3>
  170. <div>
  171. [page:Array array] - An optional array to store the quaternion. If not specified a new array will be created.<br/>
  172. [page:Integer offset] - optional) if specified, the result will be copied
  173. into this [page:Array].<br /><br />
  174. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
  175. </div>
  176. <h2>Static Methods</h2>
  177. <h3>[method:Quaternion slerp]( [page:Quaternion qStart], [page:Quaternion qEnd], [page:Quaternion qTarget], [page:Float t] )</h3>
  178. <div>
  179. [page:Quaternion qStart] - The starting quaternion (where [page:Float t] is 0)<br />
  180. [page:Quaternion qEnd] - The ending quaternion (where [page:Float t] is 1)<br />
  181. [page:Quaternion qTarget] - The target quaternion that gets set with the result<br />
  182. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  183. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  184. <code>
  185. // Code setup
  186. var startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  187. var endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  188. var t = 0;
  189. // Update a mesh's rotation in the loop
  190. t = ( t + 0.01 ) % 1; // constant angular momentum
  191. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  192. </code>
  193. </div>
  194. <h3>
  195. [method:null slerpFlat](
  196. [page:Array dst],
  197. [page:Integer dstOffset],
  198. [page:Array src0],
  199. [page:Integer srcOffset0],
  200. [page:Array src1],
  201. [page:Integer srcOffset1],
  202. [page:Float t]
  203. )
  204. </h3>
  205. <div>
  206. [page:Array dst] - The output array.<br />
  207. [page:Integer dstOffset] - An offset into the output array.<br />
  208. [page:Array src0] - The source array of the starting quaternion.<br />
  209. [page:Integer srcOffset0] - An offset into the array *src0*.<br />
  210. [page:Array src1] - The source array of the target quatnerion.<br />
  211. [page:Integer srcOffset1] - An offset into the array *src1*.<br />
  212. [page:float t] - Normalized interpolation factor (between 0 and 1).<br /><br />
  213. <div>
  214. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  215. </div>
  216. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  217. <h2>Source</h2>
  218. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  219. </body>
  220. </html>