diff options
| author | Jummit <jummit@web.de> | 2020-07-15 17:05:54 +0200 |
|---|---|---|
| committer | Jummit <jummit@web.de> | 2020-07-15 17:05:54 +0200 |
| commit | 20fdc09c963f876e5d894bc4c0fd3d24b4215190 (patch) | |
| tree | 29aac5082ee0092b14e79fc5378d7d68cd941f19 /include/core | |
| parent | 9eceb16f0553884094d0f659461649be5d333866 (diff) | |
| download | redot-cpp-20fdc09c963f876e5d894bc4c0fd3d24b4215190.tar.gz | |
Add missing move_toward to Vector2 and Vector3
Diffstat (limited to 'include/core')
| -rw-r--r-- | include/core/Vector2.hpp | 7 | ||||
| -rw-r--r-- | include/core/Vector3.hpp | 7 |
2 files changed, 14 insertions, 0 deletions
diff --git a/include/core/Vector2.hpp b/include/core/Vector2.hpp index 2a4f5fe..2a4c902 100644 --- a/include/core/Vector2.hpp +++ b/include/core/Vector2.hpp @@ -178,6 +178,13 @@ struct Vector2 { Vector2 cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const; + Vector2 move_toward(const Vector2 &p_to, const real_t p_delta) const { + Vector2 v = *this; + Vector2 vd = p_to - v; + real_t len = vd.length(); + return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta; + } + inline Vector2 slide(const Vector2 &p_vec) const { return p_vec - *this * this->dot(p_vec); } diff --git a/include/core/Vector3.hpp b/include/core/Vector3.hpp index 7e2c302..bbe8a95 100644 --- a/include/core/Vector3.hpp +++ b/include/core/Vector3.hpp @@ -167,6 +167,13 @@ struct Vector3 { Vector3 cubic_interpolate(const Vector3 &b, const Vector3 &pre_a, const Vector3 &post_b, const real_t t) const; + Vector3 move_toward(const Vector3 &p_to, const real_t p_delta) const { + Vector3 v = *this; + Vector3 vd = p_to - v; + real_t len = vd.length(); + return len <= p_delta || len < CMP_EPSILON ? p_to : v + vd / len * p_delta; + } + Vector3 bounce(const Vector3 &p_normal) const { return -reflect(p_normal); } |
