diff options
author | Ferenc Arn <tagcup@yahoo.com> | 2017-04-05 17:47:13 -0500 |
---|---|---|
committer | Ferenc Arn <tagcup@yahoo.com> | 2017-04-06 13:03:56 -0500 |
commit | 9a37ff1e34fe445a9168a7d91ae1df7d9928eb25 (patch) | |
tree | f524cd3d1fd4c31e7e1fe868eea45d3d3086c2f6 | |
parent | 454f53c77659721a529c48fd4be6bf80d33c0082 (diff) |
Added various functions basic math classes. Also enabled math checks only for debug builds.
Added set_scale, set_rotation_euler, set_rotation_axis_angle. Addresses #2565 directly.
Added an euler angle constructor for Basis in GDScript and also exposed is_normalized for vectors and quaternions.
Various other changes mostly cosmetic in nature.
-rw-r--r-- | core/math/math_2d.cpp | 9 | ||||
-rw-r--r-- | core/math/math_defs.h | 4 | ||||
-rw-r--r-- | core/math/math_funcs.h | 2 | ||||
-rw-r--r-- | core/math/matrix3.cpp | 126 | ||||
-rw-r--r-- | core/math/matrix3.h | 20 | ||||
-rw-r--r-- | core/math/quat.cpp | 4 | ||||
-rw-r--r-- | core/math/quat.h | 3 | ||||
-rw-r--r-- | core/math/vector3.h | 7 | ||||
-rw-r--r-- | core/variant_call.cpp | 18 | ||||
-rw-r--r-- | doc/base/classes.xml | 62 | ||||
-rw-r--r-- | main/tests/test_math.cpp | 2 | ||||
-rw-r--r-- | scene/resources/animation.cpp | 4 | ||||
-rw-r--r-- | servers/physics/body_sw.cpp | 4 |
13 files changed, 216 insertions, 49 deletions
diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp index 77cff6a052..c6f8e41ae5 100644 --- a/core/math/math_2d.cpp +++ b/core/math/math_2d.cpp @@ -62,7 +62,8 @@ Vector2 Vector2::normalized() const { } bool Vector2::is_normalized() const { - return Math::isequal_approx(length(), (real_t)1.0); + // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. + return Math::is_equal_approx(length_squared(), 1.0); } real_t Vector2::distance_to(const Vector2 &p_vector2) const { @@ -280,7 +281,7 @@ Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, c // slide returns the component of the vector along the given plane, specified by its normal vector. Vector2 Vector2::slide(const Vector2 &p_n) const { -#ifdef DEBUG_ENABLED +#ifdef MATH_CHECKS ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector2()); #endif return *this - p_n * this->dot(p_n); @@ -291,7 +292,7 @@ Vector2 Vector2::bounce(const Vector2 &p_n) const { } Vector2 Vector2::reflect(const Vector2 &p_n) const { -#ifdef DEBUG_ENABLED +#ifdef MATH_CHECKS ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector2()); #endif return 2.0 * p_n * this->dot(p_n) - *this; @@ -438,7 +439,9 @@ Transform2D Transform2D::inverse() const { void Transform2D::affine_invert() { real_t det = basis_determinant(); +#ifdef MATH_CHECKS ERR_FAIL_COND(det == 0); +#endif real_t idet = 1.0 / det; SWAP(elements[0][0], elements[1][1]); diff --git a/core/math/math_defs.h b/core/math/math_defs.h index 08f4e27e64..74839309d4 100644 --- a/core/math/math_defs.h +++ b/core/math/math_defs.h @@ -34,6 +34,10 @@ #define CMP_NORMALIZE_TOLERANCE 0.000001 #define CMP_POINT_IN_PLANE_EPSILON 0.00001 +#ifdef DEBUG_ENABLED +#define MATH_CHECKS +#endif + #define USEC_TO_SEC(m_usec) ((m_usec) / 1000000.0) /** * "Real" is a type that will be translated to either floats or fixed depending diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 3e02ac0bb8..46eeb6eca2 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -167,7 +167,7 @@ public: static float random(float from, float to); static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); } - static _ALWAYS_INLINE_ bool isequal_approx(real_t a, real_t b) { + static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { // TODO: Comparing floats for approximate-equality is non-trivial. // Using epsilon should cover the typical cases in Godot (where a == b is used to compare two reals), such as matrix and vector comparison operators. // A proper implementation in terms of ULPs should eventually replace the contents of this function. diff --git a/core/math/matrix3.cpp b/core/math/matrix3.cpp index b31df2fadb..898a569cbf 100644 --- a/core/math/matrix3.cpp +++ b/core/math/matrix3.cpp @@ -61,8 +61,9 @@ void Basis::invert() { real_t det = elements[0][0] * co[0] + elements[0][1] * co[1] + elements[0][2] * co[2]; - +#ifdef MATH_CHECKS ERR_FAIL_COND(det == 0); +#endif real_t s = 1.0 / det; set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s, @@ -71,8 +72,9 @@ void Basis::invert() { } void Basis::orthonormalize() { +#ifdef MATH_CHECKS ERR_FAIL_COND(determinant() == 0); - +#endif // Gram-Schmidt Process Vector3 x = get_axis(0); @@ -101,20 +103,20 @@ bool Basis::is_orthogonal() const { Basis id; Basis m = (*this) * transposed(); - return isequal_approx(id, m); + return is_equal_approx(id, m); } bool Basis::is_rotation() const { - return Math::isequal_approx(determinant(), 1) && is_orthogonal(); + return Math::is_equal_approx(determinant(), 1) && is_orthogonal(); } bool Basis::is_symmetric() const { - if (Math::abs(elements[0][1] - elements[1][0]) > CMP_EPSILON) + if (!Math::is_equal_approx(elements[0][1], elements[1][0])) return false; - if (Math::abs(elements[0][2] - elements[2][0]) > CMP_EPSILON) + if (!Math::is_equal_approx(elements[0][2], elements[2][0])) return false; - if (Math::abs(elements[1][2] - elements[2][1]) > CMP_EPSILON) + if (!Math::is_equal_approx(elements[1][2], elements[2][1])) return false; return true; @@ -122,11 +124,11 @@ bool Basis::is_symmetric() const { Basis Basis::diagonalize() { - //NOTE: only implemented for symmetric matrices - //with the Jacobi iterative method method - +//NOTE: only implemented for symmetric matrices +//with the Jacobi iterative method method +#ifdef MATH_CHECKS ERR_FAIL_COND_V(!is_symmetric(), Basis()); - +#endif const int ite_max = 1024; real_t off_matrix_norm_2 = elements[0][1] * elements[0][1] + elements[0][2] * elements[0][2] + elements[1][2] * elements[1][2]; @@ -159,7 +161,7 @@ Basis Basis::diagonalize() { // Compute the rotation angle real_t angle; - if (Math::abs(elements[j][j] - elements[i][i]) < CMP_EPSILON) { + if (Math::is_equal_approx(elements[j][j], elements[i][i])) { angle = Math_PI / 4; } else { angle = 0.5 * Math::atan(2 * elements[i][j] / (elements[j][j] - elements[i][i])); @@ -225,11 +227,25 @@ Basis Basis::scaled(const Vector3 &p_scale) const { } Vector3 Basis::get_scale() const { - // We are assuming M = R.S, and performing a polar decomposition to extract R and S. - // FIXME: We eventually need a proper polar decomposition. - // As a cheap workaround until then, to ensure that R is a proper rotation matrix with determinant +1 - // (such that it can be represented by a Quat or Euler angles), we absorb the sign flip into the scaling matrix. - // As such, it works in conjunction with get_rotation(). + // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S. + // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and + // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal). + // + // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition + // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where + // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix, + // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P, + // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique. + // Therefore, we are going to do this decomposition by sticking to a particular convention. + // This may lead to confusion for some users though. + // + // The convention we use here is to absorb the sign flip into the scaling matrix. + // The same convention is also used in other similar functions such as set_scale, + // get_rotation_axis_angle, get_rotation, set_rotation_axis_angle, set_rotation_euler, ... + // + // A proper way to get rid of this issue would be to store the scaling values (or at least their signs) + // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the + // matrix elements. real_t det_sign = determinant() > 0 ? 1 : -1; return det_sign * Vector3( Vector3(elements[0][0], elements[1][0], elements[2][0]).length(), @@ -237,6 +253,17 @@ Vector3 Basis::get_scale() const { Vector3(elements[0][2], elements[1][2], elements[2][2]).length()); } +// Sets scaling while preserving rotation. +// This requires some care when working with matrices with negative determinant, +// since we're using a particular convention for "polar" decomposition in get_scale and get_rotation. +// For details, see the explanation in get_scale. +void Basis::set_scale(const Vector3 &p_scale) { + Vector3 e = get_euler(); + Basis(); // reset to identity + scale(p_scale); + rotate(e); +} + // Multiplies the matrix from left by the rotation matrix: M -> R.M // Note that this does *not* rotate the matrix itself. // @@ -259,6 +286,7 @@ void Basis::rotate(const Vector3 &p_euler) { *this = rotated(p_euler); } +// TODO: rename this to get_rotation_euler Vector3 Basis::get_rotation() const { // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). @@ -273,6 +301,42 @@ Vector3 Basis::get_rotation() const { return m.get_euler(); } +void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const { + // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S, + // and returns the Euler angles corresponding to the rotation part, complementing get_scale(). + // See the comment in get_scale() for further information. + Basis m = orthonormalized(); + real_t det = m.determinant(); + if (det < 0) { + // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles. + m.scale(Vector3(-1, -1, -1)); + } + + m.get_axis_angle(p_axis, p_angle); +} + +// Sets rotation while preserving scaling. +// This requires some care when working with matrices with negative determinant, +// since we're using a particular convention for "polar" decomposition in get_scale and get_rotation. +// For details, see the explanation in get_scale. +void Basis::set_rotation_euler(const Vector3 &p_euler) { + Vector3 s = get_scale(); + Basis(); // reset to identity + scale(s); + rotate(p_euler); +} + +// Sets rotation while preserving scaling. +// This requires some care when working with matrices with negative determinant, +// since we're using a particular convention for "polar" decomposition in get_scale and get_rotation. +// For details, see the explanation in get_scale. +void Basis::set_rotation_axis_angle(const Vector3 &p_axis, real_t p_angle) { + Vector3 s = get_scale(); + Basis(); // reset to identity + scale(s); + rotate(p_axis, p_angle); +} + // get_euler returns a vector containing the Euler angles in the format // (a1,a2,a3), where a3 is the angle of the first rotation, and a1 is the last // (following the convention they are commonly defined in the literature). @@ -293,9 +357,9 @@ Vector3 Basis::get_euler() const { // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy Vector3 euler; - +#ifdef MATH_CHECKS ERR_FAIL_COND_V(is_rotation() == false, euler); - +#endif euler.y = Math::asin(elements[0][2]); if (euler.y < Math_PI * 0.5) { if (euler.y > -Math_PI * 0.5) { @@ -339,11 +403,11 @@ void Basis::set_euler(const Vector3 &p_euler) { *this = xmat * (ymat * zmat); } -bool Basis::isequal_approx(const Basis &a, const Basis &b) const { +bool Basis::is_equal_approx(const Basis &a, const Basis &b) const { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { - if (Math::isequal_approx(a.elements[i][j], b.elements[i][j]) == false) + if (Math::is_equal_approx(a.elements[i][j], b.elements[i][j]) == false) return false; } } @@ -386,8 +450,9 @@ Basis::operator String() const { } Basis::operator Quat() const { +#ifdef MATH_CHECKS ERR_FAIL_COND_V(is_rotation() == false, Quat()); - +#endif real_t trace = elements[0][0] + elements[1][1] + elements[2][2]; real_t temp[4]; @@ -481,9 +546,10 @@ void Basis::set_orthogonal_index(int p_index) { *this = _ortho_bases[p_index]; } -void Basis::get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const { +void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { +#ifdef MATH_CHECKS ERR_FAIL_COND(is_rotation() == false); - +#endif real_t angle, x, y, z; // variables for result real_t epsilon = 0.01; // margin to allow for rounding errors real_t epsilon2 = 0.1; // margin to distinguish between 0 and 180 degrees @@ -572,11 +638,11 @@ Basis::Basis(const Quat &p_quat) { xz - wy, yz + wx, 1.0 - (xx + yy)); } -Basis::Basis(const Vector3 &p_axis, real_t p_phi) { - // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle - +void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_phi) { +// Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle +#ifdef MATH_CHECKS ERR_FAIL_COND(p_axis.is_normalized() == false); - +#endif 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_phi); @@ -594,3 +660,7 @@ Basis::Basis(const Vector3 &p_axis, real_t p_phi) { elements[2][1] = p_axis.y * p_axis.z * (1.0 - cosine) + p_axis.x * sine; elements[2][2] = axis_sq.z + cosine * (1.0 - axis_sq.z); } + +Basis::Basis(const Vector3 &p_axis, real_t p_phi) { + set_axis_angle(p_axis, p_phi); +} diff --git a/core/math/matrix3.h b/core/math/matrix3.h index 0240bc8610..ddf473f167 100644 --- a/core/math/matrix3.h +++ b/core/math/matrix3.h @@ -76,15 +76,25 @@ public: void rotate(const Vector3 &p_euler); Basis rotated(const Vector3 &p_euler) const; + Vector3 get_rotation() const; + void get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const; - void scale(const Vector3 &p_scale); - Basis scaled(const Vector3 &p_scale) const; - Vector3 get_scale() const; + void set_rotation_euler(const Vector3 &p_euler); + void set_rotation_axis_angle(const Vector3 &p_axis, real_t p_angle); Vector3 get_euler() const; void set_euler(const Vector3 &p_euler); + void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const; + void set_axis_angle(const Vector3 &p_axis, real_t p_phi); + + void scale(const Vector3 &p_scale); + Basis scaled(const Vector3 &p_scale) const; + + Vector3 get_scale() const; + void set_scale(const Vector3 &p_scale); + // transposed dot products _FORCE_INLINE_ real_t tdotx(const Vector3 &v) const { return elements[0][0] * v[0] + elements[1][0] * v[1] + elements[2][0] * v[2]; @@ -96,7 +106,7 @@ public: return elements[0][2] * v[0] + elements[1][2] * v[1] + elements[2][2] * v[2]; } - bool isequal_approx(const Basis &a, const Basis &b) const; + bool is_equal_approx(const Basis &a, const Basis &b) const; bool operator==(const Basis &p_matrix) const; bool operator!=(const Basis &p_matrix) const; @@ -120,8 +130,6 @@ public: operator String() const; - void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const; - /* create / set */ _FORCE_INLINE_ void set(real_t xx, real_t xy, real_t xz, real_t yx, real_t yy, real_t yz, real_t zx, real_t zy, real_t zz) { diff --git a/core/math/quat.cpp b/core/math/quat.cpp index b990e9184f..6c06317cf1 100644 --- a/core/math/quat.cpp +++ b/core/math/quat.cpp @@ -91,6 +91,10 @@ Quat Quat::normalized() const { return *this / length(); } +bool Quat::is_normalized() const { + return Math::is_equal_approx(length(), 1.0); +} + Quat Quat::inverse() const { return Quat(-x, -y, -z, w); } diff --git a/core/math/quat.h b/core/math/quat.h index 3fc843b83a..dca1c8701f 100644 --- a/core/math/quat.h +++ b/core/math/quat.h @@ -47,6 +47,7 @@ public: real_t length() const; void normalize(); Quat normalized() const; + bool is_normalized() const; Quat inverse() const; _FORCE_INLINE_ real_t dot(const Quat &q) const; void set_euler(const Vector3 &p_euler); @@ -55,7 +56,7 @@ public: Quat slerpni(const Quat &q, const real_t &t) const; Quat cubic_slerp(const Quat &q, const Quat &prep, const Quat &postq, const real_t &t) const; - _FORCE_INLINE_ void get_axis_and_angle(Vector3 &r_axis, real_t &r_angle) const { + _FORCE_INLINE_ void get_axis_angle(Vector3 &r_axis, real_t &r_angle) const { r_angle = 2 * Math::acos(w); r_axis.x = x / Math::sqrt(1 - w * w); r_axis.y = y / Math::sqrt(1 - w * w); diff --git a/core/math/vector3.h b/core/math/vector3.h index 8550ae7009..22d45400b1 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -388,7 +388,8 @@ Vector3 Vector3::normalized() const { } bool Vector3::is_normalized() const { - return Math::isequal_approx(length(), (real_t)1.0); + // use length_squared() instead of length() to avoid sqrt(), makes it more stringent. + return Math::is_equal_approx(length_squared(), 1.0); } Vector3 Vector3::inverse() const { @@ -403,7 +404,7 @@ void Vector3::zero() { // slide returns the component of the vector along the given plane, specified by its normal vector. Vector3 Vector3::slide(const Vector3 &p_n) const { -#ifdef DEBUG_ENABLED +#ifdef MATH_CHECKS ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector3()); #endif return *this - p_n * this->dot(p_n); @@ -414,7 +415,7 @@ Vector3 Vector3::bounce(const Vector3 &p_n) const { } Vector3 Vector3::reflect(const Vector3 &p_n) const { -#ifdef DEBUG_ENABLED +#ifdef MATH_CHECKS ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector3()); #endif return 2.0 * p_n * this->dot(p_n) - *this; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 9c435ea0e5..b8b2651068 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -327,6 +327,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector2, normalized); VCALL_LOCALMEM0R(Vector2, length); VCALL_LOCALMEM0R(Vector2, length_squared); + VCALL_LOCALMEM0R(Vector2, is_normalized); VCALL_LOCALMEM1R(Vector2, distance_to); VCALL_LOCALMEM1R(Vector2, distance_squared_to); VCALL_LOCALMEM1R(Vector2, angle_to); @@ -361,6 +362,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector3, max_axis); VCALL_LOCALMEM0R(Vector3, length); VCALL_LOCALMEM0R(Vector3, length_squared); + VCALL_LOCALMEM0R(Vector3, is_normalized); VCALL_LOCALMEM0R(Vector3, normalized); VCALL_LOCALMEM0R(Vector3, inverse); VCALL_LOCALMEM1R(Vector3, snapped); @@ -417,6 +419,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Quat, length); VCALL_LOCALMEM0R(Quat, length_squared); VCALL_LOCALMEM0R(Quat, normalized); + VCALL_LOCALMEM0R(Quat, is_normalized); VCALL_LOCALMEM0R(Quat, inverse); VCALL_LOCALMEM1R(Quat, dot); VCALL_LOCALMEM1R(Quat, xform); @@ -703,6 +706,9 @@ struct _VariantCall { VCALL_PTR1R(Basis, scaled); VCALL_PTR0R(Basis, get_scale); VCALL_PTR0R(Basis, get_euler); + VCALL_PTR1(Basis, set_scale); + VCALL_PTR1(Basis, set_rotation_euler); + VCALL_PTR2(Basis, set_rotation_axis_angle); VCALL_PTR1R(Basis, tdotx); VCALL_PTR1R(Basis, tdoty); VCALL_PTR1R(Basis, tdotz); @@ -874,6 +880,11 @@ struct _VariantCall { r_ret = Basis(p_args[0]->operator Vector3(), p_args[1]->operator real_t()); } + static void Basis_init3(Variant &r_ret, const Variant **p_args) { + + r_ret = Basis(p_args[0]->operator Vector3()); + } + static void Transform_init1(Variant &r_ret, const Variant **p_args) { Transform t; @@ -1428,6 +1439,7 @@ void register_variant_methods() { ADDFUNC0(VECTOR2, REAL, Vector2, length, varray()); ADDFUNC0(VECTOR2, REAL, Vector2, angle, varray()); ADDFUNC0(VECTOR2, REAL, Vector2, length_squared, varray()); + ADDFUNC0(VECTOR2, BOOL, Vector2, is_normalized, varray()); ADDFUNC1(VECTOR2, REAL, Vector2, distance_to, VECTOR2, "to", varray()); ADDFUNC1(VECTOR2, REAL, Vector2, distance_squared_to, VECTOR2, "to", varray()); ADDFUNC1(VECTOR2, REAL, Vector2, angle_to, VECTOR2, "to", varray()); @@ -1461,6 +1473,7 @@ void register_variant_methods() { ADDFUNC0(VECTOR3, INT, Vector3, max_axis, varray()); ADDFUNC0(VECTOR3, REAL, Vector3, length, varray()); ADDFUNC0(VECTOR3, REAL, Vector3, length_squared, varray()); + ADDFUNC0(VECTOR3, BOOL, Vector3, is_normalized, varray()); ADDFUNC0(VECTOR3, VECTOR3, Vector3, normalized, varray()); ADDFUNC0(VECTOR3, VECTOR3, Vector3, inverse, varray()); ADDFUNC1(VECTOR3, VECTOR3, Vector3, snapped, REAL, "by", varray()); @@ -1496,6 +1509,7 @@ void register_variant_methods() { ADDFUNC0(QUAT, REAL, Quat, length, varray()); ADDFUNC0(QUAT, REAL, Quat, length_squared, varray()); ADDFUNC0(QUAT, QUAT, Quat, normalized, varray()); + ADDFUNC0(QUAT, BOOL, Quat, is_normalized, varray()); ADDFUNC0(QUAT, QUAT, Quat, inverse, varray()); ADDFUNC1(QUAT, REAL, Quat, dot, QUAT, "b", varray()); ADDFUNC1(QUAT, VECTOR3, Quat, xform, VECTOR3, "v", varray()); @@ -1691,6 +1705,9 @@ void register_variant_methods() { ADDFUNC0(BASIS, REAL, Basis, determinant, varray()); ADDFUNC2(BASIS, BASIS, Basis, rotated, VECTOR3, "axis", REAL, "phi", varray()); ADDFUNC1(BASIS, BASIS, Basis, scaled, VECTOR3, "scale", varray()); + ADDFUNC1(BASIS, NIL, Basis, set_scale, VECTOR3, "scale", varray()); + ADDFUNC1(BASIS, NIL, Basis, set_rotation_euler, VECTOR3, "euler", varray()); + ADDFUNC2(BASIS, NIL, Basis, set_rotation_axis_angle, VECTOR3, "axis", REAL, "angle", varray()); ADDFUNC0(BASIS, VECTOR3, Basis, get_scale, varray()); ADDFUNC0(BASIS, VECTOR3, Basis, get_euler, varray()); ADDFUNC1(BASIS, REAL, Basis, tdotx, VECTOR3, "with", varray()); @@ -1748,6 +1765,7 @@ void register_variant_methods() { _VariantCall::add_constructor(_VariantCall::Basis_init1, Variant::BASIS, "x_axis", Variant::VECTOR3, "y_axis", Variant::VECTOR3, "z_axis", Variant::VECTOR3); _VariantCall::add_constructor(_VariantCall::Basis_init2, Variant::BASIS, "axis", Variant::VECTOR3, "phi", Variant::REAL); + _VariantCall::add_constructor(_VariantCall::Basis_init3, Variant::BASIS, "euler", Variant::VECTOR3); _VariantCall::add_constructor(_VariantCall::Transform_init1, Variant::TRANSFORM, "x_axis", Variant::VECTOR3, "y_axis", Variant::VECTOR3, "z_axis", Variant::VECTOR3, "origin", Variant::VECTOR3); _VariantCall::add_constructor(_VariantCall::Transform_init2, Variant::TRANSFORM, "basis", Variant::BASIS, "origin", Variant::VECTOR3); diff --git a/doc/base/classes.xml b/doc/base/classes.xml index 5bb85aedc1..aa520d3869 100644 --- a/doc/base/classes.xml +++ b/doc/base/classes.xml @@ -6842,6 +6842,15 @@ <method name="Basis"> <return type="Basis"> </return> + <argument index="0" name="euler" type="Vector3"> + </argument> + <description> + Create a rotation matrix (in the XYZ convention: first Z, then Y, and X last) from the specified Euler angles, given in the vector format as (third,second,first). + </description> + </method> + <method name="Basis"> + <return type="Basis"> + </return> <argument index="0" name="x_axis" type="Vector3"> </argument> <argument index="1" name="y_axis" type="Vector3"> @@ -6863,8 +6872,7 @@ <return type="Vector3"> </return> <description> - Return Euler angles (in the XYZ convention: first Z, then Y, and X last) from the matrix. Returned vector contains the rotation angles in the format (third,second,first). - This function only works if the matrix represents a proper rotation. + Assuming that the matrix is a proper rotation matrix (orthonormal matrix with determinant +1), return Euler angles (in the XYZ convention: first Z, then Y, and X last). Returned vector contains the rotation angles in the format (third,second,first). </description> </method> <method name="get_orthogonal_index"> @@ -6906,6 +6914,26 @@ Introduce an additional rotation around the given axis by phi. Only relevant when the matrix is being used as a part of [Transform]. The axis must be a normalized vector. </description> </method> + <method name="set_rotation_euler"> + <return type="Basis"> + </return> + <argument index="0" name="euler" type="Vector3"> + </argument> + <description> + Changes only the rotation part of the [Basis] to a rotation corresponding to given Euler angles, while preserving the scaling part (as determined by get_scale). + </description> + </method> + <method name="set_rotation_axis_angle"> + <return type="Basis"> + </return> + <argument index="0" name="axis" type="Vector3"> + </argument> + <argument index="1" name="phi" type="float"> + </argument> + <description> + Changes only the rotation part of the [Basis] to a rotation around given axis by phi, while preserving the scaling part (as determined by get_scale). + </description> + </method> <method name="scaled"> <return type="Basis"> </return> @@ -6915,6 +6943,15 @@ Introduce an additional scaling specified by the given 3D scaling factor. Only relevant when the matrix is being used as a part of [Transform]. </description> </method> + <method name="set_scale"> + <return type="Basis"> + </return> + <argument index="0" name="scale" type="Vector3"> + </argument> + <description> + Changes only the scaling part of the Basis to the specified scaling, while preserving the rotation part (as determined by get_rotation). + </description> + </method> <method name="tdotx"> <return type="float"> </return> @@ -34942,6 +34979,13 @@ Returns a copy of the quaternion, normalized to unit length. </description> </method> + <method name="is_normalized"> + <return type="bool"> + </return> + <description> + Returns whether the quaternion is normalized or not. + </description> + </method> <method name="slerp"> <return type="Quat"> </return> @@ -47093,6 +47137,13 @@ do_property]. Returns a normalized vector to unit length. </description> </method> + <method name="is_normalized"> + <return type="bool"> + </return> + <description> + Returns whether the vector is normalized or not. + </description> + </method> <method name="reflect"> <return type="Vector2"> </return> @@ -47317,6 +47368,13 @@ do_property]. Return a copy of the normalized vector to unit length. This is the same as v / v.length(). </description> </method> + <method name="is_normalized"> + <return type="bool"> + </return> + <description> + Returns whether the vector is normalized or not. + </description> + </method> <method name="outer"> <return type="Basis"> </return> diff --git a/main/tests/test_math.cpp b/main/tests/test_math.cpp index c82ae1abf5..ab06349e2f 100644 --- a/main/tests/test_math.cpp +++ b/main/tests/test_math.cpp @@ -601,7 +601,7 @@ MainLoop *test() { print_line(q3); print_line("before v: " + v + " a: " + rtos(a)); - q.get_axis_and_angle(v, a); + q.get_axis_angle(v, a); print_line("after v: " + v + " a: " + rtos(a)); } diff --git a/scene/resources/animation.cpp b/scene/resources/animation.cpp index 4fddd55e20..2584521171 100644 --- a/scene/resources/animation.cpp +++ b/scene/resources/animation.cpp @@ -1760,8 +1760,8 @@ bool Animation::_transform_track_optimize_key(const TKey<TransformKey> &t0, cons Vector3 v02, v01; real_t a02, a01; - r02.get_axis_and_angle(v02, a02); - r01.get_axis_and_angle(v01, a01); + r02.get_axis_angle(v02, a02); + r01.get_axis_angle(v01, a01); if (Math::abs(a02) > p_max_optimizable_angle) return false; diff --git a/servers/physics/body_sw.cpp b/servers/physics/body_sw.cpp index 6b43ec31aa..e7f152767d 100644 --- a/servers/physics/body_sw.cpp +++ b/servers/physics/body_sw.cpp @@ -494,7 +494,7 @@ void BodySW::integrate_forces(real_t p_step) { Vector3 axis; real_t angle; - rot.get_axis_and_angle(axis, angle); + rot.get_axis_angle(axis, angle); axis.normalize(); angular_velocity = axis.normalized() * (angle / p_step); @@ -637,7 +637,7 @@ void BodySW::simulate_motion(const Transform& p_xform,real_t p_step) { Vector3 axis; real_t angle; - rot.get_axis_and_angle(axis,angle); + rot.get_axis_angle(axis,angle); axis.normalize(); angular_velocity=axis.normalized() * (angle/p_step); linear_velocity = (p_xform.origin - get_transform().origin)/p_step; |