From 422314a8fd5e19eae16cc1a36fffd20ac1365046 Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 30 Sep 2024 16:41:20 +0200 Subject: [PATCH] Added set_look_at helper method to Basis. --- core/math/basis.cpp | 16 ++++++++++++++++ core/math/basis.h | 2 ++ 2 files changed, 18 insertions(+) diff --git a/core/math/basis.cpp b/core/math/basis.cpp index 875453ce4..5401d2da0 100644 --- a/core/math/basis.cpp +++ b/core/math/basis.cpp @@ -242,6 +242,22 @@ Basis Basis::create_from_scale(const Vector3 &p_scale) { Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) { return Basis::create_looking_at(p_target, p_up); } +void Basis::set_look_at(const Vector3 &p_target, const Vector3 &p_up) { +#ifdef MATH_CHECKS + ERR_FAIL_COND_MSG(p_target.is_equal_approx(Vector3()), "The target vector can't be zero."); + ERR_FAIL_COND_MSG(p_up.is_equal_approx(Vector3()), "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_MSG(v_x.is_equal_approx(Vector3()), "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); + + set_columns(v_x, v_y, v_z); +} + Basis Basis::from_scale(const Vector3 &p_scale) { return Basis::create_from_scale(p_scale); } diff --git a/core/math/basis.h b/core/math/basis.h index bdf22ad34..9c32b7fb4 100644 --- a/core/math/basis.h +++ b/core/math/basis.h @@ -290,6 +290,8 @@ struct _NO_DISCARD_CLASS_ Basis { static Basis create_from_scale(const Vector3 &p_scale); Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); + void set_look_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); + Basis from_scale(const Vector3 &p_scale); operator Quaternion() const { return get_quaternion(); }