|
@@ -29,31 +29,17 @@
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
|
|
|
|
#include <godot_cpp/core/error_macros.hpp>
|
|
#include <godot_cpp/core/error_macros.hpp>
|
|
|
|
+#include <godot_cpp/core/math.hpp>
|
|
#include <godot_cpp/variant/basis.hpp>
|
|
#include <godot_cpp/variant/basis.hpp>
|
|
#include <godot_cpp/variant/string.hpp>
|
|
#include <godot_cpp/variant/string.hpp>
|
|
|
|
|
|
|
|
+using namespace godot;
|
|
|
|
+
|
|
#define cofac(row1, col1, row2, col2) \
|
|
#define cofac(row1, col1, row2, col2) \
|
|
(rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])
|
|
(rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])
|
|
|
|
|
|
namespace godot {
|
|
namespace godot {
|
|
|
|
|
|
-void Basis::from_z(const Vector3 &p_z) {
|
|
|
|
- if (Math::abs(p_z.z) > (real_t)Math_SQRT12) {
|
|
|
|
- // choose p in y-z plane
|
|
|
|
- real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
|
|
|
|
- real_t k = 1.0f / Math::sqrt(a);
|
|
|
|
- rows[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
|
|
|
|
- rows[1] = Vector3(a * k, -p_z[0] * rows[0][2], p_z[0] * rows[0][1]);
|
|
|
|
- } else {
|
|
|
|
- // choose p in x-y plane
|
|
|
|
- real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
|
|
|
|
- real_t k = 1.0f / Math::sqrt(a);
|
|
|
|
- rows[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
|
|
|
|
- rows[1] = Vector3(-p_z.z * rows[0].y, p_z.z * rows[0].x, a * k);
|
|
|
|
- }
|
|
|
|
- rows[2] = p_z;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
void Basis::invert() {
|
|
void Basis::invert() {
|
|
real_t co[3] = {
|
|
real_t co[3] = {
|
|
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
|
|
cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
|
|
@@ -107,13 +93,35 @@ Basis Basis::orthogonalized() const {
|
|
return c;
|
|
return c;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Returns true if the basis vectors are orthogonal (perpendicular), so it has no skew or shear, and can be decomposed into rotation and scale.
|
|
|
|
+// See https://en.wikipedia.org/wiki/Orthogonal_basis
|
|
bool Basis::is_orthogonal() const {
|
|
bool Basis::is_orthogonal() const {
|
|
- Basis identity;
|
|
|
|
- Basis m = (*this) * transposed();
|
|
|
|
|
|
+ const Vector3 x = get_column(0);
|
|
|
|
+ const Vector3 y = get_column(1);
|
|
|
|
+ const Vector3 z = get_column(2);
|
|
|
|
+ return Math::is_zero_approx(x.dot(y)) && Math::is_zero_approx(x.dot(z)) && Math::is_zero_approx(y.dot(z));
|
|
|
|
+}
|
|
|
|
|
|
- return m.is_equal_approx(identity);
|
|
|
|
|
|
+// Returns true if the basis vectors are orthonormal (orthogonal and normalized), so it has no scale, skew, or shear.
|
|
|
|
+// See https://en.wikipedia.org/wiki/Orthonormal_basis
|
|
|
|
+bool Basis::is_orthonormal() const {
|
|
|
|
+ const Vector3 x = get_column(0);
|
|
|
|
+ const Vector3 y = get_column(1);
|
|
|
|
+ const Vector3 z = get_column(2);
|
|
|
|
+ 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));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Returns true if the basis is conformal (orthogonal, uniform scale, preserves angles and distance ratios).
|
|
|
|
+// See https://en.wikipedia.org/wiki/Conformal_linear_transformation
|
|
|
|
+bool Basis::is_conformal() const {
|
|
|
|
+ const Vector3 x = get_column(0);
|
|
|
|
+ const Vector3 y = get_column(1);
|
|
|
|
+ const Vector3 z = get_column(2);
|
|
|
|
+ const real_t x_len_sq = x.length_squared();
|
|
|
|
+ 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));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Returns true if the basis only has diagonal elements, so it may only have scale or flip, but no rotation, skew, or shear.
|
|
bool Basis::is_diagonal() const {
|
|
bool Basis::is_diagonal() const {
|
|
return (
|
|
return (
|
|
Math::is_zero_approx(rows[0][1]) && Math::is_zero_approx(rows[0][2]) &&
|
|
Math::is_zero_approx(rows[0][1]) && Math::is_zero_approx(rows[0][2]) &&
|
|
@@ -121,8 +129,9 @@ bool Basis::is_diagonal() const {
|
|
Math::is_zero_approx(rows[2][0]) && Math::is_zero_approx(rows[2][1]));
|
|
Math::is_zero_approx(rows[2][0]) && Math::is_zero_approx(rows[2][1]));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// Returns true if the basis is a pure rotation matrix, so it has no scale, skew, shear, or flip.
|
|
bool Basis::is_rotation() const {
|
|
bool Basis::is_rotation() const {
|
|
- return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal();
|
|
|
|
|
|
+ return is_conformal() && Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON);
|
|
}
|
|
}
|
|
|
|
|
|
#ifdef MATH_CHECKS
|
|
#ifdef MATH_CHECKS
|
|
@@ -257,29 +266,26 @@ void Basis::scale_orthogonal(const Vector3 &p_scale) {
|
|
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
|
|
Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
|
|
Basis m = *this;
|
|
Basis m = *this;
|
|
Vector3 s = Vector3(-1, -1, -1) + p_scale;
|
|
Vector3 s = Vector3(-1, -1, -1) + p_scale;
|
|
|
|
+ bool sign = std::signbit(s.x + s.y + s.z);
|
|
|
|
+ Basis b = m.orthonormalized();
|
|
|
|
+ s = b.xform_inv(s);
|
|
Vector3 dots;
|
|
Vector3 dots;
|
|
- Basis b;
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
for (int i = 0; i < 3; i++) {
|
|
for (int j = 0; j < 3; j++) {
|
|
for (int j = 0; j < 3; j++) {
|
|
- dots[j] += s[i] * Math::abs(m.get_column(i).normalized().dot(b.get_column(j)));
|
|
|
|
|
|
+ dots[j] += s[i] * abs(m.get_column(i).normalized().dot(b.get_column(j)));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ if (sign != std::signbit(dots.x + dots.y + dots.z)) {
|
|
|
|
+ dots = -dots;
|
|
|
|
+ }
|
|
m.scale_local(Vector3(1, 1, 1) + dots);
|
|
m.scale_local(Vector3(1, 1, 1) + dots);
|
|
return m;
|
|
return m;
|
|
}
|
|
}
|
|
|
|
|
|
-float Basis::get_uniform_scale() const {
|
|
|
|
|
|
+real_t Basis::get_uniform_scale() const {
|
|
return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
|
|
return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
|
|
}
|
|
}
|
|
|
|
|
|
-void Basis::make_scale_uniform() {
|
|
|
|
- float l = (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
|
|
|
|
- for (int i = 0; i < 3; i++) {
|
|
|
|
- rows[i].normalize();
|
|
|
|
- rows[i] *= l;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
Basis Basis::scaled_local(const Vector3 &p_scale) const {
|
|
Basis Basis::scaled_local(const Vector3 &p_scale) const {
|
|
return (*this) * Basis::from_scale(p_scale);
|
|
return (*this) * Basis::from_scale(p_scale);
|
|
}
|
|
}
|
|
@@ -291,7 +297,7 @@ Vector3 Basis::get_scale_abs() const {
|
|
Vector3(rows[0][2], rows[1][2], rows[2][2]).length());
|
|
Vector3(rows[0][2], rows[1][2], rows[2][2]).length());
|
|
}
|
|
}
|
|
|
|
|
|
-Vector3 Basis::get_scale_local() const {
|
|
|
|
|
|
+Vector3 Basis::get_scale_global() const {
|
|
real_t det_sign = SIGN(determinant());
|
|
real_t det_sign = SIGN(determinant());
|
|
return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
|
|
return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
|
|
}
|
|
}
|
|
@@ -418,7 +424,7 @@ void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction)
|
|
real_t dot = p_start_direction.dot(p_end_direction);
|
|
real_t dot = p_start_direction.dot(p_end_direction);
|
|
dot = CLAMP(dot, -1.0f, 1.0f);
|
|
dot = CLAMP(dot, -1.0f, 1.0f);
|
|
const real_t angle_rads = Math::acos(dot);
|
|
const real_t angle_rads = Math::acos(dot);
|
|
- set_axis_angle(axis, angle_rads);
|
|
|
|
|
|
+ *this = Basis(axis, angle_rads) * (*this);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -453,8 +459,13 @@ void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) cons
|
|
}
|
|
}
|
|
|
|
|
|
Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
+ // This epsilon value results in angles within a +/- 0.04 degree range being simplified/truncated.
|
|
|
|
+ // Based on testing, this is the largest the epsilon can be without the angle truncation becoming
|
|
|
|
+ // visually noticeable.
|
|
|
|
+ const real_t epsilon = 0.00000025;
|
|
|
|
+
|
|
switch (p_order) {
|
|
switch (p_order) {
|
|
- case EULER_ORDER_XYZ: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_XYZ: {
|
|
// Euler angles in XYZ convention.
|
|
// Euler angles in XYZ convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -464,8 +475,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
|
|
Vector3 euler;
|
|
Vector3 euler;
|
|
real_t sy = rows[0][2];
|
|
real_t sy = rows[0][2];
|
|
- if (sy < (1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (sy < (1.0f - epsilon)) {
|
|
|
|
+ if (sy > -(1.0f - epsilon)) {
|
|
// is this a pure Y rotation?
|
|
// is this a pure Y rotation?
|
|
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
|
|
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
|
|
// return the simplest form (human friendlier in editor and scripts)
|
|
// return the simplest form (human friendlier in editor and scripts)
|
|
@@ -489,7 +500,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
}
|
|
}
|
|
return euler;
|
|
return euler;
|
|
}
|
|
}
|
|
- case EULER_ORDER_XZY: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_XZY: {
|
|
// Euler angles in XZY convention.
|
|
// Euler angles in XZY convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -499,8 +510,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
|
|
Vector3 euler;
|
|
Vector3 euler;
|
|
real_t sz = rows[0][1];
|
|
real_t sz = rows[0][1];
|
|
- if (sz < (1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (sz < (1.0f - epsilon)) {
|
|
|
|
+ if (sz > -(1.0f - epsilon)) {
|
|
euler.x = Math::atan2(rows[2][1], rows[1][1]);
|
|
euler.x = Math::atan2(rows[2][1], rows[1][1]);
|
|
euler.y = Math::atan2(rows[0][2], rows[0][0]);
|
|
euler.y = Math::atan2(rows[0][2], rows[0][0]);
|
|
euler.z = Math::asin(-sz);
|
|
euler.z = Math::asin(-sz);
|
|
@@ -518,7 +529,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
}
|
|
}
|
|
return euler;
|
|
return euler;
|
|
}
|
|
}
|
|
- case EULER_ORDER_YXZ: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_YXZ: {
|
|
// Euler angles in YXZ convention.
|
|
// Euler angles in YXZ convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -530,8 +541,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
|
|
real_t m12 = rows[1][2];
|
|
real_t m12 = rows[1][2];
|
|
|
|
|
|
- if (m12 < (1 - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (m12 > -(1 - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (m12 < (1 - epsilon)) {
|
|
|
|
+ if (m12 > -(1 - epsilon)) {
|
|
// is this a pure X rotation?
|
|
// is this a pure X rotation?
|
|
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
|
|
if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
|
|
// return the simplest form (human friendlier in editor and scripts)
|
|
// return the simplest form (human friendlier in editor and scripts)
|
|
@@ -556,7 +567,7 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
|
|
return euler;
|
|
return euler;
|
|
}
|
|
}
|
|
- case EULER_ORDER_YZX: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_YZX: {
|
|
// Euler angles in YZX convention.
|
|
// Euler angles in YZX convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -566,8 +577,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
|
|
|
|
Vector3 euler;
|
|
Vector3 euler;
|
|
real_t sz = rows[1][0];
|
|
real_t sz = rows[1][0];
|
|
- if (sz < (1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (sz < (1.0f - epsilon)) {
|
|
|
|
+ if (sz > -(1.0f - epsilon)) {
|
|
euler.x = Math::atan2(-rows[1][2], rows[1][1]);
|
|
euler.x = Math::atan2(-rows[1][2], rows[1][1]);
|
|
euler.y = Math::atan2(-rows[2][0], rows[0][0]);
|
|
euler.y = Math::atan2(-rows[2][0], rows[0][0]);
|
|
euler.z = Math::asin(sz);
|
|
euler.z = Math::asin(sz);
|
|
@@ -584,8 +595,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
euler.z = Math_PI / 2.0f;
|
|
euler.z = Math_PI / 2.0f;
|
|
}
|
|
}
|
|
return euler;
|
|
return euler;
|
|
- }
|
|
|
|
- case EULER_ORDER_ZXY: {
|
|
|
|
|
|
+ } break;
|
|
|
|
+ case EulerOrder::EULER_ORDER_ZXY: {
|
|
// Euler angles in ZXY convention.
|
|
// Euler angles in ZXY convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -594,8 +605,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
// -cx*sy sx cx*cy
|
|
// -cx*sy sx cx*cy
|
|
Vector3 euler;
|
|
Vector3 euler;
|
|
real_t sx = rows[2][1];
|
|
real_t sx = rows[2][1];
|
|
- if (sx < (1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (sx > -(1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (sx < (1.0f - epsilon)) {
|
|
|
|
+ if (sx > -(1.0f - epsilon)) {
|
|
euler.x = Math::asin(sx);
|
|
euler.x = Math::asin(sx);
|
|
euler.y = Math::atan2(-rows[2][0], rows[2][2]);
|
|
euler.y = Math::atan2(-rows[2][0], rows[2][2]);
|
|
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
|
|
euler.z = Math::atan2(-rows[0][1], rows[1][1]);
|
|
@@ -612,8 +623,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
euler.z = 0;
|
|
euler.z = 0;
|
|
}
|
|
}
|
|
return euler;
|
|
return euler;
|
|
- }
|
|
|
|
- case EULER_ORDER_ZYX: {
|
|
|
|
|
|
+ } break;
|
|
|
|
+ case EulerOrder::EULER_ORDER_ZYX: {
|
|
// Euler angles in ZYX convention.
|
|
// Euler angles in ZYX convention.
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
// See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
|
|
//
|
|
//
|
|
@@ -622,8 +633,8 @@ Vector3 Basis::get_euler(EulerOrder p_order) const {
|
|
// -sy cy*sx cy*cx
|
|
// -sy cy*sx cy*cx
|
|
Vector3 euler;
|
|
Vector3 euler;
|
|
real_t sy = rows[2][0];
|
|
real_t sy = rows[2][0];
|
|
- if (sy < (1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
- if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
|
|
|
|
|
|
+ if (sy < (1.0f - epsilon)) {
|
|
|
|
+ if (sy > -(1.0f - epsilon)) {
|
|
euler.x = Math::atan2(rows[2][1], rows[2][2]);
|
|
euler.x = Math::atan2(rows[2][1], rows[2][2]);
|
|
euler.y = Math::asin(-sy);
|
|
euler.y = Math::asin(-sy);
|
|
euler.z = Math::atan2(rows[1][0], rows[0][0]);
|
|
euler.z = Math::atan2(rows[1][0], rows[0][0]);
|
|
@@ -664,26 +675,26 @@ void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) {
|
|
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
|
|
Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
|
|
|
|
|
|
switch (p_order) {
|
|
switch (p_order) {
|
|
- case EULER_ORDER_XYZ: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_XYZ: {
|
|
*this = xmat * (ymat * zmat);
|
|
*this = xmat * (ymat * zmat);
|
|
} break;
|
|
} break;
|
|
- case EULER_ORDER_XZY: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_XZY: {
|
|
*this = xmat * zmat * ymat;
|
|
*this = xmat * zmat * ymat;
|
|
} break;
|
|
} break;
|
|
- case EULER_ORDER_YXZ: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_YXZ: {
|
|
*this = ymat * xmat * zmat;
|
|
*this = ymat * xmat * zmat;
|
|
} break;
|
|
} break;
|
|
- case EULER_ORDER_YZX: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_YZX: {
|
|
*this = ymat * zmat * xmat;
|
|
*this = ymat * zmat * xmat;
|
|
} break;
|
|
} break;
|
|
- case EULER_ORDER_ZXY: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_ZXY: {
|
|
*this = zmat * xmat * ymat;
|
|
*this = zmat * xmat * ymat;
|
|
} break;
|
|
} break;
|
|
- case EULER_ORDER_ZYX: {
|
|
|
|
|
|
+ case EulerOrder::EULER_ORDER_ZYX: {
|
|
*this = zmat * ymat * xmat;
|
|
*this = zmat * ymat * xmat;
|
|
} break;
|
|
} break;
|
|
default: {
|
|
default: {
|
|
- ERR_FAIL_MSG("Invalid order parameter for set_euler(vec3,order)");
|
|
|
|
|
|
+ ERR_FAIL_MSG("Invalid Euler order parameter.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -720,7 +731,7 @@ Basis::operator String() const {
|
|
|
|
|
|
Quaternion Basis::get_quaternion() const {
|
|
Quaternion Basis::get_quaternion() const {
|
|
#ifdef MATH_CHECKS
|
|
#ifdef MATH_CHECKS
|
|
- 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.");
|
|
|
|
|
|
+ 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.");
|
|
#endif
|
|
#endif
|
|
/* Allow getting a quaternion from an unnormalized transform */
|
|
/* Allow getting a quaternion from an unnormalized transform */
|
|
Basis m = *this;
|
|
Basis m = *this;
|
|
@@ -828,8 +839,8 @@ void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
|
|
z = (rows[1][0] - rows[0][1]) / s;
|
|
z = (rows[1][0] - rows[0][1]) / s;
|
|
|
|
|
|
r_axis = Vector3(x, y, z);
|
|
r_axis = Vector3(x, y, z);
|
|
- // CLAMP to avoid NaN if the value passed to acos is not in [0,1].
|
|
|
|
- r_angle = Math::acos(CLAMP((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2, (real_t)0.0, (real_t)1.0));
|
|
|
|
|
|
+ // acos does clamping.
|
|
|
|
+ r_angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2);
|
|
}
|
|
}
|
|
|
|
|
|
void Basis::set_quaternion(const Quaternion &p_quaternion) {
|
|
void Basis::set_quaternion(const Quaternion &p_quaternion) {
|
|
@@ -847,7 +858,7 @@ void Basis::set_quaternion(const Quaternion &p_quaternion) {
|
|
void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
|
|
void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
|
|
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
|
|
// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
|
|
#ifdef MATH_CHECKS
|
|
#ifdef MATH_CHECKS
|
|
- ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
|
|
|
|
|
|
+ ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized.");
|
|
#endif
|
|
#endif
|
|
Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
|
|
Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
|
|
real_t cosine = Math::cos(p_angle);
|
|
real_t cosine = Math::cos(p_angle);
|
|
@@ -905,7 +916,7 @@ void Basis::_set_diagonal(const Vector3 &p_diag) {
|
|
rows[2][2] = p_diag.z;
|
|
rows[2][2] = p_diag.z;
|
|
}
|
|
}
|
|
|
|
|
|
-Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
|
|
|
|
|
|
+Basis Basis::lerp(const Basis &p_to, real_t p_weight) const {
|
|
Basis b;
|
|
Basis b;
|
|
b.rows[0] = rows[0].lerp(p_to.rows[0], p_weight);
|
|
b.rows[0] = rows[0].lerp(p_to.rows[0], p_weight);
|
|
b.rows[1] = rows[1].lerp(p_to.rows[1], p_weight);
|
|
b.rows[1] = rows[1].lerp(p_to.rows[1], p_weight);
|
|
@@ -914,7 +925,7 @@ Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
|
|
return b;
|
|
return b;
|
|
}
|
|
}
|
|
|
|
|
|
-Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
|
|
|
|
|
|
+Basis Basis::slerp(const Basis &p_to, real_t p_weight) const {
|
|
//consider scale
|
|
//consider scale
|
|
Quaternion from(*this);
|
|
Quaternion from(*this);
|
|
Quaternion to(p_to);
|
|
Quaternion to(p_to);
|
|
@@ -1047,9 +1058,10 @@ Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up, bool p_use
|
|
v_z = -v_z;
|
|
v_z = -v_z;
|
|
}
|
|
}
|
|
Vector3 v_x = p_up.cross(v_z);
|
|
Vector3 v_x = p_up.cross(v_z);
|
|
-#ifdef MATH_CHECKS
|
|
|
|
- ERR_FAIL_COND_V_MSG(v_x.is_zero_approx(), Basis(), "The target vector and up vector can't be parallel to each other.");
|
|
|
|
-#endif
|
|
|
|
|
|
+ if (v_x.is_zero_approx()) {
|
|
|
|
+ WARN_PRINT("Target and up vectors are colinear. This is not advised as it may cause unwanted rotation around local Z axis.");
|
|
|
|
+ v_x = p_up.get_any_perpendicular(); // Vectors are almost parallel.
|
|
|
|
+ }
|
|
v_x.normalize();
|
|
v_x.normalize();
|
|
Vector3 v_y = v_z.cross(v_x);
|
|
Vector3 v_y = v_z.cross(v_x);
|
|
|
|
|