Skip to content

Commit

Permalink
Added set_look_at helper method to Basis.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Sep 30, 2024
1 parent 6f53257 commit 422314a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions core/math/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down

0 comments on commit 422314a

Please sign in to comment.