rotations.inc 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. #if defined _inc_rotation
  2. #undef _inc_rotation
  3. #endif
  4. #if defined _rotation_included
  5. #endinput
  6. #endif
  7. #define _rotation_included
  8. /*
  9. * rotation by Nero_3D (C) 2016
  10. *
  11. */
  12. #include <a_samp>
  13. // Reference
  14. // https://en.wikipedia.org/wiki/Euler_Angles#Rotation_matrix
  15. // https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
  16. // https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation
  17. // http://www.euclideanspace.com/maths/geometry/rotations/index.htm
  18. // https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
  19. // Right to left - absolute Rotation (active)
  20. // Left to right - relative Rotation (passive)
  21. enum eulermode {
  22. // Proper / Classic Euler angles
  23. euler_xzx,
  24. euler_xyx,
  25. euler_yxy,
  26. euler_yzy,
  27. euler_zyz,
  28. euler_zxz,
  29. // Tait-Bryan angles
  30. euler_xzy,
  31. euler_xyz,
  32. euler_yxz,
  33. euler_yzx,
  34. euler_zyx,
  35. euler_zxy // sa-mp // yaw pitch roll
  36. }
  37. const eulermode: euler_samp = euler_zxy;
  38. /*
  39. native
  40. native RotMatrixMatrixAroundRelPoint(Float: matrix1[4][4], Float: oX, Float: oY, Float: oZ, Float: matrix2[4][4])
  41. native RotMatrixQuatAroundRelPoint(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: w, Float: x, Float: y, Float: z)
  42. native RotMatrixEulerAroundRelPoint(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp)
  43. native RotMatrixAxisAroundRelPoint(Float: matrix1[4][4], Float: oX, Float: oY, Float: oZ, Float: angle, Float: aX, Float: aY, Float: aZ)
  44. native RotMatrixMatrixAroundPoint(Float: matrix1[4][4], Float: x, Float: y, Float: z, Float: matrix2[4][4])
  45. native RotMatrixQuatAroundPoint(Float: matrix[4][4], Float: x, Float: y, Float: z, Float: w, Float: qX, Float: qY, Float: qZ)
  46. native RotMatrixEulerAroundPoint(Float: matrix[4][4], Float: x, Float: y, Float: z, Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp)
  47. native RotMatrixAxisAroundPoint(Float: matrix1[4][4], Float: x, Float: y, Float: z, Float: angle, Float: aX, Float: aY, Float: aZ)
  48. native TranslateMatrix(Float: matrix[4][4], Float: x, Float: y, Float: z)
  49. native RotateMatrixWithMatrix(Float: matrix1[4][4], Float: matrix2[4][4])
  50. native RotateMatrixWithQuat(Float: matrix[4][4], Float: w, Float: x, Float: y, Float: z)
  51. native RotateMatrixWithEuler(Float: matrix[4][4], Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp)
  52. native RotateMatrixWithAxisAngle(Float: matrix[4][4], Float: angle, Float: aX, Float: aY, Float: aZ)
  53. native GetTranslationMatrix(Float: matrix[4][4], Float: x, Float: y, Float: z)
  54. native GetRotationMatrixFromQuat(Float: matrix[4][4], Float: w, Float: x, Float: y, Float: z)
  55. native GetRotationMatrixFromEuler(Float: matrix[4][4], Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp)
  56. native GetRotationMatrixFromAxisAngle(Float: matrix[4][4], Float: angle, Float: aX, Float: aY, Float: aZ)
  57. native GetQuatFromMatrix(Float: matrix[4][4], & Float: w, & Float: x, & Float: y, & Float: z)
  58. native GetQuatFromEuler(Float: rX, Float: rY, Float: rZ, & Float: w, & Float: x, & Float: y, & Float: z, eulermode: mode = euler_samp)
  59. native GetQuatFromAxisAngle(Float: angle, Float: aX, Float: aY, Float: aZ, & Float: w, & Float: x, & Float: y, & Float: z)
  60. native GetEulerFromMatrix(Float: matrix[4][4], & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp)
  61. native GetEulerFromQuat(Float: w, Float: x, Float: y, Float: z, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp)
  62. native GetEulerFromEuler(Float: oX, Float: oY, Float: oZ, eulermode: omode, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp)
  63. native GetEulerFromAxisAngle(Float: angle, Float: aX, Float: aY, Float: aZ, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp)
  64. native GetAxisAngleFromMatrix(Float: matrix[4][4], & Float: angle, & Float: aX, & Float: aY, & Float: aZ)
  65. native GetAxisAngleFromQuat(Float: w, Float: x, Float: y, Float: z, & Float: angle, & Float: aX, & Float: aY, & Float: aZ)
  66. native GetAxisAngleFromEuler(Float: rX, Float: rY, Float: rZ, & Float: angle, & Float: aX, & Float: aY, & Float: aZ, eulermode: mode = euler_samp)
  67. native MatrixRotate(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: oT, & Float: X, & Float: Y, & Float: Z)
  68. native QuatRotate(Float: w, Float: x, Float: y, Float: z, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z)
  69. native EulerRotate(Float: rX, Float: rY, Float: rZ, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z, eulermode: mode = euler_samp)
  70. native AxisAngleRotate(Float: angle, Float: aX, Float: aY, Float: aZ, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z)
  71. native GetVehicleObjectPositionWorld(vehicleid, Float: att_X, Float: att_Y, Float: att_Z, Float: att_rotX, Float: att_rotY, Float: att_rotZ, &Float: X, &Float: Y, &Float: Z, &Float: rotX, &Float: rotY, &Float: rotZ)
  72. native GetVehicleObjectPositionOffset(vehicleid, Float: X, Float: Y, Float: Z, Float: rotX, Float: rotY, Float: rotZ, &Float: att_X, &Float: att_Y, &Float: att_Z, &Float: att_rotX, &Float: att_rotY, &Float: att_rotZ)
  73. native
  74. */
  75. static // unroll variables for speed
  76. Float: m100, Float: m101, Float: m102, Float: m103,
  77. Float: m110, Float: m111, Float: m112, Float: m113,
  78. Float: m120, Float: m121, Float: m122, Float: m123,
  79. Float: m200, Float: m201, Float: m202, Float: m203,
  80. Float: m210, Float: m211, Float: m212, Float: m213,
  81. Float: m220, Float: m221, Float: m222, Float: m223
  82. ;
  83. stock RotMatrixMatrixAroundRelPoint(Float: matrix1[4][4], Float: oX, Float: oY, Float: oZ, Float: matrix2[4][4]) {
  84. new
  85. Float: X,
  86. Float: Y,
  87. Float: Z
  88. ;
  89. MatrixRotate(matrix1, oX, oY, oZ, 1.0, X, Y, Z);
  90. RotMatrixMatrixAroundPoint(matrix1, X, Y, Z, matrix2);
  91. }
  92. stock RotMatrixQuatAroundRelPoint(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: w, Float: x, Float: y, Float: z) {
  93. new
  94. Float: X,
  95. Float: Y,
  96. Float: Z
  97. ;
  98. MatrixRotate(matrix, oX, oY, oZ, 1.0, X, Y, Z);
  99. RotMatrixQuatAroundPoint(matrix, X, Y, Z, w, x, y, z);
  100. }
  101. stock RotMatrixEulerAroundRelPoint(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp) {
  102. new
  103. Float: X,
  104. Float: Y,
  105. Float: Z
  106. ;
  107. MatrixRotate(matrix, oX, oY, oZ, 1.0, X, Y, Z);
  108. RotMatrixEulerAroundPoint(matrix, X, Y, Z, rX, rY, rZ, mode);
  109. }
  110. stock RotMatrixAxisAroundRelPoint(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: angle, Float: aX, Float: aY, Float: aZ) {
  111. new
  112. Float: X,
  113. Float: Y,
  114. Float: Z
  115. ;
  116. MatrixRotate(matrix, oX, oY, oZ, 1.0, X, Y, Z);
  117. RotMatrixAxisAroundPoint(matrix, X, Y, Z, angle, aX, aY, aZ);
  118. }
  119. stock RotMatrixMatrixAroundPoint(Float: matrix1[4][4], Float: x, Float: y, Float: z, Float: matrix2[4][4]) {
  120. TranslateMatrix(matrix1, -x, -y, -z);
  121. RotateMatrixWithMatrix(matrix1, matrix2);
  122. TranslateMatrix(matrix1, x, y, z);
  123. }
  124. stock RotMatrixQuatAroundPoint(Float: matrix[4][4], Float: x, Float: y, Float: z, Float: w, Float: qX, Float: qY, Float: qZ) {
  125. TranslateMatrix(matrix, -x, -y, -z);
  126. RotateMatrixWithQuat(matrix, w, qX, qY, qZ);
  127. TranslateMatrix(matrix, x, y, z);
  128. }
  129. stock RotMatrixEulerAroundPoint(Float: matrix[4][4], Float: x, Float: y, Float: z, Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp) {
  130. TranslateMatrix(matrix, -x, -y, -z);
  131. RotateMatrixWithEuler(matrix, rX, rY, rZ, mode);
  132. TranslateMatrix(matrix, x, y, z);
  133. }
  134. stock RotMatrixAxisAroundPoint(Float: matrix1[4][4], Float: x, Float: y, Float: z, Float: angle, Float: aX, Float: aY, Float: aZ) {
  135. TranslateMatrix(matrix, -x, -y, -z);
  136. RotateMatrixWithAxisAngle(matrix, angle, aX, aY, aZ);
  137. TranslateMatrix(matrix, x, y, z);
  138. }
  139. stock TranslateMatrix(Float: matrix[4][4], Float: x, Float: y, Float: z) {
  140. matrix[0][3] += x; // impressive!
  141. matrix[1][3] += y;
  142. matrix[2][3] += z;
  143. }
  144. stock RotateMatrixWithMatrix(Float: matrix1[4][4], Float: matrix2[4][4]) { // matrix1 = matrix2 * matrix1
  145. // fill variables
  146. #emit load.s.pri matrix1
  147. #emit add.c 16
  148. #emit const.alt m200 // right
  149. #emit movs 48
  150. #emit load.s.pri matrix2
  151. #emit add.c 16
  152. #emit const.alt m100 // left
  153. #emit movs 48
  154. // optimsized for third line 0.0, 0.0, 0.0, 1.0
  155. matrix1[0][0] = m100 * m200 + m101 * m210 + m102 * m220;
  156. matrix1[0][1] = m100 * m201 + m101 * m211 + m102 * m221;
  157. matrix1[0][2] = m100 * m202 + m101 * m212 + m102 * m222;
  158. matrix1[0][3] = m100 * m203 + m101 * m213 + m102 * m223 + m103;
  159. matrix1[1][0] = m110 * m200 + m111 * m210 + m112 * m220;
  160. matrix1[1][1] = m110 * m201 + m111 * m211 + m112 * m221;
  161. matrix1[1][2] = m110 * m202 + m111 * m212 + m112 * m222;
  162. matrix1[1][3] = m110 * m203 + m111 * m213 + m112 * m223 + m113;
  163. matrix1[2][0] = m120 * m200 + m121 * m210 + m122 * m220;
  164. matrix1[2][1] = m120 * m201 + m121 * m211 + m122 * m221;
  165. matrix1[2][2] = m120 * m202 + m121 * m212 + m122 * m222;
  166. matrix1[2][3] = m120 * m203 + m121 * m213 + m122 * m223 + m123;
  167. }
  168. stock RotateMatrixWithQuat(Float: matrix[4][4], Float: w, Float: x, Float: y, Float: z) {
  169. new
  170. Float: tmp[4][4]
  171. ;
  172. GetRotationMatrixFromQuat(tmp, w, x, y, z);
  173. RotateMatrixWithMatrix(matrix, tmp);
  174. }
  175. stock RotateMatrixWithEuler(Float: matrix[4][4], Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp) {
  176. new
  177. Float: tmp[4][4]
  178. ;
  179. GetRotationMatrixFromEuler(tmp, rX, rY, rZ, mode);
  180. RotateMatrixWithMatrix(matrix, tmp);
  181. }
  182. stock RotateMatrixWithAxisAngle(Float: matrix[4][4], Float: angle, Float: aX, Float: aY, Float: aZ) {
  183. new
  184. Float: tmp[4][4]
  185. ;
  186. GetRotationMatrixFromAxisAngle(tmp, angle, aX, aY, aZ);
  187. RotateMatrixWithMatrix(matrix, tmp);
  188. }
  189. stock GetTranslationMatrix(Float: matrix[4][4], Float: x, Float: y, Float: z) {
  190. matrix[0][0] = 1.0;
  191. matrix[0][1] = 0.0;
  192. matrix[0][2] = 0.0;
  193. matrix[0][3] = x;
  194. matrix[1][0] = 0.0;
  195. matrix[1][1] = 1.0;
  196. matrix[1][2] = 0.0;
  197. matrix[1][3] = y;
  198. matrix[2][0] = 0.0;
  199. matrix[2][1] = 0.0;
  200. matrix[2][2] = 1.0;
  201. matrix[2][3] = z;
  202. matrix[3][0] = 0.0;
  203. matrix[3][1] = 0.0;
  204. matrix[3][2] = 0.0;
  205. matrix[3][3] = 1.0;
  206. }
  207. stock GetRotationMatrixFromQuat(Float: matrix[4][4], Float: w, Float: x, Float: y, Float: z) {
  208. new
  209. Float: x2 = x * x,
  210. Float: y2 = y * y,
  211. Float: z2 = z * z,
  212. Float: w2 = w * w,
  213. Float: xy = 2.0 * x * y,
  214. Float: xz = 2.0 * x * z,
  215. Float: yz = 2.0 * y * z,
  216. Float: wx = 2.0 * w * x,
  217. Float: wy = 2.0 * w * y,
  218. Float: wz = 2.0 * w * z;
  219. // rotation
  220. matrix[0][0] = w2 + x2 - y2 - z2;
  221. matrix[1][0] = xy - wz;
  222. matrix[2][0] = xz + wy;
  223. matrix[0][1] = xy + wz;
  224. matrix[1][1] = w2 - x2 + y2 - z2;
  225. matrix[2][1] = yz - wx;
  226. matrix[0][2] = xz - wy;
  227. matrix[1][2] = yz + wx;
  228. matrix[2][2] = w2 - x2 - y2 + z2;
  229. // translation
  230. matrix[0][3] = 0.0;
  231. matrix[1][3] = 0.0;
  232. matrix[2][3] = 0.0;
  233. matrix[3][0] = 0.0;
  234. matrix[3][1] = 0.0;
  235. matrix[3][2] = 0.0;
  236. matrix[3][3] = 1.0;
  237. }
  238. stock GetRotationMatrixFromEuler(Float: matrix[4][4], Float: rX, Float: rY, Float: rZ, eulermode: mode = euler_samp) {
  239. new // could be done with matrix multiplication but would be slower
  240. Float: cosX = floatcos(rX, degrees),
  241. Float: cosY = floatcos(rY, degrees),
  242. Float: cosZ = floatcos(rZ, degrees),
  243. Float: sinX = floatsin(rX, degrees),
  244. Float: sinY = floatsin(rY, degrees),
  245. Float: sinZ = floatsin(rZ, degrees);
  246. switch(mode) { // Proper Euler angles - 1(rX), 2(rY), 3(rZ)
  247. case euler_xzx: {
  248. matrix[0][0] = cosY;
  249. matrix[0][1] = -cosZ * sinY;
  250. matrix[0][2] = sinY * sinZ;
  251. matrix[1][0] = cosX * sinY;
  252. matrix[1][1] = cosX * cosY * cosZ - sinX * sinZ;
  253. matrix[1][2] = -cosZ * sinX - cosX * cosY * sinZ;
  254. matrix[2][0] = sinX * sinY;
  255. matrix[2][1] = cosX * sinZ + cosY * cosZ * sinX;
  256. matrix[2][2] = cosX * cosZ - cosY * sinX * sinZ;
  257. }
  258. case euler_xyx: {
  259. matrix[0][0] = cosY;
  260. matrix[0][1] = sinY * sinZ;
  261. matrix[0][2] = cosZ * sinY;
  262. matrix[1][0] = sinX * sinY;
  263. matrix[1][1] = cosX * cosZ - cosY * sinX * sinZ;
  264. matrix[1][2] = -cosX * sinZ - cosY * cosZ * sinX;
  265. matrix[2][0] = -cosX * sinY;
  266. matrix[2][1] = cosZ * sinX + cosX * cosY * sinZ;
  267. matrix[2][2] = cosX * cosY * cosZ - sinX * sinZ;
  268. }
  269. case euler_yxy: {
  270. matrix[0][0] = cosX * cosZ - cosY * sinX * sinZ;
  271. matrix[0][1] = sinX * sinY;
  272. matrix[0][2] = cosX * sinZ + cosY * cosZ * sinX;
  273. matrix[1][0] = sinY * sinZ;
  274. matrix[1][1] = cosY;
  275. matrix[1][2] = -cosZ * sinY;
  276. matrix[2][0] = -cosZ * sinX - cosX * cosY * sinZ;
  277. matrix[2][1] = cosX * sinY;
  278. matrix[2][2] = cosX * cosY * cosZ - sinX * sinZ;
  279. }
  280. case euler_yzy: {
  281. matrix[0][0] = cosX * cosY * cosZ - sinX * sinZ;
  282. matrix[0][1] = -cosX * sinY;
  283. matrix[0][2] = cosZ * sinX + cosX * cosY * sinZ;
  284. matrix[1][0] = cosZ * sinY;
  285. matrix[1][1] = cosY;
  286. matrix[1][2] = sinY * sinZ;
  287. matrix[2][0] = -cosX * sinZ - cosY * cosZ * sinX;
  288. matrix[2][1] = sinX * sinY;
  289. matrix[2][2] = cosX * cosZ - cosY * sinX * sinZ;
  290. }
  291. case euler_zyz: {
  292. matrix[0][0] = cosX * cosY * cosZ - sinX * sinZ;
  293. matrix[0][1] = -cosZ * sinX - cosX * cosY * sinZ;
  294. matrix[0][2] = cosX * sinY;
  295. matrix[1][0] = cosX * sinZ + cosY * cosZ * sinX;
  296. matrix[1][1] = cosX * cosZ - cosY * sinX * sinZ;
  297. matrix[1][2] = sinX * sinY;
  298. matrix[2][0] = -cosZ * sinY;
  299. matrix[2][1] = sinY * sinZ;
  300. matrix[2][2] = cosY;
  301. }
  302. case euler_zxz: {
  303. matrix[0][0] = cosX * cosZ - cosY * sinX * sinZ;
  304. matrix[0][1] = -cosX * sinZ - cosY * cosZ * sinX;
  305. matrix[0][2] = sinX * sinY;
  306. matrix[1][0] = cosZ * sinX + cosX * cosY * sinZ;
  307. matrix[1][1] = cosX * cosY * cosZ - sinX * sinZ;
  308. matrix[1][2] = -cosX * sinY;
  309. matrix[2][0] = sinY * sinZ;
  310. matrix[2][1] = cosZ * sinY;
  311. matrix[2][2] = cosY;
  312. } // Tait-Bryan angles - X(rX), Y(rY), Z(rZ)
  313. case euler_xzy: {
  314. matrix[0][0] = cosZ * cosY;
  315. matrix[0][1] = -sinZ;
  316. matrix[0][2] = cosZ * sinY;
  317. matrix[1][0] = sinX * sinY + cosX * cosY * sinZ;
  318. matrix[1][1] = cosX * cosZ;
  319. matrix[1][2] = cosX * sinZ * sinY - cosY * sinX;
  320. matrix[2][0] = cosY * sinX * sinZ - cosX * sinY;
  321. matrix[2][1] = cosZ * sinX;
  322. matrix[2][2] = cosX * cosY + sinX * sinZ * sinY;
  323. }
  324. case euler_xyz: {
  325. matrix[0][0] = cosY * cosZ;
  326. matrix[0][1] = -cosY * sinZ;
  327. matrix[0][2] = sinY;
  328. matrix[1][0] = cosX * sinZ + cosZ * sinX * sinY;
  329. matrix[1][1] = cosX * cosZ - sinX * sinY * sinZ;
  330. matrix[1][2] = -cosY * sinX;
  331. matrix[2][0] = sinX * sinZ - cosX * cosZ * sinY;
  332. matrix[2][1] = cosZ * sinX + cosX * sinY * sinZ;
  333. matrix[2][2] = cosX * cosY;
  334. }
  335. case euler_yxz: {
  336. matrix[0][0] = cosY * cosZ + sinY * sinX * sinZ;
  337. matrix[0][1] = cosZ * sinY * sinX - cosY * sinZ;
  338. matrix[0][2] = cosX * sinY;
  339. matrix[1][0] = cosX * sinZ;
  340. matrix[1][1] = cosX * cosZ;
  341. matrix[1][2] = -sinX;
  342. matrix[2][0] = cosY * sinX * sinZ - cosZ * sinY;
  343. matrix[2][1] = cosY * cosZ * sinX + sinY * sinZ;
  344. matrix[2][2] = cosY * cosX;
  345. }
  346. case euler_yzx: {
  347. matrix[0][0] = cosY * cosZ;
  348. matrix[0][1] = sinY * sinX - cosY * cosX * sinZ;
  349. matrix[0][2] = cosX * sinY + cosY * sinZ * sinX;
  350. matrix[1][0] = sinZ;
  351. matrix[1][1] = cosZ * cosX;
  352. matrix[1][2] = -cosZ * sinX;
  353. matrix[2][0] = -cosZ * sinY;
  354. matrix[2][1] = cosY * sinX + cosX * sinY * sinZ;
  355. matrix[2][2] = cosY * cosX - sinY * sinZ * sinX;
  356. }
  357. case euler_zyx: { // pitch roll yaw
  358. matrix[0][0] = cosZ * cosY;
  359. matrix[0][1] = cosZ * sinY * sinX - cosX * sinZ;
  360. matrix[0][2] = sinZ * sinX + cosZ * cosX * sinY;
  361. matrix[1][0] = cosY * sinZ;
  362. matrix[1][1] = cosZ * cosX + sinZ * sinY * sinX;
  363. matrix[1][2] = cosX * sinZ * sinY - cosZ * sinX;
  364. matrix[2][0] = -sinY;
  365. matrix[2][1] = cosY * sinX;
  366. matrix[2][2] = cosY * cosX;
  367. }
  368. case euler_zxy: { // sa-mp
  369. matrix[0][0] = cosZ * cosY - sinZ * sinX * sinY;
  370. matrix[0][1] = -sinZ * cosX;
  371. matrix[0][2] = cosZ * sinY + sinZ * sinX * cosY;
  372. matrix[1][0] = sinZ * cosY + cosZ * sinX * sinY;
  373. matrix[1][1] = cosZ * cosX;
  374. matrix[1][2] = sinZ * sinY - cosZ * sinX * cosY;
  375. matrix[2][0] = -cosX * sinY;
  376. matrix[2][1] = sinX;
  377. matrix[2][2] = cosX * cosY;
  378. }
  379. }
  380. matrix[0][3] = 0.0;
  381. matrix[1][3] = 0.0;
  382. matrix[2][3] = 0.0;
  383. matrix[3][0] = 0.0;
  384. matrix[3][1] = 0.0;
  385. matrix[3][2] = 0.0;
  386. matrix[3][3] = 1.0;
  387. }
  388. stock GetRotationMatrixFromAxisAngle(Float: matrix[4][4], Float: angle, Float: aX, Float: aY, Float: aZ) {
  389. new
  390. Float: cos = floatcos(angle, degrees),
  391. Float: sin = floatsin(angle, degrees),
  392. Float: t = 1.0 - cos;
  393. matrix[0][0] = t * aX * aX + cos;
  394. matrix[1][1] = t * aY * aY + cos;
  395. matrix[2][2] = t * aZ * aZ + cos;
  396. cos = t * aX * aY;
  397. matrix[0][1] = cos - sin * aZ;
  398. matrix[1][0] = cos + sin * aZ;
  399. cos = t * aX * aZ;
  400. matrix[0][2] = cos + sin * aY;
  401. matrix[2][0] = cos - sin * aY;
  402. cos = t * aY * aZ;
  403. matrix[1][2] = cos - sin * aX;
  404. matrix[2][1] = cos + sin * aX;
  405. matrix[0][3] = 0.0;
  406. matrix[1][3] = 0.0;
  407. matrix[2][3] = 0.0;
  408. matrix[3][0] = 0.0;
  409. matrix[3][1] = 0.0;
  410. matrix[3][2] = 0.0;
  411. matrix[3][3] = 1.0;
  412. }
  413. // can result in NaN (1.0 + matrix[0][0] + matrix[1][1] + matrix[2][2]) could be <= 0.0
  414. stock GetQuatFromMatrix(Float: matrix[4][4], & Float: w, & Float: x, & Float: y, & Float: z) {
  415. #emit load.s.pri matrix
  416. #emit add.c 16
  417. #emit const.alt m100
  418. #emit movs 44
  419. z = -4.0 * (w = floatsqroot(1.0 + m100 + m111 + m122) / 2.0);
  420. x = (m121 - m112) / z;
  421. y = (m102 - m120) / z;
  422. z = (m110 - m101) / z;
  423. #emit retn // neccessary to ensure what no variable gets removed because it isn't used
  424. #emit const.pri m103
  425. #emit const.pri m113
  426. // #emit const.pri m123 - not needed thus only 44 instead of 48 bytes to copy
  427. }
  428. stock GetQuatFromEuler(Float: rX, Float: rY, Float: rZ, & Float: w, & Float: x, & Float: y, & Float: z, eulermode: mode = euler_samp) {
  429. rX /= 2.0; // could be done with multiplication but would be slower
  430. rY /= 2.0;
  431. rZ /= 2.0;
  432. new Float: cosX = floatcos(rX, degrees),
  433. Float: cosY = floatcos(rY, degrees),
  434. Float: cosZ = floatcos(rZ, degrees),
  435. Float: sinX = floatsin(rX, degrees),
  436. Float: sinY = floatsin(rY, degrees),
  437. Float: sinZ = floatsin(rZ, degrees);
  438. switch(mode) {
  439. case euler_xzx: {
  440. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  441. x = cosX * cosY * sinZ + sinX * cosY * cosZ;
  442. y = cosX * sinY * sinZ - sinX * sinY * cosZ;
  443. z = cosX * sinY * cosZ + sinX * sinY * sinZ;
  444. }
  445. case euler_xyx: {
  446. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  447. x = cosX * cosY * sinZ + sinX * cosY * cosZ;
  448. y = cosX * sinY * cosZ + sinX * sinY * sinZ;
  449. z = sinX * sinY * cosZ - cosX * sinY * sinZ;
  450. }
  451. case euler_yxy: {
  452. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  453. x = cosX * sinY * cosZ + sinX * sinY * sinZ;
  454. y = cosX * cosY * sinZ + sinX * cosY * cosZ;
  455. z = cosX * sinY * sinZ - sinX * sinY * cosZ;
  456. }
  457. case euler_yzy: {
  458. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  459. x = sinX * sinY * cosZ - cosX * sinY * sinZ;
  460. y = cosX * cosY * sinZ + sinX * cosY * cosZ;
  461. z = cosX * sinY * cosZ + sinX * sinY * sinZ;
  462. }
  463. case euler_zyz: {
  464. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  465. x = cosX * sinY * sinZ - sinX * sinY * cosZ;
  466. y = cosX * sinY * cosZ + sinX * sinY * sinZ;
  467. z = cosX * cosY * sinZ + sinX * cosY * cosZ;
  468. }
  469. case euler_zxz: {
  470. w = sinX * cosY * sinZ - cosX * cosY * cosZ;
  471. x = cosX * sinY * cosZ + sinX * sinY * sinZ;
  472. y = sinX * sinY * cosZ - cosX * sinY * sinZ;
  473. z = cosX * cosY * sinZ + sinX * cosY * cosZ;
  474. }
  475. case euler_xzy: {
  476. w = cosX * cosY * cosZ + sinX * sinY * sinZ;
  477. x = cosX * sinY * sinZ - sinX * cosY * cosZ;
  478. y = sinX * cosY * sinZ - cosX * sinY * cosZ;
  479. z = -(cosX * cosY * sinZ + sinX * sinY * cosZ);
  480. }
  481. case euler_xyz: {
  482. w = sinX * sinY * sinZ - cosX * cosY * cosZ;
  483. x = sinX * cosY * cosZ + cosX * sinY * sinZ;
  484. y = cosX * sinY * cosZ - sinX * cosY * sinZ;
  485. z = cosX * cosY * sinZ + sinX * sinY * cosZ;
  486. }
  487. case euler_yxz: {
  488. w = cosX * cosY * cosZ + sinX * sinY * sinZ;
  489. x = -(sinX * cosY * cosZ + cosX * sinY * sinZ);
  490. y = sinX * cosY * sinZ - cosX * sinY * cosZ;
  491. z = sinX * sinY * cosZ - cosX * cosY * sinZ;
  492. }
  493. case euler_yzx: {
  494. w = sinX * sinY * sinZ - cosX * cosY * cosZ;
  495. x = sinX * cosY * cosZ + cosX * sinY * sinZ;
  496. y = cosX * sinY * cosZ + sinX * cosY * sinZ;
  497. z = cosX * cosY * sinZ - sinX * sinY * cosZ;
  498. }
  499. case euler_zyx: {
  500. w = cosX * cosY * cosZ + sinX * sinY * sinZ;
  501. x = cosX * sinY * sinZ - sinX * cosY * cosZ;
  502. y = -(cosX * sinY * cosZ + sinX * cosY * sinZ);
  503. z = sinX * sinY * cosZ - cosX * cosY * sinZ;
  504. }
  505. case euler_zxy: {
  506. w = sinX * sinY * sinZ - cosX * cosY * cosZ;
  507. x = sinX * cosY * cosZ - cosX * sinY * sinZ;
  508. y = cosX * sinY * cosZ + sinX * cosY * sinZ;
  509. z = cosX * cosY * sinZ + sinX * sinY * cosZ;
  510. }
  511. }
  512. }
  513. stock GetQuatFromAxisAngle(Float: angle, Float: aX, Float: aY, Float: aZ, & Float: w, & Float: x, & Float: y, & Float: z) {
  514. angle /= -2.0;
  515. w = floatsin(angle, degrees) / VectorSize(aX, aY, aZ);
  516. x = aX * w;
  517. y = aY * w;
  518. z = aZ * w;
  519. w = floatcos(angle, degrees);
  520. }
  521. stock GetEulerFromMatrix(Float: matrix[4][4], & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp) {
  522. switch(mode) {
  523. case euler_xzx: {
  524. rY = matrix[0][0];
  525. if(rY > 0.9999) {
  526. rX = atan2(matrix[2][1], matrix[1][1]); // -12, 22
  527. rY = 0.0;
  528. rZ = 0.0;
  529. } else if(rY < -0.9999) {
  530. rX = atan2(matrix[2][1], matrix[1][1]);
  531. rY = 180.0;
  532. rZ = 0.0;
  533. } else {
  534. rX = atan2(matrix[2][0], matrix[1][0]);
  535. rY = acos(rY);
  536. rZ = atan2(matrix[0][2], -matrix[0][1]);
  537. }
  538. }
  539. case euler_xyx: {
  540. rY = matrix[0][0];
  541. if(rY > 0.9999) {
  542. rX = atan2(matrix[2][1], matrix[1][1]); // -12, 22
  543. rY = 0.0;
  544. rZ = 0.0;
  545. } else if(rY < -0.9999) {
  546. rX = atan2(matrix[2][1], matrix[1][1]);
  547. rY = 180.0;
  548. rZ = 0.0;
  549. } else {
  550. rX = atan2(matrix[1][0], -matrix[2][0]);
  551. rY = acos(rY);
  552. rZ = atan2(matrix[0][1], matrix[0][2]);
  553. }
  554. }
  555. case euler_yxy: {
  556. rY = matrix[1][1];
  557. if(rY > 0.9999) {
  558. rX = atan2(matrix[0][2], matrix[2][2]); // -20, 00
  559. rY = 0.0;
  560. rZ = 0.0;
  561. } else if(rY < -0.9999) {
  562. rX = atan2(matrix[0][2], matrix[2][2]);
  563. rY = 180.0;
  564. rZ = 0.0;
  565. } else {
  566. rX = atan2(matrix[0][1], matrix[2][1]);
  567. rY = acos(rY);
  568. rZ = atan2(matrix[1][0], -matrix[1][2]);
  569. }
  570. }
  571. case euler_yzy: {
  572. rY = matrix[1][1];
  573. if(rY > 0.9999) {
  574. rX = atan2(matrix[0][2], matrix[2][2]); // -20, 00
  575. rY = 0.0;
  576. rZ = 0.0;
  577. } else if(rY < -0.9999) {
  578. rX = atan2(matrix[0][2], matrix[2][2]);
  579. rY = 180.0;
  580. rZ = 0.0;
  581. } else {
  582. rX = atan2(matrix[2][1], -matrix[0][1]);
  583. rY = acos(rY);
  584. rZ = atan2(matrix[1][2], matrix[1][0]);
  585. }
  586. }
  587. case euler_zyz: {
  588. rY = matrix[2][2];
  589. if(rY > 0.9999) {
  590. rX = atan2(matrix[1][0], matrix[0][0]); // -01, 11
  591. rY = 0.0;
  592. rZ = 0.0;
  593. } else if(rY < -0.9999) {
  594. rX = atan2(matrix[1][0], matrix[0][0]);
  595. rY = 180.0;
  596. rZ = 0.0;
  597. } else {
  598. rX = atan2(matrix[1][2], matrix[0][2]);
  599. rY = acos(rY);
  600. rZ = atan2(matrix[2][1], -matrix[2][0]);
  601. }
  602. }
  603. case euler_zxz: {
  604. rY = matrix[2][2];
  605. if(rY > 0.9999) {
  606. rX = atan2(matrix[1][0], matrix[0][0]); // -01, 11
  607. rY = 0.0;
  608. rZ = 0.0;
  609. } else if(rY < -0.9999) {
  610. rX = atan2(matrix[1][0], matrix[0][0]);
  611. rY = 180.0;
  612. rZ = 0.0;
  613. } else {
  614. rX = atan2(matrix[0][2], -matrix[1][2]);
  615. rY = acos(rY);
  616. rZ = atan2(matrix[2][0], matrix[2][1]);
  617. }
  618. }
  619. case euler_xzy: {
  620. rZ = matrix[0][1];
  621. if(rZ < -0.9999) {
  622. rX = atan2(matrix[2][0], matrix[1][0]); // -12, 22
  623. rY = 0.0;
  624. rZ = 90.0;
  625. } else if(rZ > 0.9999) {
  626. rX = atan2(matrix[2][0], matrix[1][0]);
  627. rY = 0.0;
  628. rZ = -90.0;
  629. } else {
  630. rX = atan2(matrix[2][1], matrix[1][1]);
  631. rY = atan2(matrix[0][2], matrix[0][0]);
  632. rZ = -asin(rZ);
  633. }
  634. }
  635. case euler_xyz: {
  636. rY = matrix[0][2];
  637. if(rY > 0.9999) {
  638. rX = atan2(matrix[2][1], matrix[1][1]); // 10, -20
  639. rY = 90.0;
  640. rZ = 0.0;
  641. } else if(rY < -0.9999) {
  642. rX = atan2(matrix[2][1], matrix[1][1]);
  643. rY = -90.0;
  644. rZ = 0.0;
  645. } else {
  646. rX = -atan2(matrix[1][2], matrix[2][2]);
  647. rY = asin(rY);
  648. rZ = -atan2(matrix[0][1], matrix[0][0]);
  649. }
  650. }
  651. case euler_yxz: {
  652. rX = matrix[1][2];
  653. if(rX < -0.9999) {
  654. rX = 90.0;
  655. rY = atan2(matrix[0][1], matrix[2][1]); // -20, 00
  656. rZ = 0.0;
  657. } else if(rX > 0.9999) {
  658. rX = -90.0;
  659. rY = atan2(matrix[0][1], matrix[2][1]);
  660. rZ = 0.0;
  661. } else {
  662. rX = -asin(rX);
  663. rY = atan2(matrix[0][2], matrix[2][2]);
  664. rZ = atan2(matrix[1][0], matrix[1][1]);
  665. }
  666. }
  667. case euler_yzx: {
  668. rZ = matrix[1][0];
  669. if(rZ > 0.9999) {
  670. rX = 0.0;
  671. rY = atan2(matrix[0][2], matrix[2][2]); // 21, -01
  672. rZ = 90.0;
  673. } else if(rZ < -0.9999) {
  674. rX = 0.0;
  675. rY = atan2(matrix[0][2], matrix[2][2]);
  676. rZ = -90.0;
  677. } else {
  678. rX = -atan2(matrix[1][2], matrix[1][1]);
  679. rY = -atan2(matrix[2][0], matrix[0][0]);
  680. rZ = asin(rZ);
  681. }
  682. }
  683. case euler_zyx: { // pitch roll yaw
  684. rY = matrix[2][0];
  685. if(rY < -0.9999) {
  686. rX = 0.0;
  687. rY = 90.0;
  688. rZ = atan2(matrix[1][2], matrix[0][2]); // -01, 11
  689. } else if(rY > 0.9999) {
  690. rX = 0.0;
  691. rY = -90.0;
  692. rZ = atan2(matrix[1][2], matrix[0][2]);
  693. } else {
  694. rX = atan2(matrix[2][1], matrix[2][2]);
  695. rY = -asin(rY);
  696. rZ = atan2(matrix[1][0], matrix[0][0]);
  697. }
  698. }
  699. case euler_zxy: { // sa-mp
  700. rX = matrix[2][1]; // singularitie at rX = +- 90.0
  701. if(rX > 0.9999) { // ~ 89.2 degrees
  702. rX = 90.0;
  703. rY = 0.0;
  704. rZ = atan2(matrix[1][0], matrix[0][0]); // 02, -12
  705. } else if(rX < -0.9999) {
  706. rX = -90.0;
  707. rY = 0.0;
  708. rZ = atan2(matrix[1][0], matrix[0][0]);
  709. } else { // it does work without the singularities but it gets inaccurate
  710. rX = asin(rX);
  711. rY = -atan2(matrix[2][0], matrix[2][2]);
  712. rZ = -atan2(matrix[0][1], matrix[1][1]);
  713. }
  714. }
  715. }
  716. }
  717. stock GetEulerFromQuat(Float: w, Float: x, Float: y, Float: z, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp) {
  718. switch(mode) {
  719. case euler_xzx: {
  720. rY = w * w + x * x - y * y - z * z;
  721. if(rY > 0.9999) {
  722. rX = atan2(2.0 * (y * z - w * x), w * w - x * x + y * y - z * z);
  723. rY = 0.0;
  724. rZ = 0.0;
  725. } else if(rY < -0.9999) {
  726. rX = atan2(2.0 * (y * z - w * x), w * w - x * x + y * y - z * z);
  727. rY = 180.0;
  728. rZ = 0.0;
  729. } else {
  730. rX = atan2(x * z + w * y, x * y - w * z);
  731. rY = acos(rY);
  732. rZ = atan2(x * z - w * y, -(x * y + w * z));
  733. }
  734. }
  735. case euler_xyx: {
  736. rY = w * w + x * x - y * y - z * z;
  737. if(rY > 0.9999) {
  738. rX = atan2(2.0 * (y * z - w * x), w * w - x * x + y * y - z * z);
  739. rY = 0.0;
  740. rZ = 0.0;
  741. } else if(rY < -0.9999) {
  742. rX = atan2(2.0 * (y * z - w * x), w * w - x * x + y * y - z * z);
  743. rY = 180.0;
  744. rZ = 0.0;
  745. } else {
  746. rX = atan2(x * y - w * z, -(x * z + w * y));
  747. rY = acos(rY);
  748. rZ = atan2(x * y + w * z, x * z - w * y);
  749. }
  750. }
  751. case euler_yxy: {
  752. rY = w * w - x * x + y * y - z * z;
  753. if(rY > 0.9999) {
  754. rX = atan2(2.0 * (x * z - w * y), w * w - x * x - y * y + z * z);
  755. rY = 0.0;
  756. rZ = 0.0;
  757. } else if(rY < -0.9999) {
  758. rX = atan2(2.0 * (x * z - w * y), w * w - x * x - y * y + z * z);
  759. rY = 180.0;
  760. rZ = 0.0;
  761. } else {
  762. rX = atan2(x * y + w * z, y * z - w * x);
  763. rY = acos(rY);
  764. rZ = atan2(x * y - w * z, -(y * z + w * x));
  765. }
  766. }
  767. case euler_yzy: {
  768. rY = w * w - x * x + y * y - z * z;
  769. if(rY > 0.9999) {
  770. rX = atan2(2.0 * (x * z - w * y), w * w - x * x - y * y + z * z);
  771. rY = 0.0;
  772. rZ = 0.0;
  773. } else if(rY < -0.9999) {
  774. rX = atan2(2.0 * (x * z - w * y), w * w - x * x - y * y + z * z);
  775. rY = 180.0;
  776. rZ = 0.0;
  777. } else {
  778. rX = atan2(y * z - w * x, -(x * y + w * z));
  779. rY = acos(rY);
  780. rZ = atan2(y * z + w * x, x * y - w * z);
  781. }
  782. }
  783. case euler_zyz: {
  784. rY = w * w - x * x - y * y + z * z;
  785. if(rY > 0.9999) {
  786. rX = atan2(2.0 * (x * y - w * z), w * w + x * x - y * y - z * z);
  787. rY = 0.0;
  788. rZ = 0.0;
  789. } else if(rY < -0.9999) {
  790. rX = atan2(2.0 * (x * y - w * z), w * w + x * x - y * y - z * z);
  791. rY = 180.0;
  792. rZ = 0.0;
  793. } else {
  794. rX = atan2(y * z + w * x, x * z - w * y);
  795. rY = acos(rY);
  796. rZ = atan2(y * z - w * x, -(x * z + w * y));
  797. }
  798. }
  799. case euler_zxz: {
  800. rY = w * w - x * x - y * y + z * z;
  801. if(rY > 0.9999) {
  802. rX = atan2(2.0 * (x * y - w * z), w * w + x * x - y * y - z * z);
  803. rY = 0.0;
  804. rZ = 0.0;
  805. } else if(rY < -0.9999) {
  806. rX = atan2(2.0 * (x * y - w * z), w * w + x * x - y * y - z * z);
  807. rY = 180.0;
  808. rZ = 0.0;
  809. } else {
  810. rX = atan2(x * z - w * y, -(y * z + w * x));
  811. rY = acos(rY);
  812. rZ = atan2(x * z + w * y, y * z - w * x);
  813. }
  814. }
  815. case euler_xzy: {
  816. rZ = x * y + w * z;
  817. if(rZ < -0.49995) {
  818. rX = atan2(x * z + w * y, x * y - w * z);
  819. rY = 0.0;
  820. rZ = 90.0;
  821. } else if(rZ > 0.49995) {
  822. rX = atan2(x * z + w * y, x * y - w * z);
  823. rY = 0.0;
  824. rZ = -90.0;
  825. } else {
  826. mode = eulermode: (w * w - z * z);
  827. rY = x * x - y * y;
  828. rX = atan2(2.0 * (y * z - w * x), Float: mode - rY);
  829. rY = atan2(2.0 * (x * z - w * y), Float: mode + rY);
  830. rZ = asin(-2.0 * rZ);
  831. }
  832. }
  833. case euler_xyz: {
  834. rY = x * z - w * y;
  835. if(rY > 0.49995) {
  836. rX = atan2(w * x - y * z, x * z + w * y);
  837. rY = 90.0;
  838. rZ = 0.0;
  839. } else if(rY < -0.49995) {
  840. rX = atan2(y * z - w * x, x * z + w * y);
  841. rY = -90.0;
  842. rZ = 0.0;
  843. } else {
  844. mode = eulermode: (w * w - y * y);
  845. rZ = x * x - z * z;
  846. rX = atan2(-2.0 * (y * z + w * x), Float: mode - rZ);
  847. rY = asin(2.0 * rY);
  848. rZ = atan2(-2.0 * (x * y + w * z), Float: mode + rZ);
  849. }
  850. }
  851. case euler_yxz: {
  852. rX = y * z + w * x;
  853. if(rX < -0.49995) {
  854. rX = 90.0;
  855. rY = atan2(x * y + w * z, y * z - w * x);
  856. rZ = 0.0;
  857. } else if(rX > 0.49995) {
  858. rX = -90.0;
  859. rY = atan2(x * y + w * z, y * z - w * x);
  860. rZ = 0.0;
  861. } else {
  862. mode = eulermode: (w * w - x * x);
  863. rZ = y * y - z * z;
  864. rX = asin(-2.0 * rX);
  865. rY = atan2(2.0 * (x * z - w * y), Float: mode - rZ);
  866. rZ = atan2(2.0 * (x * y - w * z), Float: mode + rZ);
  867. }
  868. }
  869. case euler_yzx: {
  870. rZ = x * y - w * z;
  871. if(rZ > 0.49995) {
  872. rX = 0.0;
  873. rY = atan2(w * y - x * z, x * y + w * z);
  874. rZ = 90.0;
  875. } else if(rZ < -0.49995) {
  876. rX = 0.0;
  877. rY = atan2(x * z - w * y, x * y + w * z);
  878. rZ = -90.0;
  879. } else {
  880. mode = eulermode: (w * w - z * z);
  881. rY = x * x - y * y;
  882. rX = atan2(-2.0 * (y * z + w * x), Float: mode - rY);
  883. rY = atan2(-2.0 * (x * z + w * y), Float: mode + rY);
  884. rZ = asin(2.0 * rZ);
  885. }
  886. }
  887. case euler_zyx: { // pitch roll yaw
  888. rY = x * z + w * y;
  889. if(rY < -0.49995) {
  890. rX = 0.0;
  891. rY = 90.0;
  892. rZ = atan2(y * z + w * x, x * z - w * y);
  893. } else if(rY > 0.49995) {
  894. rX = 0.0;
  895. rY = -90.0;
  896. rZ = atan2(y * z + w * x, x * z - w * y);
  897. } else {
  898. mode = eulermode: (w * w - y * y);
  899. rZ = x * x - z * z;
  900. rX = atan2(2.0 * (y * z - w * x), Float: mode - rZ);
  901. rY = asin(-2.0 * rY);
  902. rZ = atan2(2.0 * (x * y - w * z), Float: mode + rZ);
  903. }
  904. }
  905. case euler_zxy: { // sa-mp
  906. rX = y * z - w * x;
  907. if(rX > 0.49995) {
  908. rY = 0.0;
  909. rX = 90.0;
  910. rZ = atan2(w * y - x * z, y * z + w * x);
  911. } else if(rX < -0.49995) {
  912. rY = 0.0;
  913. rX = -90.0;
  914. rZ = atan2(w * y - x * z, y * z + w * x);
  915. } else {
  916. mode = eulermode: (w * w - x * x);
  917. rZ = y * y - z * z;
  918. rX = asin(2.0 * rX);
  919. rY = atan2(-2.0 * (x * z + w * y), Float: mode - rZ);
  920. rZ = atan2(-2.0 * (x * y + w * z), Float: mode + rZ);
  921. }
  922. }
  923. }
  924. }
  925. stock GetEulerFromEuler(Float: oX, Float: oY, Float: oZ, eulermode: omode, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp) {
  926. GetQuatFromEuler(oX, oY, oZ, rX, oX, oY, oZ, omode); // to much work I guess
  927. GetEulerFromQuat(rX, oX, oY, oZ, rX, rY, rZ, mode);
  928. }
  929. stock GetEulerFromAxisAngle(Float: angle, Float: aX, Float: aY, Float: aZ, & Float: rX, & Float: rY, & Float: rZ, eulermode: mode = euler_samp) {
  930. new Float: matrix[4][4]; // leaving that unoptimised, see no use for it
  931. GetRotationMatrixFromAxisAngle(matrix, angle, aX, aY, aZ);
  932. GetEulerFromMatrix(matrix, rX, rY, rZ, mode);
  933. }
  934. stock GetAxisAngleFromMatrix(Float: matrix[4][4], & Float: angle, & Float: aX, & Float: aY, & Float: aZ) {
  935. #emit load.s.pri matrix
  936. #emit add.c 16
  937. #emit const.alt m100
  938. #emit movs 44
  939. aX = m121 - m112; // does have untriggered singularities
  940. aY = m102 - m120;
  941. aZ = m110 - m101;
  942. angle = VectorSize(aX, aY, aZ);
  943. aX /= angle;
  944. aY /= angle;
  945. aZ /= angle;
  946. angle = acos((m100 + m111 + m122 - 1.0) / 2.0);
  947. #emit retn // neccessary to ensure what no variable gets removed because it isn't used
  948. #emit const.pri m103
  949. #emit const.pri m113
  950. // #emit const.pri m123
  951. }
  952. stock GetAxisAngleFromQuat(Float: w, Float: x, Float: y, Float: z, & Float: angle, & Float: aX, & Float: aY, & Float: aZ) {
  953. angle = 2.0 * acos(-w);
  954. w = VectorSize(x, y, z); // w = floatsqroot(1.0 - w * w); // w = floatsin(angle, degrees); // |sin(angle)|
  955. if(w == 0.0) { // no rotation
  956. aX = 1.0;
  957. aY = 0.0;
  958. aZ = 0.0;
  959. angle = 0.0;
  960. } else {
  961. aX = x / w;
  962. aY = y / w;
  963. aZ = z / w;
  964. }
  965. }
  966. stock GetAxisAngleFromEuler(Float: rX, Float: rY, Float: rZ, & Float: angle, & Float: aX, & Float: aY, & Float: aZ, eulermode: mode = euler_samp) {
  967. GetQuatFromEuler(rX, rY, rZ, angle, aX, aY, aZ, mode);
  968. GetAxisAngleFromQuat(angle, aX, aY, aZ, angle, aX, aY, aZ);
  969. }
  970. stock MatrixRotate(Float: matrix[4][4], Float: oX, Float: oY, Float: oZ, Float: oT, & Float: X, & Float: Y, & Float: Z) {
  971. #emit load.s.pri matrix
  972. #emit add.c 16
  973. #emit const.alt m100
  974. #emit movs 48
  975. // oT = Translation, 0.0 for no translation, 1.0 for translation
  976. X += oX * m100 + oY * m101 + oZ * m102 + oT * m103; // M * v | v = [oX, oY, oZ]
  977. Y += oX * m110 + oY * m111 + oZ * m112 + oT * m113;
  978. Z += oX * m120 + oY * m121 + oZ * m122 + oT * m123;
  979. }
  980. stock QuatRotate(Float: w, Float: x, Float: y, Float: z, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z) {
  981. // wikipedia suggest this formula (a x b - cross prudct)
  982. // v + 2*r x (r x v + w*v) | q = w,r
  983. // 15 muls, 15 adds, 9 saves, 42 loads
  984. // new
  985. // Float: tX = y * oZ - z * oY - w * oX,
  986. // Float: tY = z * oX - x * oZ - w * oY,
  987. // Float: tZ = x * oY - y * oX - w * oZ;
  988. // x += x;
  989. // y += y;
  990. // z += z;
  991. // X += oX + y * tZ - z * tY;
  992. // Y += oY + z * tX - x * tZ;
  993. // Z += oZ + x * tY - y * tX;
  994. // ---
  995. // but I used this one because it more compact (a.b - dot product)
  996. // 2*((r.v)*r + (0.5 - (|r.r|^2))*v + w * (r x v)) | q = w,r
  997. // 22 muls, 12 adds, 5 saves, 38 loads
  998. new
  999. Float: dot = x * oX + y * oY + z * oZ,
  1000. Float: abs = w * w - 0.5;
  1001. X += 2.0 * (dot * x + abs * oX + w * (z * oY - y * oZ));
  1002. Y += 2.0 * (dot * y + abs * oY + w * (x * oZ - z * oX));
  1003. Z += 2.0 * (dot * z + abs * oZ + w * (y * oX - x * oY));
  1004. }
  1005. stock EulerRotate(Float: rX, Float: rY, Float: rZ, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z, eulermode: mode = euler_samp) {
  1006. // saddly I couldn't find a way to rotate euler angles without a step in between, is there even a way?
  1007. GetQuatFromEuler(rX, rY, rZ, Float: mode, rX, rY, rZ, mode);
  1008. QuatRotate(Float: mode, rX, rY, rZ, oX, oY, oZ, X, Y, Z);
  1009. }
  1010. stock AxisAngleRotate(Float: angle, Float: aX, Float: aY, Float: aZ, Float: oX, Float: oY, Float: oZ, & Float: X, & Float: Y, & Float: Z) {
  1011. new // uses Rodrigues rotation formula, similar to QuatRotate
  1012. Float: cos = floatcos(angle, degrees),
  1013. Float: sin = floatsin(angle, degrees),
  1014. Float: ts = (1.0 - cos) * (aX * oX + aY * oY + aZ * oZ);
  1015. // cos * v + sin * (e x v) + (1 - cos) * (e.v) * e | e = [aX, aY, aZ] v = [oX, oY, oZ]
  1016. X += ts * aX + cos * oX + sin * (aY * oZ - aZ * oY);
  1017. Y += ts * aY + cos * oY + sin * (aZ * oX - aX * oZ);
  1018. Z += ts * aZ + cos * oZ + sin * (aX * oY - aY * oX);
  1019. }