diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 83c616e97..8fb42fa5e 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -217,7 +217,25 @@ Basis Basis::transposed() const { return tr; } -Basis Basis::from_scale(const Vector3 &p_scale) { +Basis Basis::create_looking_at(const Vector3 &p_target, const Vector3 &p_up) { +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero."); + ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero."); +#endif + Vector3 v_z = -p_target.normalized(); + Vector3 v_x = p_up.cross(v_z); +#ifdef MATH_CHECKS + ERR_FAIL_COND_V_MSG(v_x.is_equal_approx(Vector3()), Basis(), "The target vector and up vector can't be parallel to each other."); +#endif + v_x.normalize(); + Vector3 v_y = v_z.cross(v_x); + + Basis basis; + basis.set_columns(v_x, v_y, v_z); + return basis; +} + +Basis Basis::create_from_scale(const Vector3 &p_scale) { return Basis(p_scale.x, 0, 0, 0, p_scale.y, 0, 0, 0, p_scale.z); } @@ -1185,20 +1203,3 @@ void Basis::rotate_sh(real_t *p_values) { p_values[8] = d4 * s_scale_dst4; } -Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) { -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero."); - ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero."); -#endif - Vector3 v_z = -p_target.normalized(); - Vector3 v_x = p_up.cross(v_z); -#ifdef MATH_CHECKS - ERR_FAIL_COND_V_MSG(v_x.is_equal_approx(Vector3()), Basis(), "The target vector and up vector can't be parallel to each other."); -#endif - v_x.normalize(); - Vector3 v_y = v_z.cross(v_x); - - Basis basis; - basis.set_columns(v_x, v_y, v_z); - return basis; -} diff --git a/core/math/basis.h b/core/math/basis.h index 17975d0ed..beb583f8e 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -286,8 +286,8 @@ struct _NO_DISCARD_CLASS_ Basis { // only be used in cases of single normals, or when the basis changes each time. Vector3 xform_normal(const Vector3 &p_vector) const { return get_normal_xform_basis().xform_normal_fast(p_vector); } - static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); - static Basis from_scale(const Vector3 &p_scale); + static Basis create_looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); + static Basis create_from_scale(const Vector3 &p_scale); operator Quaternion() const { return get_quaternion(); } diff --git a/modules/gdnative/gdnative/basis.cpp b/modules/gdnative/gdnative/basis.cpp index 46e36ba0a..f1c5e59b7 100644 --- a/modules/gdnative/gdnative/basis.cpp +++ b/modules/gdnative/gdnative/basis.cpp @@ -691,23 +691,23 @@ pandemonium_vector3 GDAPI pandemonium_basis_xform_normal(const pandemonium_basis return dest; } -pandemonium_basis GDAPI pandemonium_basis_looking_at(const pandemonium_vector3 *p_target) { +pandemonium_basis GDAPI pandemonium_basis_create_looking_at(const pandemonium_vector3 *p_target) { pandemonium_basis dest; const Vector3 *target = (const Vector3 *)p_target; - *((Basis *)&dest) = Basis::looking_at(*target); + *((Basis *)&dest) = Basis::create_looking_at(*target); return dest; } pandemonium_basis GDAPI pandemonium_basis_looking_at_up(const pandemonium_vector3 *p_target, const pandemonium_vector3 *p_up) { pandemonium_basis dest; const Vector3 *target = (const Vector3 *)p_target; const Vector3 *up = (const Vector3 *)p_up; - *((Basis *)&dest) = Basis::looking_at(*target, *up); + *((Basis *)&dest) = Basis::create_looking_at(*target, *up); return dest; } -pandemonium_basis GDAPI pandemonium_basis_from_scale(const pandemonium_vector3 *p_scale) { +pandemonium_basis GDAPI pandemonium_basis_create_from_scale(const pandemonium_vector3 *p_scale) { pandemonium_basis dest; const Vector3 *scale = (const Vector3 *)p_scale; - *((Basis *)&dest) = Basis::from_scale(*scale); + *((Basis *)&dest) = Basis::create_from_scale(*scale); return dest; } diff --git a/modules/gdnative/gdnative_api.json b/modules/gdnative/gdnative_api.json index 5ce6629c2..d74db07c5 100644 --- a/modules/gdnative/gdnative_api.json +++ b/modules/gdnative/gdnative_api.json @@ -1696,7 +1696,7 @@ ] }, { - "name": "pandemonium_basis_looking_at", + "name": "pandemonium_basis_create_looking_at", "return_type": "pandemonium_basis", "arguments": [ ["const pandemonium_vector3 *", "p_target"] @@ -1711,7 +1711,7 @@ ] }, { - "name": "pandemonium_basis_from_scale", + "name": "pandemonium_basis_create_from_scale", "return_type": "pandemonium_basis", "arguments": [ ["const pandemonium_vector3 *", "p_scale"] @@ -13911,4 +13911,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/modules/gdnative/include/gdn/basis.h b/modules/gdnative/include/gdn/basis.h index 7658d7611..98eb5da3d 100644 --- a/modules/gdnative/include/gdn/basis.h +++ b/modules/gdnative/include/gdn/basis.h @@ -213,9 +213,9 @@ pandemonium_basis GDAPI pandemonium_basis_get_normal_xform_basis(const pandemoni pandemonium_vector3 GDAPI pandemonium_basis_xform_normal_fast(const pandemonium_basis *p_self, const pandemonium_vector3 *p_vector); pandemonium_vector3 GDAPI pandemonium_basis_xform_normal(const pandemonium_basis *p_self, const pandemonium_vector3 *p_vector); -pandemonium_basis GDAPI pandemonium_basis_looking_at(const pandemonium_vector3 *p_target); +pandemonium_basis GDAPI pandemonium_basis_create_looking_at(const pandemonium_vector3 *p_target); pandemonium_basis GDAPI pandemonium_basis_looking_at_up(const pandemonium_vector3 *p_target, const pandemonium_vector3 *p_up); -pandemonium_basis GDAPI pandemonium_basis_from_scale(const pandemonium_vector3 *p_scale); +pandemonium_basis GDAPI pandemonium_basis_create_from_scale(const pandemonium_vector3 *p_scale); // p_elements is a pointer to an array of 3 (!!) vector3 void GDAPI pandemonium_basis_get_elements(const pandemonium_basis *p_self, pandemonium_vector3 *p_elements);