quaternion.js 14 KB

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