basis.cpp 33 KB

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