Add missing move_toward to Vector2 and Vector3

pull/427/head
Jummit 2020-07-15 17:05:54 +02:00
parent 9eceb16f05
commit 20fdc09c96
2 changed files with 14 additions and 0 deletions

View File

@ -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 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 { inline Vector2 slide(const Vector2 &p_vec) const {
return p_vec - *this * this->dot(p_vec); return p_vec - *this * this->dot(p_vec);
} }

View File

@ -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 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 { Vector3 bounce(const Vector3 &p_normal) const {
return -reflect(p_normal); return -reflect(p_normal);
} }