diff options
Diffstat (limited to 'core/math/vector3.h')
-rw-r--r-- | core/math/vector3.h | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/core/math/vector3.h b/core/math/vector3.h index fc02e66c33..a6bc20ccb2 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -6,6 +6,7 @@ /* http://www.godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ @@ -75,6 +76,7 @@ struct Vector3 { _FORCE_INLINE_ void normalize(); _FORCE_INLINE_ Vector3 normalized() const; + _FORCE_INLINE_ bool is_normalized() const; _FORCE_INLINE_ Vector3 inverse() const; _FORCE_INLINE_ void zero(); @@ -106,6 +108,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 */ @@ -214,7 +217,7 @@ real_t Vector3::distance_squared_to(const Vector3 &p_b) const { real_t Vector3::angle_to(const Vector3 &p_b) const { - return Math::acos(this->dot(p_b) / Math::sqrt(this->length_squared() * p_b.length_squared())); + return Math::atan2(cross(p_b).length(), dot(p_b)); } /* Operators */ @@ -385,6 +388,10 @@ Vector3 Vector3::normalized() const { return v; } +bool Vector3::is_normalized() const { + return Math::isequal_approx(length(), (real_t)1.0); +} + Vector3 Vector3::inverse() const { return Vector3(1.0 / x, 1.0 / y, 1.0 / z); @@ -395,14 +402,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 |