basis.cpp 33 KB

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