Added set_look_at helper method to Basis.

This commit is contained in:
Relintai 2024-09-30 16:41:20 +02:00
parent 6f53257e05
commit 422314a8fd
2 changed files with 18 additions and 0 deletions

View File

@ -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) { Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) {
return Basis::create_looking_at(p_target, 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) { Basis Basis::from_scale(const Vector3 &p_scale) {
return Basis::create_from_scale(p_scale); return Basis::create_from_scale(p_scale);
} }

View File

@ -290,6 +290,8 @@ struct _NO_DISCARD_CLASS_ Basis {
static Basis create_from_scale(const Vector3 &p_scale); static Basis create_from_scale(const Vector3 &p_scale);
Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0)); 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); Basis from_scale(const Vector3 &p_scale);
operator Quaternion() const { return get_quaternion(); } operator Quaternion() const { return get_quaternion(); }