summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2017-04-05 01:23:09 +0200
committerGitHub <noreply@github.com>2017-04-05 01:23:09 +0200
commit2c4e4432afca009614ffd5ee837d1578f54eb841 (patch)
tree8c9ed90c4e1be333672002779cd7f5b3151ea622 /core/math
parent7ed83e988930776826b8e6e7cf0c53f8d50cfe26 (diff)
parent1a620bd5faebd83015182ea032f40936a9916af6 (diff)
Merge pull request #8214 from tagcup/bounce_reflect_slide
Made slide and reflect active verbs acting on itself in Vector2 and V…
Diffstat (limited to 'core/math')
-rw-r--r--core/math/math_2d.cpp22
-rw-r--r--core/math/math_2d.h2
-rw-r--r--core/math/vector3.h20
3 files changed, 35 insertions, 9 deletions
diff --git a/core/math/math_2d.cpp b/core/math/math_2d.cpp
index 8f942c423f..77cff6a052 100644
--- a/core/math/math_2d.cpp
+++ b/core/math/math_2d.cpp
@@ -61,6 +61,10 @@ Vector2 Vector2::normalized() const {
return v;
}
+bool Vector2::is_normalized() const {
+ return Math::isequal_approx(length(), (real_t)1.0);
+}
+
real_t Vector2::distance_to(const Vector2 &p_vector2) const {
return Math::sqrt((x - p_vector2.x) * (x - p_vector2.x) + (y - p_vector2.y) * (y - p_vector2.y));
@@ -274,13 +278,23 @@ Vector2 Vector2::cubic_interpolate(const Vector2 &p_b, const Vector2 &p_pre_a, c
*/
}
-Vector2 Vector2::slide(const Vector2 &p_vec) const {
+// slide returns the component of the vector along the given plane, specified by its normal vector.
+Vector2 Vector2::slide(const Vector2 &p_n) const {
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector2());
+#endif
+ return *this - p_n * this->dot(p_n);
+}
- return p_vec - *this * this->dot(p_vec);
+Vector2 Vector2::bounce(const Vector2 &p_n) const {
+ return -reflect(p_n);
}
-Vector2 Vector2::reflect(const Vector2 &p_vec) const {
- return p_vec - *this * this->dot(p_vec) * 2.0;
+Vector2 Vector2::reflect(const Vector2 &p_n) const {
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector2());
+#endif
+ return 2.0 * p_n * this->dot(p_n) - *this;
}
bool Rect2::intersects_segment(const Point2 &p_from, const Point2 &p_to, Point2 *r_pos, Point2 *r_normal) const {
diff --git a/core/math/math_2d.h b/core/math/math_2d.h
index af6437d7f1..50ebcb845f 100644
--- a/core/math/math_2d.h
+++ b/core/math/math_2d.h
@@ -82,6 +82,7 @@ struct Vector2 {
void normalize();
Vector2 normalized() const;
+ bool is_normalized() const;
real_t length() const;
real_t length_squared() const;
@@ -106,6 +107,7 @@ struct Vector2 {
Vector2 cubic_interpolate_soft(const Vector2 &p_b, const Vector2 &p_pre_a, const Vector2 &p_post_b, real_t p_t) const;
Vector2 slide(const Vector2 &p_vec) const;
+ Vector2 bounce(const Vector2 &p_vec) const;
Vector2 reflect(const Vector2 &p_vec) const;
Vector2 operator+(const Vector2 &p_v) const;
diff --git a/core/math/vector3.h b/core/math/vector3.h
index 951380e898..8550ae7009 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -107,6 +107,7 @@ struct Vector3 {
_FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const;
_FORCE_INLINE_ Vector3 slide(const Vector3 &p_vec) const;
+ _FORCE_INLINE_ Vector3 bounce(const Vector3 &p_vec) const;
_FORCE_INLINE_ Vector3 reflect(const Vector3 &p_vec) const;
/* Operators */
@@ -400,14 +401,23 @@ void Vector3::zero() {
x = y = z = 0;
}
-Vector3 Vector3::slide(const Vector3 &p_vec) const {
-
- return p_vec - *this * this->dot(p_vec);
+// slide returns the component of the vector along the given plane, specified by its normal vector.
+Vector3 Vector3::slide(const Vector3 &p_n) const {
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector3());
+#endif
+ return *this - p_n * this->dot(p_n);
}
-Vector3 Vector3::reflect(const Vector3 &p_vec) const {
+Vector3 Vector3::bounce(const Vector3 &p_n) const {
+ return -reflect(p_n);
+}
- return p_vec - *this * this->dot(p_vec) * 2.0;
+Vector3 Vector3::reflect(const Vector3 &p_n) const {
+#ifdef DEBUG_ENABLED
+ ERR_FAIL_COND_V(p_n.is_normalized() == false, Vector3());
+#endif
+ return 2.0 * p_n * this->dot(p_n) - *this;
}
#endif