basis.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /*************************************************************************/
  2. /* basis.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "basis.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/string/print_string.h"
  33. #define cofac(row1, col1, row2, col2) \
  34. (elements[row1][col1] * elements[row2][col2] - elements[row1][col2] * elements[row2][col1])
  35. void Basis::from_z(const Vector3 &p_z) {
  36. if (Math::abs(p_z.z) > Math_SQRT12) {
  37. // choose p in y-z plane
  38. real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
  39. real_t k = 1.0 / Math::sqrt(a);
  40. elements[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
  41. elements[1] = Vector3(a * k, -p_z[0] * elements[0][2], p_z[0] * elements[0][1]);
  42. } else {
  43. // choose p in x-y plane
  44. real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
  45. real_t k = 1.0 / Math::sqrt(a);
  46. elements[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
  47. elements[1] = Vector3(-p_z.z * elements[0].y, p_z.z * elements[0].x, a * k);
  48. }
  49. elements[2] = p_z;
  50. }
  51. void Basis::invert() {
  52. real_t co[3] = {
  53. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  54. };
  55. real_t det = elements[0][0] * co[0] +
  56. elements[0][1] * co[1] +
  57. elements[0][2] * co[2];
  58. #ifdef MATH_CHECKS
  59. ERR_FAIL_COND(det == 0);
  60. #endif
  61. real_t s = 1.0 / det;
  62. set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  63. co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  64. co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  65. }
  66. void Basis::orthonormalize() {
  67. // Gram-Schmidt Process
  68. Vector3 x = get_axis(0);
  69. Vector3 y = get_axis(1);
  70. Vector3 z = get_axis(2);
  71. x.normalize();
  72. y = (y - x * (x.dot(y)));
  73. y.normalize();
  74. z = (z - x * (x.dot(z)) - y * (y.dot(z)));
  75. z.normalize();
  76. set_axis(0, x);
  77. set_axis(1, y);
  78. set_axis(2, z);
  79. }
  80. Basis Basis::orthonormalized() const {
  81. Basis c = *this;
  82. c.orthonormalize();
  83. return c;
  84. }
  85. bool Basis::is_orthogonal() const {
  86. Basis identity;
  87. Basis m = (*this) * transposed();
  88. return m.is_equal_approx(identity);
  89. }
  90. bool Basis::is_diagonal() const {
  91. return (
  92. Math::is_zero_approx(elements[0][1]) && Math::is_zero_approx(elements[0][2]) &&
  93. Math::is_zero_approx(elements[1][0]) && Math::is_zero_approx(elements[1][2]) &&
  94. Math::is_zero_approx(elements[2][0]) && Math::is_zero_approx(elements[2][1]));
  95. }
  96. bool Basis::is_rotation() const {
  97. return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal();
  98. }
  99. #ifdef MATH_CHECKS
  100. // This method is only used once, in diagonalize. If it's desired elsewhere, feel free to remove the #ifdef.
  101. bool Basis::is_symmetric() const {
  102. if (!Math::is_equal_approx(elements[0][1], elements[1][0])) {
  103. return false;
  104. }
  105. if (!Math::is_equal_approx(elements[0][2], elements[2][0])) {
  106. return false;
  107. }
  108. if (!Math::is_equal_approx(elements[1][2], elements[2][1])) {
  109. return false;
  110. }
  111. return true;
  112. }
  113. #endif
  114. Basis Basis::diagonalize() {
  115. //NOTE: only implemented for symmetric matrices
  116. //with the Jacobi iterative method
  117. #ifdef MATH_CHECKS
  118. ERR_FAIL_COND_V(!is_symmetric(), Basis());
  119. #endif
  120. const int ite_max = 1024;
  121. real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2];
  122. int ite = 0;
  123. Basis acc_rot;
  124. while (off_matrix_norm_2 > CMP_EPSILON2 && ite++ < ite_max) {
  125. real_t el01_2 = elements[0][1] * elements[0][1];
  126. real_t el02_2 = elements[0][2] * elements[0][2];
  127. real_t el12_2 = elements[1][2] * elements[1][2];
  128. // Find the pivot element
  129. int i, j;
  130. if (el01_2 > el02_2) {
  131. if (el12_2 > el01_2) {
  132. i = 1;
  133. j = 2;
  134. } else {
  135. i = 0;
  136. j = 1;
  137. }
  138. } else {
  139. if (el12_2 > el02_2) {
  140. i = 1;
  141. j = 2;
  142. } else {
  143. i = 0;
  144. j = 2;
  145. }
  146. }
  147. // Compute the rotation angle
  148. real_t angle;
  149. if (Math::is_equal_approx(elements[j][j], elements[i][i])) {
  150. angle = Math_PI / 4;
  151. } else {
  152. angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i]));
  153. }
  154. // Compute the rotation matrix
  155. Basis rot;
  156. rot.elements[i][i] = rot.elements[j][j] = Math::cos(angle);
  157. rot.elements[i][j] = -(rot.elements[j][i] = Math::sin(angle));
  158. // Update the off matrix norm
  159. off_matrix_norm_2 -= elements[i][j] * elements[i][j];
  160. // Apply the rotation
  161. *this = rot * *this * rot.transposed();
  162. acc_rot = rot * acc_rot;
  163. }
  164. return acc_rot;
  165. }
  166. Basis Basis::inverse() const {
  167. Basis inv = *this;
  168. inv.invert();
  169. return inv;
  170. }
  171. void Basis::transpose() {
  172. SWAP(elements[0][1], elements[1][0]);
  173. SWAP(elements[0][2], elements[2][0]);
  174. SWAP(elements[1][2], elements[2][1]);
  175. }
  176. Basis Basis::transposed() const {
  177. Basis tr = *this;
  178. tr.transpose();
  179. return tr;
  180. }
  181. Basis Basis::from_scale(const Vector3 &p_scale) {
  182. return Basis(p_scale.x, 0, 0, 0, p_scale.y, 0, 0, 0, p_scale.z);
  183. }
  184. // Multiplies the matrix from left by the scaling matrix: M -> S.M
  185. // See the comment for Basis::rotated for further explanation.
  186. void Basis::scale(const Vector3 &p_scale) {
  187. elements[0][0] *= p_scale.x;
  188. elements[0][1] *= p_scale.x;
  189. elements[0][2] *= p_scale.x;
  190. elements[1][0] *= p_scale.y;
  191. elements[1][1] *= p_scale.y;
  192. elements[1][2] *= p_scale.y;
  193. elements[2][0] *= p_scale.z;
  194. elements[2][1] *= p_scale.z;
  195. elements[2][2] *= p_scale.z;
  196. }
  197. Basis Basis::scaled(const Vector3 &p_scale) const {
  198. Basis m = *this;
  199. m.scale(p_scale);
  200. return m;
  201. }
  202. void Basis::scale_local(const Vector3 &p_scale) {
  203. // performs a scaling in object-local coordinate system:
  204. // M -> (M.S.Minv).M = M.S.
  205. *this = scaled_local(p_scale);
  206. }
  207. float Basis::get_uniform_scale() const {
  208. return (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0;
  209. }
  210. void Basis::make_scale_uniform() {
  211. float l = (elements[0].length() + elements[1].length() + elements[2].length()) / 3.0;
  212. for (int i = 0; i < 3; i++) {
  213. elements[i].normalize();
  214. elements[i] *= l;
  215. }
  216. }
  217. Basis Basis::scaled_local(const Vector3 &p_scale) const {
  218. return (*this) * Basis::from_scale(p_scale);
  219. }
  220. Vector3 Basis::get_scale_abs() const {
  221. return Vector3(
  222. Vector3(elements[0][0], elements[1][0], elements[2][0]).length(),
  223. Vector3(elements[0][1], elements[1][1], elements[2][1]).length(),
  224. Vector3(elements[0][2], elements[1][2], elements[2][2]).length());
  225. }
  226. Vector3 Basis::get_scale_local() const {
  227. real_t det_sign = SGN(determinant());
  228. return det_sign * Vector3(elements[0].length(), elements[1].length(), elements[2].length());
  229. }
  230. // get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature.
  231. Vector3 Basis::get_scale() const {
  232. // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S.
  233. // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and
  234. // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal).
  235. //
  236. // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition
  237. // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where
  238. // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix,
  239. // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P,
  240. // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique.
  241. // Therefore, we are going to do this decomposition by sticking to a particular convention.
  242. // This may lead to confusion for some users though.
  243. //
  244. // The convention we use here is to absorb the sign flip into the scaling matrix.
  245. // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ...
  246. //
  247. // A proper way to get rid of this issue would be to store the scaling values (or at least their signs)
  248. // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the
  249. // matrix elements.
  250. //
  251. // The rotation part of this decomposition is returned by get_rotation* functions.
  252. real_t det_sign = SGN(determinant());
  253. return det_sign * get_scale_abs();
  254. }
  255. // Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S.
  256. // Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3.
  257. // This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so.
  258. Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const {
  259. #ifdef MATH_CHECKS
  260. ERR_FAIL_COND_V(determinant() == 0, Vector3());
  261. Basis m = transposed() * (*this);
  262. ERR_FAIL_COND_V(!m.is_diagonal(), Vector3());
  263. #endif
  264. Vector3 scale = get_scale();
  265. Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale
  266. rotref = (*this) * inv_scale;
  267. #ifdef MATH_CHECKS
  268. ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3());
  269. #endif
  270. return scale.abs();
  271. }
  272. // Multiplies the matrix from left by the rotation matrix: M -> R.M
  273. // Note that this does *not* rotate the matrix itself.
  274. //
  275. // The main use of Basis is as Transform.basis, which is used by the transformation matrix
  276. // of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
  277. // not the matrix itself (which is R * (*this) * R.transposed()).
  278. Basis Basis::rotated(const Vector3 &p_axis, real_t p_phi) const {
  279. return Basis(p_axis, p_phi) * (*this);
  280. }
  281. void Basis::rotate(const Vector3 &p_axis, real_t p_phi) {
  282. *this = rotated(p_axis, p_phi);
  283. }
  284. void Basis::rotate_local(const Vector3 &p_axis, real_t p_phi) {
  285. // performs a rotation in object-local coordinate system:
  286. // M -> (M.R.Minv).M = M.R.
  287. *this = rotated_local(p_axis, p_phi);
  288. }
  289. Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_phi) const {
  290. return (*this) * Basis(p_axis, p_phi);
  291. }
  292. Basis Basis::rotated(const Vector3 &p_euler) const {
  293. return Basis(p_euler) * (*this);
  294. }
  295. void Basis::rotate(const Vector3 &p_euler) {
  296. *this = rotated(p_euler);
  297. }
  298. Basis Basis::rotated(const Quaternion &p_quaternion) const {
  299. return Basis(p_quaternion) * (*this);
  300. }
  301. void Basis::rotate(const Quaternion &p_quaternion) {
  302. *this = rotated(p_quaternion);
  303. }
  304. Vector3 Basis::get_euler_normalized(EulerOrder p_order) const {
  305. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  306. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  307. // See the comment in get_scale() for further information.
  308. Basis m = orthonormalized();
  309. real_t det = m.determinant();
  310. if (det < 0) {
  311. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  312. m.scale(Vector3(-1, -1, -1));
  313. }
  314. return m.get_euler(p_order);
  315. }
  316. Quaternion Basis::get_rotation_quaternion() const {
  317. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  318. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  319. // See the comment in get_scale() for further information.
  320. Basis m = orthonormalized();
  321. real_t det = m.determinant();
  322. if (det < 0) {
  323. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  324. m.scale(Vector3(-1, -1, -1));
  325. }
  326. return m.get_quaternion();
  327. }
  328. void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction) {
  329. // Takes two vectors and rotates the basis from the first vector to the second vector.
  330. // Adopted from: https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
  331. const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
  332. if (axis.length_squared() != 0) {
  333. real_t dot = p_start_direction.dot(p_end_direction);
  334. dot = CLAMP(dot, -1.0, 1.0);
  335. const real_t angle_rads = Math::acos(dot);
  336. set_axis_angle(axis, angle_rads);
  337. }
  338. }
  339. void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
  340. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  341. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  342. // See the comment in get_scale() for further information.
  343. Basis m = orthonormalized();
  344. real_t det = m.determinant();
  345. if (det < 0) {
  346. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  347. m.scale(Vector3(-1, -1, -1));
  348. }
  349. m.get_axis_angle(p_axis, p_angle);
  350. }
  351. void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const {
  352. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  353. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  354. // See the comment in get_scale() for further information.
  355. Basis m = transposed();
  356. m.orthonormalize();
  357. real_t det = m.determinant();
  358. if (det < 0) {
  359. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  360. m.scale(Vector3(-1, -1, -1));
  361. }
  362. m.get_axis_angle(p_axis, p_angle);
  363. p_angle = -p_angle;
  364. }
  365. Vector3 Basis::get_euler(EulerOrder p_order) const {
  366. switch (p_order) {
  367. case EULER_ORDER_XYZ: {
  368. // Euler angles in XYZ convention.
  369. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  370. //
  371. // rot = cy*cz -cy*sz sy
  372. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  373. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  374. Vector3 euler;
  375. real_t sy = elements[0][2];
  376. if (sy < (1.0 - CMP_EPSILON)) {
  377. if (sy > -(1.0 - CMP_EPSILON)) {
  378. // is this a pure Y rotation?
  379. if (elements[1][0] == 0.0 && elements[0][1] == 0.0 && elements[1][2] == 0 && elements[2][1] == 0 && elements[1][1] == 1) {
  380. // return the simplest form (human friendlier in editor and scripts)
  381. euler.x = 0;
  382. euler.y = atan2(elements[0][2], elements[0][0]);
  383. euler.z = 0;
  384. } else {
  385. euler.x = Math::atan2(-elements[1][2], elements[2][2]);
  386. euler.y = Math::asin(sy);
  387. euler.z = Math::atan2(-elements[0][1], elements[0][0]);
  388. }
  389. } else {
  390. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  391. euler.y = -Math_PI / 2.0;
  392. euler.z = 0.0;
  393. }
  394. } else {
  395. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  396. euler.y = Math_PI / 2.0;
  397. euler.z = 0.0;
  398. }
  399. return euler;
  400. } break;
  401. case EULER_ORDER_XZY: {
  402. // Euler angles in XZY convention.
  403. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  404. //
  405. // rot = cz*cy -sz cz*sy
  406. // sx*sy+cx*cy*sz cx*cz cx*sz*sy-cy*sx
  407. // cy*sx*sz cz*sx cx*cy+sx*sz*sy
  408. Vector3 euler;
  409. real_t sz = elements[0][1];
  410. if (sz < (1.0 - CMP_EPSILON)) {
  411. if (sz > -(1.0 - CMP_EPSILON)) {
  412. euler.x = Math::atan2(elements[2][1], elements[1][1]);
  413. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  414. euler.z = Math::asin(-sz);
  415. } else {
  416. // It's -1
  417. euler.x = -Math::atan2(elements[1][2], elements[2][2]);
  418. euler.y = 0.0;
  419. euler.z = Math_PI / 2.0;
  420. }
  421. } else {
  422. // It's 1
  423. euler.x = -Math::atan2(elements[1][2], elements[2][2]);
  424. euler.y = 0.0;
  425. euler.z = -Math_PI / 2.0;
  426. }
  427. return euler;
  428. } break;
  429. case EULER_ORDER_YXZ: {
  430. // Euler angles in YXZ convention.
  431. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  432. //
  433. // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy
  434. // cx*sz cx*cz -sx
  435. // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx
  436. Vector3 euler;
  437. real_t m12 = elements[1][2];
  438. if (m12 < (1 - CMP_EPSILON)) {
  439. if (m12 > -(1 - CMP_EPSILON)) {
  440. // is this a pure X rotation?
  441. if (elements[1][0] == 0 && elements[0][1] == 0 && elements[0][2] == 0 && elements[2][0] == 0 && elements[0][0] == 1) {
  442. // return the simplest form (human friendlier in editor and scripts)
  443. euler.x = atan2(-m12, elements[1][1]);
  444. euler.y = 0;
  445. euler.z = 0;
  446. } else {
  447. euler.x = asin(-m12);
  448. euler.y = atan2(elements[0][2], elements[2][2]);
  449. euler.z = atan2(elements[1][0], elements[1][1]);
  450. }
  451. } else { // m12 == -1
  452. euler.x = Math_PI * 0.5;
  453. euler.y = atan2(elements[0][1], elements[0][0]);
  454. euler.z = 0;
  455. }
  456. } else { // m12 == 1
  457. euler.x = -Math_PI * 0.5;
  458. euler.y = -atan2(elements[0][1], elements[0][0]);
  459. euler.z = 0;
  460. }
  461. return euler;
  462. } break;
  463. case EULER_ORDER_YZX: {
  464. // Euler angles in YZX convention.
  465. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  466. //
  467. // rot = cy*cz sy*sx-cy*cx*sz cx*sy+cy*sz*sx
  468. // sz cz*cx -cz*sx
  469. // -cz*sy cy*sx+cx*sy*sz cy*cx-sy*sz*sx
  470. Vector3 euler;
  471. real_t sz = elements[1][0];
  472. if (sz < (1.0 - CMP_EPSILON)) {
  473. if (sz > -(1.0 - CMP_EPSILON)) {
  474. euler.x = Math::atan2(-elements[1][2], elements[1][1]);
  475. euler.y = Math::atan2(-elements[2][0], elements[0][0]);
  476. euler.z = Math::asin(sz);
  477. } else {
  478. // It's -1
  479. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  480. euler.y = 0.0;
  481. euler.z = -Math_PI / 2.0;
  482. }
  483. } else {
  484. // It's 1
  485. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  486. euler.y = 0.0;
  487. euler.z = Math_PI / 2.0;
  488. }
  489. return euler;
  490. } break;
  491. case EULER_ORDER_ZXY: {
  492. // Euler angles in ZXY convention.
  493. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  494. //
  495. // rot = cz*cy-sz*sx*sy -cx*sz cz*sy+cy*sz*sx
  496. // cy*sz+cz*sx*sy cz*cx sz*sy-cz*cy*sx
  497. // -cx*sy sx cx*cy
  498. Vector3 euler;
  499. real_t sx = elements[2][1];
  500. if (sx < (1.0 - CMP_EPSILON)) {
  501. if (sx > -(1.0 - CMP_EPSILON)) {
  502. euler.x = Math::asin(sx);
  503. euler.y = Math::atan2(-elements[2][0], elements[2][2]);
  504. euler.z = Math::atan2(-elements[0][1], elements[1][1]);
  505. } else {
  506. // It's -1
  507. euler.x = -Math_PI / 2.0;
  508. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  509. euler.z = 0;
  510. }
  511. } else {
  512. // It's 1
  513. euler.x = Math_PI / 2.0;
  514. euler.y = Math::atan2(elements[0][2], elements[0][0]);
  515. euler.z = 0;
  516. }
  517. return euler;
  518. } break;
  519. case EULER_ORDER_ZYX: {
  520. // Euler angles in ZYX convention.
  521. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  522. //
  523. // rot = cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*cy
  524. // cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx
  525. // -sy cy*sx cy*cx
  526. Vector3 euler;
  527. real_t sy = elements[2][0];
  528. if (sy < (1.0 - CMP_EPSILON)) {
  529. if (sy > -(1.0 - CMP_EPSILON)) {
  530. euler.x = Math::atan2(elements[2][1], elements[2][2]);
  531. euler.y = Math::asin(-sy);
  532. euler.z = Math::atan2(elements[1][0], elements[0][0]);
  533. } else {
  534. // It's -1
  535. euler.x = 0;
  536. euler.y = Math_PI / 2.0;
  537. euler.z = -Math::atan2(elements[0][1], elements[1][1]);
  538. }
  539. } else {
  540. // It's 1
  541. euler.x = 0;
  542. euler.y = -Math_PI / 2.0;
  543. euler.z = -Math::atan2(elements[0][1], elements[1][1]);
  544. }
  545. return euler;
  546. } break;
  547. default: {
  548. ERR_FAIL_V_MSG(Vector3(), "Invalid parameter for get_euler(order)");
  549. }
  550. }
  551. return Vector3();
  552. }
  553. void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) {
  554. real_t c, s;
  555. c = Math::cos(p_euler.x);
  556. s = Math::sin(p_euler.x);
  557. Basis xmat(1.0, 0.0, 0.0, 0.0, c, -s, 0.0, s, c);
  558. c = Math::cos(p_euler.y);
  559. s = Math::sin(p_euler.y);
  560. Basis ymat(c, 0.0, s, 0.0, 1.0, 0.0, -s, 0.0, c);
  561. c = Math::cos(p_euler.z);
  562. s = Math::sin(p_euler.z);
  563. Basis zmat(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0);
  564. switch (p_order) {
  565. case EULER_ORDER_XYZ: {
  566. *this = xmat * (ymat * zmat);
  567. } break;
  568. case EULER_ORDER_XZY: {
  569. *this = xmat * zmat * ymat;
  570. } break;
  571. case EULER_ORDER_YXZ: {
  572. *this = ymat * xmat * zmat;
  573. } break;
  574. case EULER_ORDER_YZX: {
  575. *this = ymat * zmat * xmat;
  576. } break;
  577. case EULER_ORDER_ZXY: {
  578. *this = zmat * xmat * ymat;
  579. } break;
  580. case EULER_ORDER_ZYX: {
  581. *this = zmat * ymat * xmat;
  582. } break;
  583. default: {
  584. ERR_FAIL_MSG("Invalid order parameter for set_euler(vec3,order)");
  585. }
  586. }
  587. }
  588. bool Basis::is_equal_approx(const Basis &p_basis) const {
  589. return elements[0].is_equal_approx(p_basis.elements[0]) && elements[1].is_equal_approx(p_basis.elements[1]) && elements[2].is_equal_approx(p_basis.elements[2]);
  590. }
  591. bool Basis::operator==(const Basis &p_matrix) const {
  592. for (int i = 0; i < 3; i++) {
  593. for (int j = 0; j < 3; j++) {
  594. if (elements[i][j] != p_matrix.elements[i][j]) {
  595. return false;
  596. }
  597. }
  598. }
  599. return true;
  600. }
  601. bool Basis::operator!=(const Basis &p_matrix) const {
  602. return (!(*this == p_matrix));
  603. }
  604. Basis::operator String() const {
  605. return "[X: " + get_axis(0).operator String() +
  606. ", Y: " + get_axis(1).operator String() +
  607. ", Z: " + get_axis(2).operator String() + "]";
  608. }
  609. Quaternion Basis::get_quaternion() const {
  610. #ifdef MATH_CHECKS
  611. ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
  612. #endif
  613. /* Allow getting a quaternion from an unnormalized transform */
  614. Basis m = *this;
  615. real_t trace = m.elements[0][0] + m.elements[1][1] + m.elements[2][2];
  616. real_t temp[4];
  617. if (trace > 0.0) {
  618. real_t s = Math::sqrt(trace + 1.0);
  619. temp[3] = (s * 0.5);
  620. s = 0.5 / s;
  621. temp[0] = ((m.elements[2][1] - m.elements[1][2]) * s);
  622. temp[1] = ((m.elements[0][2] - m.elements[2][0]) * s);
  623. temp[2] = ((m.elements[1][0] - m.elements[0][1]) * s);
  624. } else {
  625. int i = m.elements[0][0] < m.elements[1][1]
  626. ? (m.elements[1][1] < m.elements[2][2] ? 2 : 1)
  627. : (m.elements[0][0] < m.elements[2][2] ? 2 : 0);
  628. int j = (i + 1) % 3;
  629. int k = (i + 2) % 3;
  630. real_t s = Math::sqrt(m.elements[i][i] - m.elements[j][j] - m.elements[k][k] + 1.0);
  631. temp[i] = s * 0.5;
  632. s = 0.5 / s;
  633. temp[3] = (m.elements[k][j] - m.elements[j][k]) * s;
  634. temp[j] = (m.elements[j][i] + m.elements[i][j]) * s;
  635. temp[k] = (m.elements[k][i] + m.elements[i][k]) * s;
  636. }
  637. return Quaternion(temp[0], temp[1], temp[2], temp[3]);
  638. }
  639. static const Basis _ortho_bases[24] = {
  640. Basis(1, 0, 0, 0, 1, 0, 0, 0, 1),
  641. Basis(0, -1, 0, 1, 0, 0, 0, 0, 1),
  642. Basis(-1, 0, 0, 0, -1, 0, 0, 0, 1),
  643. Basis(0, 1, 0, -1, 0, 0, 0, 0, 1),
  644. Basis(1, 0, 0, 0, 0, -1, 0, 1, 0),
  645. Basis(0, 0, 1, 1, 0, 0, 0, 1, 0),
  646. Basis(-1, 0, 0, 0, 0, 1, 0, 1, 0),
  647. Basis(0, 0, -1, -1, 0, 0, 0, 1, 0),
  648. Basis(1, 0, 0, 0, -1, 0, 0, 0, -1),
  649. Basis(0, 1, 0, 1, 0, 0, 0, 0, -1),
  650. Basis(-1, 0, 0, 0, 1, 0, 0, 0, -1),
  651. Basis(0, -1, 0, -1, 0, 0, 0, 0, -1),
  652. Basis(1, 0, 0, 0, 0, 1, 0, -1, 0),
  653. Basis(0, 0, -1, 1, 0, 0, 0, -1, 0),
  654. Basis(-1, 0, 0, 0, 0, -1, 0, -1, 0),
  655. Basis(0, 0, 1, -1, 0, 0, 0, -1, 0),
  656. Basis(0, 0, 1, 0, 1, 0, -1, 0, 0),
  657. Basis(0, -1, 0, 0, 0, 1, -1, 0, 0),
  658. Basis(0, 0, -1, 0, -1, 0, -1, 0, 0),
  659. Basis(0, 1, 0, 0, 0, -1, -1, 0, 0),
  660. Basis(0, 0, 1, 0, -1, 0, 1, 0, 0),
  661. Basis(0, 1, 0, 0, 0, 1, 1, 0, 0),
  662. Basis(0, 0, -1, 0, 1, 0, 1, 0, 0),
  663. Basis(0, -1, 0, 0, 0, -1, 1, 0, 0)
  664. };
  665. int Basis::get_orthogonal_index() const {
  666. //could be sped up if i come up with a way
  667. Basis orth = *this;
  668. for (int i = 0; i < 3; i++) {
  669. for (int j = 0; j < 3; j++) {
  670. real_t v = orth[i][j];
  671. if (v > 0.5) {
  672. v = 1.0;
  673. } else if (v < -0.5) {
  674. v = -1.0;
  675. } else {
  676. v = 0;
  677. }
  678. orth[i][j] = v;
  679. }
  680. }
  681. for (int i = 0; i < 24; i++) {
  682. if (_ortho_bases[i] == orth) {
  683. return i;
  684. }
  685. }
  686. return 0;
  687. }
  688. void Basis::set_orthogonal_index(int p_index) {
  689. //there only exist 24 orthogonal bases in r3
  690. ERR_FAIL_INDEX(p_index, 24);
  691. *this = _ortho_bases[p_index];
  692. }
  693. void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  694. /* checking this is a bad idea, because obtaining from scaled transform is a valid use case
  695. #ifdef MATH_CHECKS
  696. ERR_FAIL_COND(!is_rotation());
  697. #endif
  698. */
  699. real_t angle, x, y, z; // variables for result
  700. real_t epsilon = 0.01; // margin to allow for rounding errors
  701. real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees
  702. if ((Math::abs(elements[1][0] - elements[0][1]) < epsilon) && (Math::abs(elements[2][0] - elements[0][2]) < epsilon) && (Math::abs(elements[2][1] - elements[1][2]) < epsilon)) {
  703. // singularity found
  704. // first check for identity matrix which must have +1 for all terms
  705. // in leading diagonal and zero in other terms
  706. if ((Math::abs(elements[1][0] + elements[0][1]) < epsilon2) && (Math::abs(elements[2][0] + elements[0][2]) < epsilon2) && (Math::abs(elements[2][1] + elements[1][2]) < epsilon2) && (Math::abs(elements[0][0] + elements[1][1] + elements[2][2] - 3) < epsilon2)) {
  707. // this singularity is identity matrix so angle = 0
  708. r_axis = Vector3(0, 1, 0);
  709. r_angle = 0;
  710. return;
  711. }
  712. // otherwise this singularity is angle = 180
  713. angle = Math_PI;
  714. real_t xx = (elements[0][0] + 1) / 2;
  715. real_t yy = (elements[1][1] + 1) / 2;
  716. real_t zz = (elements[2][2] + 1) / 2;
  717. real_t xy = (elements[1][0] + elements[0][1]) / 4;
  718. real_t xz = (elements[2][0] + elements[0][2]) / 4;
  719. real_t yz = (elements[2][1] + elements[1][2]) / 4;
  720. if ((xx > yy) && (xx > zz)) { // elements[0][0] is the largest diagonal term
  721. if (xx < epsilon) {
  722. x = 0;
  723. y = Math_SQRT12;
  724. z = Math_SQRT12;
  725. } else {
  726. x = Math::sqrt(xx);
  727. y = xy / x;
  728. z = xz / x;
  729. }
  730. } else if (yy > zz) { // elements[1][1] is the largest diagonal term
  731. if (yy < epsilon) {
  732. x = Math_SQRT12;
  733. y = 0;
  734. z = Math_SQRT12;
  735. } else {
  736. y = Math::sqrt(yy);
  737. x = xy / y;
  738. z = yz / y;
  739. }
  740. } else { // elements[2][2] is the largest diagonal term so base result on this
  741. if (zz < epsilon) {
  742. x = Math_SQRT12;
  743. y = Math_SQRT12;
  744. z = 0;
  745. } else {
  746. z = Math::sqrt(zz);
  747. x = xz / z;
  748. y = yz / z;
  749. }
  750. }
  751. r_axis = Vector3(x, y, z);
  752. r_angle = angle;
  753. return;
  754. }
  755. // as we have reached here there are no singularities so we can handle normally
  756. real_t s = Math::sqrt((elements[1][2] - elements[2][1]) * (elements[1][2] - elements[2][1]) + (elements[2][0] - elements[0][2]) * (elements[2][0] - elements[0][2]) + (elements[0][1] - elements[1][0]) * (elements[0][1] - elements[1][0])); // s=|axis||sin(angle)|, used to normalise
  757. angle = Math::acos((elements[0][0] + elements[1][1] + elements[2][2] - 1) / 2);
  758. if (angle < 0) {
  759. s = -s;
  760. }
  761. x = (elements[2][1] - elements[1][2]) / s;
  762. y = (elements[0][2] - elements[2][0]) / s;
  763. z = (elements[1][0] - elements[0][1]) / s;
  764. r_axis = Vector3(x, y, z);
  765. r_angle = angle;
  766. }
  767. void Basis::set_quaternion(const Quaternion &p_quaternion) {
  768. real_t d = p_quaternion.length_squared();
  769. real_t s = 2.0 / d;
  770. real_t xs = p_quaternion.x * s, ys = p_quaternion.y * s, zs = p_quaternion.z * s;
  771. real_t wx = p_quaternion.w * xs, wy = p_quaternion.w * ys, wz = p_quaternion.w * zs;
  772. real_t xx = p_quaternion.x * xs, xy = p_quaternion.x * ys, xz = p_quaternion.x * zs;
  773. real_t yy = p_quaternion.y * ys, yz = p_quaternion.y * zs, zz = p_quaternion.z * zs;
  774. set(1.0 - (yy + zz), xy - wz, xz + wy,
  775. xy + wz, 1.0 - (xx + zz), yz - wx,
  776. xz - wy, yz + wx, 1.0 - (xx + yy));
  777. }
  778. void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) {
  779. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
  780. #ifdef MATH_CHECKS
  781. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  782. #endif
  783. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  784. real_t cosine = Math::cos(p_phi);
  785. elements[0][0] = axis_sq.x + cosine * (1.0 - axis_sq.x);
  786. elements[1][1] = axis_sq.y + cosine * (1.0 - axis_sq.y);
  787. elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z);
  788. real_t sine = Math::sin(p_phi);
  789. real_t t = 1 - cosine;
  790. real_t xyzt = p_axis.x * p_axis.y * t;
  791. real_t zyxs = p_axis.z * sine;
  792. elements[0][1] = xyzt - zyxs;
  793. elements[1][0] = xyzt + zyxs;
  794. xyzt = p_axis.x * p_axis.z * t;
  795. zyxs = p_axis.y * sine;
  796. elements[0][2] = xyzt + zyxs;
  797. elements[2][0] = xyzt - zyxs;
  798. xyzt = p_axis.y * p_axis.z * t;
  799. zyxs = p_axis.x * sine;
  800. elements[1][2] = xyzt - zyxs;
  801. elements[2][1] = xyzt + zyxs;
  802. }
  803. void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_phi, const Vector3 &p_scale) {
  804. _set_diagonal(p_scale);
  805. rotate(p_axis, p_phi);
  806. }
  807. void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale) {
  808. _set_diagonal(p_scale);
  809. rotate(p_euler);
  810. }
  811. void Basis::set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale) {
  812. _set_diagonal(p_scale);
  813. rotate(p_quaternion);
  814. }
  815. // This also sets the non-diagonal elements to 0, which is misleading from the
  816. // name, so we want this method to be private. Use `from_scale` externally.
  817. void Basis::_set_diagonal(const Vector3 &p_diag) {
  818. elements[0][0] = p_diag.x;
  819. elements[0][1] = 0;
  820. elements[0][2] = 0;
  821. elements[1][0] = 0;
  822. elements[1][1] = p_diag.y;
  823. elements[1][2] = 0;
  824. elements[2][0] = 0;
  825. elements[2][1] = 0;
  826. elements[2][2] = p_diag.z;
  827. }
  828. Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
  829. //consider scale
  830. Quaternion from(*this);
  831. Quaternion to(p_to);
  832. Basis b(from.slerp(to, p_weight));
  833. b.elements[0] *= Math::lerp(elements[0].length(), p_to.elements[0].length(), p_weight);
  834. b.elements[1] *= Math::lerp(elements[1].length(), p_to.elements[1].length(), p_weight);
  835. b.elements[2] *= Math::lerp(elements[2].length(), p_to.elements[2].length(), p_weight);
  836. return b;
  837. }
  838. void Basis::rotate_sh(real_t *p_values) {
  839. // code by John Hable
  840. // http://filmicworlds.com/blog/simple-and-fast-spherical-harmonic-rotation/
  841. // this code is Public Domain
  842. const static real_t s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi))
  843. const static real_t s_c4 = -0.31539156525; // (-sqrt(5))/(4*sqrt(pi))
  844. const static real_t s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi))
  845. const static real_t s_c_scale = 1.0 / 0.91529123286551084;
  846. const static real_t s_c_scale_inv = 0.91529123286551084;
  847. const static real_t s_rc2 = 1.5853309190550713 * s_c_scale;
  848. const static real_t s_c4_div_c3 = s_c4 / s_c3;
  849. const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
  850. const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv;
  851. const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv;
  852. real_t src[9] = { p_values[0], p_values[1], p_values[2], p_values[3], p_values[4], p_values[5], p_values[6], p_values[7], p_values[8] };
  853. real_t m00 = elements[0][0];
  854. real_t m01 = elements[0][1];
  855. real_t m02 = elements[0][2];
  856. real_t m10 = elements[1][0];
  857. real_t m11 = elements[1][1];
  858. real_t m12 = elements[1][2];
  859. real_t m20 = elements[2][0];
  860. real_t m21 = elements[2][1];
  861. real_t m22 = elements[2][2];
  862. p_values[0] = src[0];
  863. p_values[1] = m11 * src[1] - m12 * src[2] + m10 * src[3];
  864. p_values[2] = -m21 * src[1] + m22 * src[2] - m20 * src[3];
  865. p_values[3] = m01 * src[1] - m02 * src[2] + m00 * src[3];
  866. real_t sh0 = src[7] + src[8] + src[8] - src[5];
  867. real_t sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8];
  868. real_t sh2 = src[4];
  869. real_t sh3 = -src[7];
  870. real_t sh4 = -src[5];
  871. // Rotations. R0 and R1 just use the raw matrix columns
  872. real_t r2x = m00 + m01;
  873. real_t r2y = m10 + m11;
  874. real_t r2z = m20 + m21;
  875. real_t r3x = m00 + m02;
  876. real_t r3y = m10 + m12;
  877. real_t r3z = m20 + m22;
  878. real_t r4x = m01 + m02;
  879. real_t r4y = m11 + m12;
  880. real_t r4z = m21 + m22;
  881. // dense matrix multiplication one column at a time
  882. // column 0
  883. real_t sh0_x = sh0 * m00;
  884. real_t sh0_y = sh0 * m10;
  885. real_t d0 = sh0_x * m10;
  886. real_t d1 = sh0_y * m20;
  887. real_t d2 = sh0 * (m20 * m20 + s_c4_div_c3);
  888. real_t d3 = sh0_x * m20;
  889. real_t d4 = sh0_x * m00 - sh0_y * m10;
  890. // column 1
  891. real_t sh1_x = sh1 * m02;
  892. real_t sh1_y = sh1 * m12;
  893. d0 += sh1_x * m12;
  894. d1 += sh1_y * m22;
  895. d2 += sh1 * (m22 * m22 + s_c4_div_c3);
  896. d3 += sh1_x * m22;
  897. d4 += sh1_x * m02 - sh1_y * m12;
  898. // column 2
  899. real_t sh2_x = sh2 * r2x;
  900. real_t sh2_y = sh2 * r2y;
  901. d0 += sh2_x * r2y;
  902. d1 += sh2_y * r2z;
  903. d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
  904. d3 += sh2_x * r2z;
  905. d4 += sh2_x * r2x - sh2_y * r2y;
  906. // column 3
  907. real_t sh3_x = sh3 * r3x;
  908. real_t sh3_y = sh3 * r3y;
  909. d0 += sh3_x * r3y;
  910. d1 += sh3_y * r3z;
  911. d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
  912. d3 += sh3_x * r3z;
  913. d4 += sh3_x * r3x - sh3_y * r3y;
  914. // column 4
  915. real_t sh4_x = sh4 * r4x;
  916. real_t sh4_y = sh4 * r4y;
  917. d0 += sh4_x * r4y;
  918. d1 += sh4_y * r4z;
  919. d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
  920. d3 += sh4_x * r4z;
  921. d4 += sh4_x * r4x - sh4_y * r4y;
  922. // extra multipliers
  923. p_values[4] = d0;
  924. p_values[5] = -d1;
  925. p_values[6] = d2 * s_scale_dst2;
  926. p_values[7] = -d3;
  927. p_values[8] = d4 * s_scale_dst4;
  928. }
  929. Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) {
  930. #ifdef MATH_CHECKS
  931. ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero.");
  932. ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero.");
  933. #endif
  934. Vector3 v_z = -p_target.normalized();
  935. Vector3 v_x = p_up.cross(v_z);
  936. #ifdef MATH_CHECKS
  937. ERR_FAIL_COND_V_MSG(v_x.is_equal_approx(Vector3()), Basis(), "The target vector and up vector can't be parallel to each other.");
  938. #endif
  939. v_x.normalize();
  940. Vector3 v_y = v_z.cross(v_x);
  941. Basis basis;
  942. basis.set(v_x, v_y, v_z);
  943. return basis;
  944. }