summaryrefslogtreecommitdiffstats
path: root/core/math/vector3.h
diff options
context:
space:
mode:
authorFerenc Arn <tagcup@yahoo.com>2017-03-31 10:25:09 -0500
committerFerenc Arn <tagcup@yahoo.com>2017-04-03 10:02:12 -0500
commit1a620bd5faebd83015182ea032f40936a9916af6 (patch)
treee1705e3805bae8cb898694136f65d7622dd2bc8c /core/math/vector3.h
parent6731924dcfa7451d8b31461b438fd2a5aa8af1f6 (diff)
downloadredot-engine-1a620bd5faebd83015182ea032f40936a9916af6.tar.gz
Made slide and reflect active verbs acting on itself in Vector2 and Vector3.
This is in alignment with other functions in vector classes. Also added checks for normalization, fixed the sign of reflect (which now corresponds to reflection along a plane mathematically), added bounce method and updated docs. Fixes #8201.
Diffstat (limited to 'core/math/vector3.h')
-rw-r--r--core/math/vector3.h20
1 files changed, 15 insertions, 5 deletions
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