diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2021-07-20 23:32:00 -0400 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2021-08-01 12:49:02 -0500 |
commit | 9f3ae0adcd734a16c299cd0f023212478b3960d3 (patch) | |
tree | 46fe469fc6df039da0710626ab790865bd67d343 /core/math | |
parent | f06d201bb7a58e0399278ee29328740ea9efd16b (diff) |
Move code for looking_at to Basis
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/basis.cpp | 18 | ||||
-rw-r--r-- | core/math/basis.h | 2 | ||||
-rw-r--r-- | core/math/transform_3d.cpp | 32 |
3 files changed, 22 insertions, 30 deletions
diff --git a/core/math/basis.cpp b/core/math/basis.cpp index aa3831d4cf..b5e25fb837 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -1129,3 +1129,21 @@ void Basis::rotate_sh(real_t *p_values) { p_values[7] = -d3; 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(v_x, v_y, v_z); + return basis; +} diff --git a/core/math/basis.h b/core/math/basis.h index 2889a4aa5e..3db2227b70 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -242,6 +242,8 @@ public: operator Quaternion() const { return get_quaternion(); } + static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); + Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }; Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); } diff --git a/core/math/transform_3d.cpp b/core/math/transform_3d.cpp index 51766b39f4..4f4943c8ef 100644 --- a/core/math/transform_3d.cpp +++ b/core/math/transform_3d.cpp @@ -71,40 +71,12 @@ void Transform3D::rotate_basis(const Vector3 &p_axis, real_t p_phi) { Transform3D Transform3D::looking_at(const Vector3 &p_target, const Vector3 &p_up) const { Transform3D t = *this; - t.set_look_at(origin, p_target, p_up); + t.basis = Basis::looking_at(p_target - origin, p_up); return t; } void Transform3D::set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up) { -#ifdef MATH_CHECKS - ERR_FAIL_COND(p_eye == p_target); - ERR_FAIL_COND(p_up.length() == 0); -#endif - // Reference: MESA source code - Vector3 v_x, v_y, v_z; - - /* Make rotation matrix */ - - /* Z vector */ - v_z = p_eye - p_target; - - v_z.normalize(); - - v_y = p_up; - - v_x = v_y.cross(v_z); -#ifdef MATH_CHECKS - ERR_FAIL_COND(v_x.length() == 0); -#endif - - /* Recompute Y = Z cross X */ - v_y = v_z.cross(v_x); - - v_x.normalize(); - v_y.normalize(); - - basis.set(v_x, v_y, v_z); - + basis = Basis::looking_at(p_target - p_eye, p_up); origin = p_eye; } |