Merge pull request #6675 from J08nY/issue-vector3-angle-to

Adds Vector.angle_to(Vector other), fixes #3912

[ci skip]
This commit is contained in:
Rémi Verschelde 2016-10-03 13:53:46 +02:00 committed by GitHub
commit 917f5e9bbc
3 changed files with 109 additions and 102 deletions

View file

@ -100,6 +100,7 @@ struct Vector3 {
_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;
@ -153,6 +154,7 @@ Vector3 Vector3::cross(const Vector3& p_b) const {
return ret;
}
real_t Vector3::dot(const Vector3& p_b) const {
return x*p_b.x + y*p_b.y + z*p_b.z;
@ -180,19 +182,23 @@ 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();
}
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) {
@ -202,6 +208,7 @@ Vector3& Vector3::operator+=(const Vector3& p_v) {
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);
@ -219,8 +226,6 @@ Vector3 Vector3::operator-(const Vector3& p_v) const {
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;
@ -240,20 +245,22 @@ Vector3& Vector3::operator/=(const Vector3& p_v) {
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);
}
Vector3& Vector3::operator*=(real_t p_scalar) {
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;
}
@ -263,11 +270,11 @@ Vector3 Vector3::operator*(real_t p_scalar) const {
}
Vector3& Vector3::operator/=(real_t p_scalar) {
x/=p_scalar;
y/=p_scalar;
z/=p_scalar;
return *this;
}
Vector3 Vector3::operator/(real_t p_scalar) const {
@ -280,7 +287,6 @@ Vector3 Vector3::operator-() const {
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);
@ -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,22 +347,20 @@ 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) {
x=y=z=0;
} else {
x/=l;
y/=l;
z/=l;
}
}
Vector3 Vector3::normalized() const {
Vector3 v=*this;
@ -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

View file

@ -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());