summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2016-10-03 13:53:46 +0200
committerGitHub <noreply@github.com>2016-10-03 13:53:46 +0200
commit917f5e9bbc688214755f723e4c03cd471fb795d5 (patch)
treecc3b2759189fb1db45b860c090576298577dfc99
parentc46e205237192ed58e21f13d90d7b6a75c7570ba (diff)
parentdeb36b44d1a67157a44695ed7519605f431a3d89 (diff)
Merge pull request #6675 from J08nY/issue-vector3-angle-to
Adds Vector.angle_to(Vector other), fixes #3912 [ci skip]
-rw-r--r--core/math/vector3.cpp12
-rw-r--r--core/math/vector3.h197
-rw-r--r--core/variant_call.cpp2
3 files changed, 109 insertions, 102 deletions
diff --git a/core/math/vector3.cpp b/core/math/vector3.cpp
index b4a13d8f6d..fae3831dd6 100644
--- a/core/math/vector3.cpp
+++ b/core/math/vector3.cpp
@@ -49,13 +49,13 @@ void Vector3::set_axis(int p_axis,real_t p_value) {
}
real_t Vector3::get_axis(int p_axis) const {
- ERR_FAIL_INDEX_V(p_axis,3,0);
- return operator[](p_axis);
+ ERR_FAIL_INDEX_V(p_axis,3,0);
+ return operator[](p_axis);
}
int Vector3::min_axis() const {
- return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
+ return x < y ? (x < z ? 0 : 2) : (y < z ? 1 : 2);
}
int Vector3::max_axis() const {
@@ -71,9 +71,9 @@ void Vector3::snap(float p_val) {
}
Vector3 Vector3::snapped(float p_val) const {
- Vector3 v=*this;
- v.snap(p_val);
- return v;
+ Vector3 v=*this;
+ v.snap(p_val);
+ return v;
}
diff --git a/core/math/vector3.h b/core/math/vector3.h
index 910446023a..06840be5e7 100644
--- a/core/math/vector3.h
+++ b/core/math/vector3.h
@@ -63,77 +63,78 @@ struct Vector3 {
return coord[p_axis];
}
- void set_axis(int p_axis,real_t p_value);
- real_t get_axis(int p_axis) const;
+ void set_axis(int p_axis,real_t p_value);
+ real_t get_axis(int p_axis) const;
- int min_axis() const;
- int max_axis() const;
+ int min_axis() const;
+ int max_axis() const;
- _FORCE_INLINE_ real_t length() const;
- _FORCE_INLINE_ real_t length_squared() const;
+ _FORCE_INLINE_ real_t length() const;
+ _FORCE_INLINE_ real_t length_squared() const;
- _FORCE_INLINE_ void normalize();
- _FORCE_INLINE_ Vector3 normalized() const;
- _FORCE_INLINE_ Vector3 inverse() const;
+ _FORCE_INLINE_ void normalize();
+ _FORCE_INLINE_ Vector3 normalized() const;
+ _FORCE_INLINE_ Vector3 inverse() const;
_FORCE_INLINE_ void zero();
- void snap(float p_val);
- Vector3 snapped(float p_val) const;
+ void snap(float p_val);
+ Vector3 snapped(float p_val) const;
void rotate(const Vector3& p_axis,float p_phi);
Vector3 rotated(const Vector3& p_axis,float p_phi) const;
- /* Static Methods between 2 vector3s */
+ /* Static Methods between 2 vector3s */
- _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const;
+ _FORCE_INLINE_ Vector3 linear_interpolate(const Vector3& p_b,float p_t) const;
Vector3 cubic_interpolate(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
Vector3 cubic_interpolaten(const Vector3& p_b,const Vector3& p_pre_a, const Vector3& p_post_b,float p_t) const;
- _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
- _FORCE_INLINE_ real_t dot(const Vector3& p_b) const;
+ _FORCE_INLINE_ Vector3 cross(const Vector3& p_b) const;
+ _FORCE_INLINE_ real_t dot(const Vector3& p_b) const;
- _FORCE_INLINE_ Vector3 abs() const;
- _FORCE_INLINE_ Vector3 floor() const;
- _FORCE_INLINE_ Vector3 ceil() const;
+ _FORCE_INLINE_ Vector3 abs() const;
+ _FORCE_INLINE_ Vector3 floor() const;
+ _FORCE_INLINE_ Vector3 ceil() const;
- _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const;
- _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
+ _FORCE_INLINE_ real_t distance_to(const Vector3& p_b) const;
+ _FORCE_INLINE_ real_t distance_squared_to(const Vector3& p_b) const;
+ _FORCE_INLINE_ real_t angle_to(const Vector3& p_b) const;
_FORCE_INLINE_ Vector3 slide(const Vector3& p_vec) const;
_FORCE_INLINE_ Vector3 reflect(const Vector3& p_vec) const;
- /* Operators */
+ /* Operators */
- _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v);
- _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const;
+ _FORCE_INLINE_ Vector3& operator+=(const Vector3& p_v);
+ _FORCE_INLINE_ Vector3 operator+(const Vector3& p_v) const;
+ _FORCE_INLINE_ Vector3& operator-=(const Vector3& p_v);
+ _FORCE_INLINE_ Vector3 operator-(const Vector3& p_v) const;
+ _FORCE_INLINE_ Vector3& operator*=(const Vector3& p_v);
+ _FORCE_INLINE_ Vector3 operator*(const Vector3& p_v) const;
+ _FORCE_INLINE_ Vector3& operator/=(const Vector3& p_v);
+ _FORCE_INLINE_ Vector3 operator/(const Vector3& p_v) const;
- _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar);
- _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
- _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar);
- _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
+ _FORCE_INLINE_ Vector3& operator*=(real_t p_scalar);
+ _FORCE_INLINE_ Vector3 operator*(real_t p_scalar) const;
+ _FORCE_INLINE_ Vector3& operator/=(real_t p_scalar);
+ _FORCE_INLINE_ Vector3 operator/(real_t p_scalar) const;
- _FORCE_INLINE_ Vector3 operator-() const;
+ _FORCE_INLINE_ Vector3 operator-() const;
- _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
- _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
- _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
+ _FORCE_INLINE_ bool operator==(const Vector3& p_v) const;
+ _FORCE_INLINE_ bool operator!=(const Vector3& p_v) const;
+ _FORCE_INLINE_ bool operator<(const Vector3& p_v) const;
_FORCE_INLINE_ bool operator<=(const Vector3& p_v) const;
operator String() const;
- _FORCE_INLINE_ Vector3() { x=y=z=0; }
- _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
+ _FORCE_INLINE_ Vector3() { x=y=z=0; }
+ _FORCE_INLINE_ Vector3(real_t p_x,real_t p_y,real_t p_z) { x=p_x; y=p_y; z=p_z; }
};
@@ -151,11 +152,12 @@ Vector3 Vector3::cross(const Vector3& p_b) const {
(x * p_b.y) - (y * p_b.x)
);
- return ret;
+ return ret;
}
+
real_t Vector3::dot(const Vector3& p_b) const {
- return x*p_b.x + y*p_b.y + z*p_b.z;
+ return x*p_b.x + y*p_b.y + z*p_b.z;
}
Vector3 Vector3::abs() const {
@@ -180,115 +182,119 @@ Vector3 Vector3::linear_interpolate(const Vector3& p_b,float p_t) const {
y+(p_t * (p_b.y-y)),
z+(p_t * (p_b.z-z))
);
-
}
-
-
real_t Vector3::distance_to(const Vector3& p_b) const {
+
return (p_b-*this).length();
}
+
real_t Vector3::distance_squared_to(const Vector3& p_b) const {
- return (p_b-*this).length_squared();
+ return (p_b-*this).length_squared();
+}
+
+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()));
}
/* Operators */
Vector3& Vector3::operator+=(const Vector3& p_v) {
- x+=p_v.x;
- y+=p_v.y;
- z+=p_v.z;
- return *this;
+ x+=p_v.x;
+ y+=p_v.y;
+ z+=p_v.z;
+ return *this;
}
+
Vector3 Vector3::operator+(const Vector3& p_v) const {
- return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
+ return Vector3(x+p_v.x, y+p_v.y, z+ p_v.z);
}
Vector3& Vector3::operator-=(const Vector3& p_v) {
- x-=p_v.x;
- y-=p_v.y;
- z-=p_v.z;
- return *this;
+ x-=p_v.x;
+ y-=p_v.y;
+ z-=p_v.z;
+ return *this;
}
Vector3 Vector3::operator-(const Vector3& p_v) const {
- return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
+ return Vector3(x-p_v.x, y-p_v.y, z- p_v.z);
}
-
-
Vector3& Vector3::operator*=(const Vector3& p_v) {
- x*=p_v.x;
- y*=p_v.y;
- z*=p_v.z;
- return *this;
+ x*=p_v.x;
+ y*=p_v.y;
+ z*=p_v.z;
+ return *this;
}
Vector3 Vector3::operator*(const Vector3& p_v) const {
- return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
+ return Vector3(x*p_v.x, y*p_v.y, z* p_v.z);
}
Vector3& Vector3::operator/=(const Vector3& p_v) {
- x/=p_v.x;
- y/=p_v.y;
- z/=p_v.z;
- return *this;
+ x/=p_v.x;
+ y/=p_v.y;
+ z/=p_v.z;
+ return *this;
}
+
Vector3 Vector3::operator/(const Vector3& p_v) const {
- return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
+ return Vector3(x/p_v.x, y/p_v.y, z/ p_v.z);
}
Vector3& Vector3::operator*=(real_t p_scalar) {
- x*=p_scalar;
- y*=p_scalar;
- z*=p_scalar;
- return *this;
+ x*=p_scalar;
+ y*=p_scalar;
+ z*=p_scalar;
+ return *this;
}
_FORCE_INLINE_ Vector3 operator*(real_t p_scalar, const Vector3& p_vec) {
+
return p_vec * p_scalar;
}
Vector3 Vector3::operator*(real_t p_scalar) const {
- return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
+ return Vector3( x*p_scalar, y*p_scalar, z*p_scalar);
}
Vector3& Vector3::operator/=(real_t p_scalar) {
- x/=p_scalar;
- y/=p_scalar;
- z/=p_scalar;
- return *this;
+ x/=p_scalar;
+ y/=p_scalar;
+ z/=p_scalar;
+ return *this;
}
Vector3 Vector3::operator/(real_t p_scalar) const {
- return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
+ return Vector3( x/p_scalar, y/p_scalar, z/p_scalar);
}
Vector3 Vector3::operator-() const {
- return Vector3( -x, -y, -z );
+ return Vector3( -x, -y, -z );
}
-
bool Vector3::operator==(const Vector3& p_v) const {
- return (x==p_v.x && y==p_v.y && z==p_v.z);
+ return (x==p_v.x && y==p_v.y && z==p_v.z);
}
bool Vector3::operator!=(const Vector3& p_v) const {
- return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
+ return (x!=p_v.x || y!=p_v.y || z!=p_v.z);
}
bool Vector3::operator<(const Vector3& p_v) const {
@@ -298,9 +304,9 @@ bool Vector3::operator<(const Vector3& p_v) const {
return z<p_v.z;
else
return y<p_v.y;
- } else
+ } else {
return x<p_v.x;
-
+ }
}
bool Vector3::operator<=(const Vector3& p_v) const {
@@ -310,9 +316,9 @@ bool Vector3::operator<=(const Vector3& p_v) const {
return z<=p_v.z;
else
return y<p_v.y;
- } else
+ } else {
return x<p_v.x;
-
+ }
}
_FORCE_INLINE_ Vector3 vec3_cross(const Vector3& p_a, const Vector3& p_b) {
@@ -333,6 +339,7 @@ real_t Vector3::length() const {
return Math::sqrt(x2+y2+z2);
}
+
real_t Vector3::length_squared() const {
real_t x2=x*x;
@@ -340,27 +347,25 @@ real_t Vector3::length_squared() const {
real_t z2=z*z;
return x2+y2+z2;
-
}
void Vector3::normalize() {
- real_t l=length();
- if (l==0) {
-
+ real_t l=length();
+ if (l==0) {
x=y=z=0;
- } else {
-
+ } else {
x/=l;
y/=l;
z/=l;
- }
+ }
}
+
Vector3 Vector3::normalized() const {
- Vector3 v=*this;
- v.normalize();
- return v;
+ Vector3 v=*this;
+ v.normalize();
+ return v;
}
Vector3 Vector3::inverse() const {
@@ -377,10 +382,10 @@ Vector3 Vector3::slide(const Vector3& p_vec) const {
return p_vec - *this * this->dot(p_vec);
}
+
Vector3 Vector3::reflect(const Vector3& p_vec) const {
return p_vec - *this * this->dot(p_vec) * 2.0;
-
}
#endif
diff --git a/core/variant_call.cpp b/core/variant_call.cpp
index a84eed4d88..51cd4c2399 100644
--- a/core/variant_call.cpp
+++ b/core/variant_call.cpp
@@ -375,6 +375,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var
VCALL_LOCALMEM0R(Vector3, ceil);
VCALL_LOCALMEM1R(Vector3, distance_to);
VCALL_LOCALMEM1R(Vector3, distance_squared_to);
+ VCALL_LOCALMEM1R(Vector3, angle_to);
VCALL_LOCALMEM1R(Vector3, slide);
VCALL_LOCALMEM1R(Vector3, reflect);
@@ -1487,6 +1488,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl
ADDFUNC0(VECTOR3,VECTOR3,Vector3,ceil,varray());
ADDFUNC1(VECTOR3,REAL,Vector3,distance_to,VECTOR3,"b",varray());
ADDFUNC1(VECTOR3,REAL,Vector3,distance_squared_to,VECTOR3,"b",varray());
+ ADDFUNC1(VECTOR3,REAL,Vector3,angle_to,VECTOR3,"to",varray());
ADDFUNC1(VECTOR3,VECTOR3,Vector3,slide,VECTOR3,"by",varray());
ADDFUNC1(VECTOR3,VECTOR3,Vector3,reflect,VECTOR3,"by",varray());