diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2019-04-08 12:00:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-08 12:00:54 +0200 |
commit | 7f3373d79f017c17ad5ffd0eba877fc056ad649b (patch) | |
tree | b78f9874fe96e979edf8cab43da744dcb94bd0ab /core/math | |
parent | a994db62df731879bb9c54e65b4ddca32396e9d3 (diff) | |
parent | 55f3bd97a270b691c26d6eda70bc7c0a3ec8f4e8 (diff) |
Merge pull request #27452 from Chaosus/direction_to
Added method to retrieve a direction vector from one point to another
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/vector2.h | 7 | ||||
-rw-r--r-- | core/math/vector3.h | 7 |
2 files changed, 14 insertions, 0 deletions
diff --git a/core/math/vector2.h b/core/math/vector2.h index a20326f667..ae2d1ec660 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -65,6 +65,7 @@ struct Vector2 { real_t distance_squared_to(const Vector2 &p_vector2) const; real_t angle_to(const Vector2 &p_vector2) const; real_t angle_to_point(const Vector2 &p_vector2) const; + _FORCE_INLINE_ Vector2 direction_to(const Vector2 &p_b) const; real_t dot(const Vector2 &p_other) const; real_t cross(const Vector2 &p_other) const; @@ -236,6 +237,12 @@ Vector2 Vector2::slerp(const Vector2 &p_b, real_t p_t) const { return rotated(theta * p_t); } +Vector2 Vector2::direction_to(const Vector2 &p_b) const { + Vector2 ret(p_b.x - x, p_b.y - y); + ret.normalize(); + return ret; +} + Vector2 Vector2::linear_interpolate(const Vector2 &p_a, const Vector2 &p_b, real_t p_t) { Vector2 res = p_a; diff --git a/core/math/vector3.h b/core/math/vector3.h index b11838d16e..e9074c5bd4 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -112,6 +112,7 @@ struct Vector3 { _FORCE_INLINE_ Vector3 project(const Vector3 &p_b) const; _FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const; + _FORCE_INLINE_ Vector3 direction_to(const Vector3 &p_b) const; _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const; _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_normal) const; @@ -244,6 +245,12 @@ real_t Vector3::angle_to(const Vector3 &p_b) const { return Math::atan2(cross(p_b).length(), dot(p_b)); } +Vector3 Vector3::direction_to(const Vector3 &p_b) const { + Vector3 ret(p_b.x - x, p_b.y - y, p_b.z - z); + ret.normalize(); + return ret; +} + /* Operators */ Vector3 &Vector3::operator+=(const Vector3 &p_v) { |