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