basis.cpp 34 KB

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