quaternion.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /**
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Copyright (c) 2008, Luke Benstead.
  6. All rights reserved.
  7. Redistribution and use in source and binary forms, with or without modification,
  8. are permitted provided that the following conditions are met:
  9. Redistributions of source code must retain the above copyright notice,
  10. this list of conditions and the following disclaimer.
  11. Redistributions in binary form must reproduce the above copyright notice,
  12. this list of conditions and the following disclaimer in the documentation
  13. and/or other materials provided with the distribution.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /**
  26. * The Quaternion class
  27. * @param {Number} x
  28. * @param {Number} y
  29. * @param {Number} z
  30. * @param {Number} w
  31. * @constructor
  32. */
  33. cc.kmQuaternion = function (x, y, z, w) {
  34. this.x = x || 0;
  35. this.y = y || 0;
  36. this.z = z || 0;
  37. this.w = w || 0;
  38. };
  39. ///< Returns pOut, sets pOut to the conjugate of pIn
  40. cc.kmQuaternionConjugate = function (pOut, pIn) {
  41. pOut.x = -pIn.x;
  42. pOut.y = -pIn.y;
  43. pOut.z = -pIn.z;
  44. pOut.w = pIn.w;
  45. return pOut;
  46. };
  47. ///< Returns the dot product of the 2 quaternions
  48. cc.kmQuaternionDot = function (q1, q2) {
  49. // A dot B = B dot A = AtBt + AxBx + AyBy + AzBz
  50. return (q1.w * q2.w +
  51. q1.x * q2.x +
  52. q1.y * q2.y +
  53. q1.z * q2.z);
  54. };
  55. ///< Returns the exponential of the quaternion
  56. cc.kmQuaternionExp = function (pOut, pIn) {
  57. //TODO not implement
  58. //cc.assert(0);
  59. return pOut;
  60. };
  61. ///< Makes the passed quaternion an identity quaternion
  62. cc.kmQuaternionIdentity = function (pOut) {
  63. pOut.x = 0.0;
  64. pOut.y = 0.0;
  65. pOut.z = 0.0;
  66. pOut.w = 1.0;
  67. return pOut;
  68. };
  69. ///< Returns the inverse of the passed Quaternion
  70. cc.kmQuaternionInverse = function (pOut, pIn) {
  71. var l = cc.kmQuaternionLength(pIn);
  72. var tmp = new cc.kmQuaternion();
  73. if (Math.abs(l) > cc.kmEpsilon) {
  74. pOut.x = 0.0;
  75. pOut.y = 0.0;
  76. pOut.z = 0.0;
  77. pOut.w = 0.0;
  78. return pOut;
  79. }
  80. ///Get the conjugute and divide by the length
  81. cc.kmQuaternionScale(pOut,
  82. cc.kmQuaternionConjugate(tmp, pIn), 1.0 / l);
  83. return pOut;
  84. };
  85. ///< Returns true if the quaternion is an identity quaternion
  86. cc.kmQuaternionIsIdentity = function (pIn) {
  87. return (pIn.x == 0.0 && pIn.y == 0.0 && pIn.z == 0.0 &&
  88. pIn.w == 1.0);
  89. };
  90. ///< Returns the length of the quaternion
  91. cc.kmQuaternionLength = function (pIn) {
  92. return Math.sqrt(cc.kmQuaternionLengthSq(pIn));
  93. };
  94. ///< Returns the length of the quaternion squared (prevents a sqrt)
  95. cc.kmQuaternionLengthSq = function (pIn) {
  96. return pIn.x * pIn.x + pIn.y * pIn.y +
  97. pIn.z * pIn.z + pIn.w * pIn.w;
  98. };
  99. ///< Returns the natural logarithm
  100. cc.kmQuaternionLn = function (pOut, pIn) {
  101. /*
  102. A unit quaternion, is defined by:
  103. Q == (cos(theta), sin(theta) * v) where |v| = 1
  104. The natural logarithm of Q is, ln(Q) = (0, theta * v)
  105. */
  106. //assert(0);
  107. //TODO not implement
  108. return pOut;
  109. };
  110. ///< Multiplies 2 quaternions together
  111. cc.kmQuaternionMultiply = function (pOut, q1, q2) {
  112. pOut.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z;
  113. pOut.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;
  114. pOut.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;
  115. pOut.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;
  116. return pOut;
  117. };
  118. ///< Normalizes a quaternion
  119. cc.kmQuaternionNormalize = function (pOut, pIn) {
  120. var length = cc.kmQuaternionLength(pIn);
  121. if(Math.abs(length) <= cc.kmEpsilon)
  122. throw "cc.kmQuaternionNormalize(): pIn is an invalid value";
  123. cc.kmQuaternionScale(pOut, pIn, 1.0 / length);
  124. return pOut;
  125. };
  126. ///< Rotates a quaternion around an axis
  127. cc.kmQuaternionRotationAxis = function (pOut, pV, angle) {
  128. var rad = angle * 0.5;
  129. var scale = Math.sin(rad);
  130. pOut.w = Math.cos(rad);
  131. pOut.x = pV.x * scale;
  132. pOut.y = pV.y * scale;
  133. pOut.z = pV.z * scale;
  134. return pOut;
  135. };
  136. ///< Creates a quaternion from a rotation matrix
  137. cc.kmQuaternionRotationMatrix = function (pOut, pIn) {
  138. /*
  139. Note: The OpenGL matrices are transposed from the description below
  140. taken from the Matrix and Quaternion FAQ
  141. if ( mat[0] > mat[5] && mat[0] > mat[10] ) { // Column 0:
  142. S = sqrt( 1.0 + mat[0] - mat[5] - mat[10] ) * 2;
  143. X = 0.25 * S;
  144. Y = (mat[4] + mat[1] ) / S;
  145. Z = (mat[2] + mat[8] ) / S;
  146. W = (mat[9] - mat[6] ) / S;
  147. } else if ( mat[5] > mat[10] ) { // Column 1:
  148. S = sqrt( 1.0 + mat[5] - mat[0] - mat[10] ) * 2;
  149. X = (mat[4] + mat[1] ) / S;
  150. Y = 0.25 * S;
  151. Z = (mat[9] + mat[6] ) / S;
  152. W = (mat[2] - mat[8] ) / S;
  153. } else { // Column 2:
  154. S = sqrt( 1.0 + mat[10] - mat[0] - mat[5] ) * 2;
  155. X = (mat[2] + mat[8] ) / S;
  156. Y = (mat[9] + mat[6] ) / S;
  157. Z = 0.25 * S;
  158. W = (mat[4] - mat[1] ) / S;
  159. }
  160. */
  161. var x, y, z, w;
  162. var m4x4 = [];
  163. var scale = 0.0;
  164. var diagonal = 0.0;
  165. if (!pIn) {
  166. return null;
  167. }
  168. /* 0 3 6
  169. 1 4 7
  170. 2 5 8
  171. 0 1 2 3
  172. 4 5 6 7
  173. 8 9 10 11
  174. 12 13 14 15*/
  175. m4x4[0] = pIn.mat[0];
  176. m4x4[1] = pIn.mat[3];
  177. m4x4[2] = pIn.mat[6];
  178. m4x4[4] = pIn.mat[1];
  179. m4x4[5] = pIn.mat[4];
  180. m4x4[6] = pIn.mat[7];
  181. m4x4[8] = pIn.mat[2];
  182. m4x4[9] = pIn.mat[5];
  183. m4x4[10] = pIn.mat[8];
  184. m4x4[15] = 1;
  185. var pMatrix = m4x4[0];
  186. diagonal = pMatrix[0] + pMatrix[5] + pMatrix[10] + 1;
  187. if (diagonal > cc.kmEpsilon) {
  188. // Calculate the scale of the diagonal
  189. scale = Math.sqrt(diagonal) * 2;
  190. // Calculate the x, y, x and w of the quaternion through the respective equation
  191. x = ( pMatrix[9] - pMatrix[6] ) / scale;
  192. y = ( pMatrix[2] - pMatrix[8] ) / scale;
  193. z = ( pMatrix[4] - pMatrix[1] ) / scale;
  194. w = 0.25 * scale;
  195. } else {
  196. // If the first element of the diagonal is the greatest value
  197. if (pMatrix[0] > pMatrix[5] && pMatrix[0] > pMatrix[10]) {
  198. // Find the scale according to the first element, and double that value
  199. scale = Math.sqrt(1.0 + pMatrix[0] - pMatrix[5] - pMatrix[10]) * 2.0;
  200. // Calculate the x, y, x and w of the quaternion through the respective equation
  201. x = 0.25 * scale;
  202. y = (pMatrix[4] + pMatrix[1] ) / scale;
  203. z = (pMatrix[2] + pMatrix[8] ) / scale;
  204. w = (pMatrix[9] - pMatrix[6] ) / scale;
  205. }
  206. // Else if the second element of the diagonal is the greatest value
  207. else if (pMatrix[5] > pMatrix[10]) {
  208. // Find the scale according to the second element, and double that value
  209. scale = Math.sqrt(1.0 + pMatrix[5] - pMatrix[0] - pMatrix[10]) * 2.0;
  210. // Calculate the x, y, x and w of the quaternion through the respective equation
  211. x = (pMatrix[4] + pMatrix[1] ) / scale;
  212. y = 0.25 * scale;
  213. z = (pMatrix[9] + pMatrix[6] ) / scale;
  214. w = (pMatrix[2] - pMatrix[8] ) / scale;
  215. } else {
  216. // Else the third element of the diagonal is the greatest value
  217. // Find the scale according to the third element, and double that value
  218. scale = Math.sqrt(1.0 + pMatrix[10] - pMatrix[0] - pMatrix[5]) * 2.0;
  219. // Calculate the x, y, x and w of the quaternion through the respective equation
  220. x = (pMatrix[2] + pMatrix[8] ) / scale;
  221. y = (pMatrix[9] + pMatrix[6] ) / scale;
  222. z = 0.25 * scale;
  223. w = (pMatrix[4] - pMatrix[1] ) / scale;
  224. }
  225. }
  226. pOut.x = x;
  227. pOut.y = y;
  228. pOut.z = z;
  229. pOut.w = w;
  230. return pOut;
  231. };
  232. ///< Create a quaternion from yaw, pitch and roll
  233. cc.kmQuaternionRotationYawPitchRoll = function (pOut, yaw, pitch, roll) {
  234. var ex, ey, ez; // temp half euler angles
  235. var cr, cp, cy, sr, sp, sy, cpcy, spsy; // temp vars in roll,pitch yaw
  236. ex = cc.kmDegreesToRadians(pitch) / 2.0; // convert to rads and half them
  237. ey = cc.kmDegreesToRadians(yaw) / 2.0;
  238. ez = cc.kmDegreesToRadians(roll) / 2.0;
  239. cr = Math.cos(ex);
  240. cp = Math.cos(ey);
  241. cy = Math.cos(ez);
  242. sr = Math.sin(ex);
  243. sp = Math.sin(ey);
  244. sy = Math.sin(ez);
  245. cpcy = cp * cy;
  246. spsy = sp * sy;
  247. pOut.w = cr * cpcy + sr * spsy;
  248. pOut.x = sr * cpcy - cr * spsy;
  249. pOut.y = cr * sp * cy + sr * cp * sy;
  250. pOut.z = cr * cp * sy - sr * sp * cy;
  251. cc.kmQuaternionNormalize(pOut, pOut);
  252. return pOut;
  253. };
  254. ///< Interpolate between 2 quaternions
  255. cc.kmQuaternionSlerp = function (pOut, q1, q2, t) {
  256. /*float CosTheta = Q0.DotProd(Q1);
  257. float Theta = acosf(CosTheta);
  258. float SinTheta = sqrtf(1.0f-CosTheta*CosTheta);
  259. float Sin_T_Theta = sinf(T*Theta)/SinTheta;
  260. float Sin_OneMinusT_Theta = sinf((1.0f-T)*Theta)/SinTheta;
  261. Quaternion Result = Q0*Sin_OneMinusT_Theta;
  262. Result += (Q1*Sin_T_Theta);
  263. return Result;*/
  264. if (q1.x == q2.x &&
  265. q1.y == q2.y &&
  266. q1.z == q2.z &&
  267. q1.w == q2.w) {
  268. pOut.x = q1.x;
  269. pOut.y = q1.y;
  270. pOut.z = q1.z;
  271. pOut.w = q1.w;
  272. return pOut;
  273. }
  274. var ct = cc.kmQuaternionDot(q1, q2);
  275. var theta = Math.acos(ct);
  276. var st = Math.sqrt(1.0 - cc.kmSQR(ct));
  277. var stt = Math.sin(t * theta) / st;
  278. var somt = Math.sin((1.0 - t) * theta) / st;
  279. var temp = new cc.kmQuaternion(), temp2 = new cc.kmQuaternion();
  280. cc.kmQuaternionScale(temp, q1, somt);
  281. cc.kmQuaternionScale(temp2, q2, stt);
  282. cc.kmQuaternionAdd(pOut, temp, temp2);
  283. return pOut;
  284. };
  285. ///< Get the axis and angle of rotation from a quaternion
  286. cc.kmQuaternionToAxisAngle = function (pIn, pAxis, pAngle) {
  287. var tempAngle; // temp angle
  288. var scale; // temp vars
  289. tempAngle = Math.acos(pIn.w);
  290. scale = Math.sqrt(cc.kmSQR(pIn.x) + cc.kmSQR(pIn.y) + cc.kmSQR(pIn.z));
  291. if (((scale > -cc.kmEpsilon) && scale < cc.kmEpsilon)
  292. || (scale < 2 * cc.kmPI + cc.kmEpsilon && scale > 2 * cc.kmPI - cc.kmEpsilon)) { // angle is 0 or 360 so just simply set axis to 0,0,1 with angle 0
  293. pAngle = 0.0;
  294. pAxis.x = 0.0;
  295. pAxis.y = 0.0;
  296. pAxis.z = 1.0;
  297. } else {
  298. pAngle = tempAngle * 2.0; // angle in radians
  299. pAxis.x = pIn.x / scale;
  300. pAxis.y = pIn.y / scale;
  301. pAxis.z = pIn.z / scale;
  302. cc.kmVec3Normalize(pAxis, pAxis);
  303. }
  304. };
  305. ///< Scale a quaternion
  306. cc.kmQuaternionScale = function (pOut, pIn, s) {
  307. pOut.x = pIn.x * s;
  308. pOut.y = pIn.y * s;
  309. pOut.z = pIn.z * s;
  310. pOut.w = pIn.w * s;
  311. return pOut;
  312. };
  313. cc.kmQuaternionAssign = function (pOut, pIn) {
  314. pOut.x = pIn.x;
  315. pOut.y = pIn.y;
  316. pOut.z = pIn.z;
  317. pOut.w = pIn.w;
  318. return pOut;
  319. };
  320. cc.kmQuaternionAdd = function (pOut, pQ1, pQ2) {
  321. pOut.x = pQ1.x + pQ2.x;
  322. pOut.y = pQ1.y + pQ2.y;
  323. pOut.z = pQ1.z + pQ2.z;
  324. pOut.w = pQ1.w + pQ2.w;
  325. return pOut;
  326. };
  327. /** Adapted from the OGRE engine!
  328. Gets the shortest arc quaternion to rotate this vector to the destination
  329. vector.
  330. @remarks
  331. If you call this with a dest vector that is close to the inverse
  332. of this vector, we will rotate 180 degrees around the 'fallbackAxis'
  333. (if specified, or a generated axis if not) since in this case
  334. ANY axis of rotation is valid.
  335. */
  336. cc.kmQuaternionRotationBetweenVec3 = function (pOut, vec1, vec2, fallback) {
  337. var v1 = new cc.kmVec3(), v2 = new cc.kmVec3();
  338. var a;
  339. cc.kmVec3Assign(v1, vec1);
  340. cc.kmVec3Assign(v2, vec2);
  341. cc.kmVec3Normalize(v1, v1);
  342. cc.kmVec3Normalize(v2, v2);
  343. a = cc.kmVec3Dot(v1, v2);
  344. if (a >= 1.0) {
  345. cc.kmQuaternionIdentity(pOut);
  346. return pOut;
  347. }
  348. if (a < (1e-6 - 1.0)) {
  349. if (Math.abs(cc.kmVec3LengthSq(fallback)) < cc.kmEpsilon) {
  350. cc.kmQuaternionRotationAxis(pOut, fallback, cc.kmPI);
  351. } else {
  352. var axis = new cc.kmVec3();
  353. var X = new cc.kmVec3();
  354. X.x = 1.0;
  355. X.y = 0.0;
  356. X.z = 0.0;
  357. cc.kmVec3Cross(axis, X, vec1);
  358. //If axis is zero
  359. if (Math.abs(cc.kmVec3LengthSq(axis)) < cc.kmEpsilon) {
  360. var Y = new cc.kmVec3();
  361. Y.x = 0.0;
  362. Y.y = 1.0;
  363. Y.z = 0.0;
  364. cc.kmVec3Cross(axis, Y, vec1);
  365. }
  366. cc.kmVec3Normalize(axis, axis);
  367. cc.kmQuaternionRotationAxis(pOut, axis, cc.kmPI);
  368. }
  369. } else {
  370. var s = Math.sqrt((1 + a) * 2);
  371. var invs = 1 / s;
  372. var c = new cc.kmVec3();
  373. cc.kmVec3Cross(c, v1, v2);
  374. pOut.x = c.x * invs;
  375. pOut.y = c.y * invs;
  376. pOut.z = c.z * invs;
  377. pOut.w = s * 0.5;
  378. cc.kmQuaternionNormalize(pOut, pOut);
  379. }
  380. return pOut;
  381. };
  382. cc.kmQuaternionMultiplyVec3 = function (pOut, q, v) {
  383. var uv = new cc.kmVec3(), uuv = new cc.kmVec3(), qvec = new cc.kmVec3();
  384. qvec.x = q.x;
  385. qvec.y = q.y;
  386. qvec.z = q.z;
  387. cc.kmVec3Cross(uv, qvec, v);
  388. cc.kmVec3Cross(uuv, qvec, uv);
  389. cc.kmVec3Scale(uv, uv, (2.0 * q.w));
  390. cc.kmVec3Scale(uuv, uuv, 2.0);
  391. cc.kmVec3Add(pOut, v, uv);
  392. cc.kmVec3Add(pOut, pOut, uuv);
  393. return pOut;
  394. };