2
0

Quaternion.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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">Implementation of a <a href="http://en.wikipedia.org/wiki/Quaternion">quaternion</a>. This is used for rotating things without encountering the dreaded <a href="http://en.wikipedia.org/wiki/Gimbal_lock">gimbal lock</a> issue, amongst other advantages.</div>
  13. <h2>Example</h2>
  14. <code>var quaternion = new THREE.Quaternion();
  15. quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );
  16. var vector = new THREE.Vector3( 1, 0, 0 );
  17. vector.applyQuaternion( quaternion );
  18. </code>
  19. <h2>Constructor</h2>
  20. <h3>[name]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] )</h3>
  21. <div>
  22. x - x coordinate<br />
  23. y - y coordinate<br />
  24. z - z coordinate<br />
  25. w - w coordinate
  26. </div>
  27. <h2>Properties</h2>
  28. <h3>[property:Float x]</h3>
  29. <h3>[property:Float y]</h3>
  30. <h3>[property:Float z]</h3>
  31. <h3>[property:Float w]</h3>
  32. <h2>Methods</h2>
  33. <h3>[method:Quaternion set]( [page:Float x], [page:Float y], [page:Float z], [page:Float w] ) [page:Quaternion this]</h3>
  34. <div>
  35. Sets values of this quaternion.
  36. </div>
  37. <h3>[method:Quaternion copy]( [page:Quaternion q] ) [page:Quaternion this]</h3>
  38. <div>
  39. Copies values of *q* to this quaternion.
  40. </div>
  41. <h3>[method:Quaternion fromArray]( [page:Array array], [page:Integer offset] ) [page:Quaternion this]</h3>
  42. <div>
  43. array -- Array of format (x, y, z, w) used to construct the quaternion.<br />
  44. offset -- An optional offset into the array.
  45. </div>
  46. <div>
  47. Sets this quaternion's component values from an array.
  48. </div>
  49. <h3>[method:Quaternion setFromEuler]( [page:Euler euler] ) [page:Quaternion this]</h3>
  50. <div>
  51. Sets this quaternion from rotation specified by Euler angle.
  52. </div>
  53. <h3>[method:Quaternion setFromAxisAngle]( [page:Vector3 axis], [page:Float angle] ) [page:Quaternion this]</h3>
  54. <div>
  55. Sets this quaternion from rotation specified by axis and angle.<br />
  56. Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm].<br />
  57. *Axis* is asumed to be normalized, *angle* is in radians.
  58. </div>
  59. <h3>[method:Quaternion setFromRotationMatrix]( [page:Matrix4 m] ) [page:Quaternion this]</h3>
  60. <div>
  61. Sets this quaternion from rotation component of *m*.<br />
  62. Adapted from [link:http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm].
  63. </div>
  64. <h3>[method:Quaternion setFromUnitVectors]( [page:Vector3 vFrom], [page:Vector3 vTo] ) [page:Quaternion this]</h3>
  65. <div>
  66. Sets this quaternion to the rotation required to rotate direction vector *vFrom* to direction vector *vTo*.<br />
  67. Adapted from [link:http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors].<br />
  68. *vFrom* and *vTo* are assumed to be normalized.
  69. </div>
  70. <h3>[method:Quaternion inverse]() [page:Quaternion this]</h3>
  71. <div>
  72. Inverts this quaternion.
  73. </div>
  74. <h3>[method:Float length]() [page:Quaternion this]</h3>
  75. <div>
  76. Computes length of this quaternion.
  77. </div>
  78. <h3>[method:Quaternion normalize]() [page:Quaternion this]</h3>
  79. <div>
  80. Normalizes this quaternion.
  81. </div>
  82. <h3>[method:Quaternion multiply]( [page:Quaternion q] ) [page:Quaternion this]</h3>
  83. <div>
  84. Multiplies this quaternion by *q*.
  85. </div>
  86. <h3>[method:Quaternion premultiply]( [page:Quaternion q] ) [page:Quaternion this]</h3>
  87. <div>
  88. Pre-multiplies this quaternion by *q*.
  89. </div>
  90. <h3>[method:Quaternion multiplyQuaternions]( [page:Quaternion a], [page:Quaternion b] ) [page:Quaternion this]</h3>
  91. <div>
  92. Sets this quaternion to *a x b*<br />
  93. Adapted from [link:http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm].
  94. </div>
  95. <h3>[method:Quaternion multiplyVector3]( [page:Vector3 vector], [page:Vector3 dest] ) [page:Quaternion this]</h3>
  96. <div>
  97. Rotates *vector* by this quaternion into *dest*.<br />
  98. If *dest* is not specified, result goes to *vec*.
  99. </div>
  100. <h3>[method:Float lengthSq]() [page:Quaternion this]</h3>
  101. <div>
  102. Calculates the squared length of the quaternion.
  103. </div>
  104. <h3>[method:Quaternion conjugate]() [page:Quaternion this]</h3>
  105. <div>
  106. Returns the rotational conjugate of this quaternion. The conjugate of a quaternion
  107. represents the same rotation in the opposite direction about the rotational axis.
  108. </div>
  109. <h3>[method:Quaternion slerp]( [page:Quaternion quaternionB], [page:float t] ) [page:Quaternion this]</h3>
  110. <div>
  111. quaternionB -- The other quaternion rotation<br />
  112. t -- Normalized 0 to 1 interpolation factor
  113. </div>
  114. <div>
  115. Handles the spherical linear interpolation between quaternions. *t* represents the amount of rotation
  116. between this quaternion (where *t* is 0) and quaternionB (where *t* is 1). This quaternion is set to
  117. the result. Also see the static version of the *slerp* below.
  118. </div>
  119. <code>
  120. // rotate a mesh towards a target quaternion
  121. mesh.quaternion.slerp( endQuaternion, 0.01 );
  122. </code>
  123. <h3>[method:Boolean equals]( [page:Quaternion v] ) [page:Quaternion this]</h3>
  124. <div>
  125. v -- Quaternion that this quaternion will be compared to.
  126. </div>
  127. <div>
  128. Compares each component of *v* to each component of this quaternion to determine if they
  129. represent the same rotation.
  130. </div>
  131. <h3>[method:Quaternion clone]() [page:Quaternion this]</h3>
  132. <div>
  133. Clones this quaternion.
  134. </div>
  135. <h3>[method:Array toArray]( [page:Array array], [page:Integer offset] ) [page:Quaternion this]</h3>
  136. <div>
  137. array -- An optional array to store the quaternion.<br/>
  138. offset -- An optional offset into the output array.
  139. </div>
  140. <div>
  141. Returns the numerical elements of this quaternion in an array of format (x, y, z, w).
  142. </div>
  143. <h2>Static Methods</h2>
  144. <h3>[method:Quaternion slerp]( [page:Quaternion qStart], [page:Quaternion qEnd], [page:Quaternion qTarget], [page:Float t] )</h3>
  145. <div>
  146. qStart -- The starting quaternion (where *t* is 0)<br />
  147. qEnd -- The ending quaternion (where *t* is 1)<br />
  148. qTarget -- The target quaternion that gets set with the result<br />
  149. t -- Normalized 0 to 1 interpolation factor
  150. </div>
  151. <div>
  152. Unlike the normal method, the static version of slerp sets a target quaternion to the result of the slerp operation.
  153. </div>
  154. <code>
  155. // Code setup
  156. var startQuaternion = new THREE.Quaternion().set( 0, 0, 0, 1 ).normalize();
  157. var endQuaternion = new THREE.Quaternion().set( 1, 1, 1, 1 ).normalize();
  158. var t = 0;
  159. </code>
  160. <code>
  161. // Update a mesh's rotation in the loop
  162. t = ( t + 0.01 ) % 1; // constant angular momentum
  163. THREE.Quaternion.slerp( startQuaternion, endQuaternion, mesh.quaternion, t );
  164. </code>
  165. <h3>[method:null slerpFlat]( [page:Array dst], [page:Integer dstOffset], [page:Array src0], [page:Integer srcOffset0], [page:Array src1], [page:Integer srcOffset1], [page:Float t] )</h3>
  166. <div>
  167. dst -- The output array.<br />
  168. dstOffset -- An offset into the output array.<br />
  169. src0 -- The source array of the starting quaternion.<br />
  170. srcOffset0 -- An offset into the array *src0*.<br />
  171. src1 -- The source array of the target quatnerion.<br />
  172. srcOffset1 -- An offset into the array *src1*.<br />
  173. t -- Interpolation factor 0 at start, 1 at end.
  174. </div>
  175. <div>
  176. Like the static *slerp* method above, but operates directly on flat arrays of numbers.
  177. </div>
  178. <!-- Note: Do not add non-static methods to the bottom of this page. Put them above the <h2>Static Methods</h2> -->
  179. <h2>Source</h2>
  180. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  181. </body>
  182. </html>