diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2018-08-16 15:31:54 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-16 15:31:54 +0200 |
commit | be9e349f563ec716cf3740850dc7aec20aed97c0 (patch) | |
tree | c1d167bff7526c07a6fa90c589929b1433d036d9 | |
parent | 9e4d1512db4dd0fb748d60fddf9b1c7b9ef67adc (diff) | |
parent | 037f4638aba99981393edd247057f851e80db489 (diff) |
Merge pull request #20804 from karroffel/vector-project
add `project` method to Vector2/3
-rw-r--r-- | core/math/vector2.cpp | 7 | ||||
-rw-r--r-- | core/math/vector2.h | 2 | ||||
-rw-r--r-- | core/math/vector3.h | 6 | ||||
-rw-r--r-- | core/variant_call.cpp | 4 | ||||
-rw-r--r-- | doc/classes/Vector2.xml | 9 | ||||
-rw-r--r-- | doc/classes/Vector3.xml | 9 | ||||
-rw-r--r-- | servers/physics_2d/joints_2d_sw.cpp | 2 |
7 files changed, 32 insertions, 7 deletions
diff --git a/core/math/vector2.cpp b/core/math/vector2.cpp index 441e7d8907..75d9b8b311 100644 --- a/core/math/vector2.cpp +++ b/core/math/vector2.cpp @@ -121,11 +121,8 @@ Vector2 Vector2::rotated(real_t p_by) const { return v; } -Vector2 Vector2::project(const Vector2 &p_vec) const { - - Vector2 v1 = p_vec; - Vector2 v2 = *this; - return v2 * (v1.dot(v2) / v2.dot(v2)); +Vector2 Vector2::project(const Vector2 &p_b) const { + return p_b * (dot(p_b) / p_b.dot(p_b)); } Vector2 Vector2::snapped(const Vector2 &p_by) const { diff --git a/core/math/vector2.h b/core/math/vector2.h index 7c8882f6e2..fbcdc80b60 100644 --- a/core/math/vector2.h +++ b/core/math/vector2.h @@ -68,7 +68,7 @@ struct Vector2 { real_t dot(const Vector2 &p_other) const; real_t cross(const Vector2 &p_other) const; - Vector2 project(const Vector2 &p_vec) const; + Vector2 project(const Vector2 &p_b) const; Vector2 plane_project(real_t p_d, const Vector2 &p_vec) const; diff --git a/core/math/vector3.h b/core/math/vector3.h index 433adf09ee..a719e3965d 100644 --- a/core/math/vector3.h +++ b/core/math/vector3.h @@ -109,6 +109,8 @@ 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_ Vector3 project(const Vector3 &p_b) const; + _FORCE_INLINE_ real_t angle_to(const Vector3 &p_b) const; _FORCE_INLINE_ Vector3 slide(const Vector3 &p_normal) const; @@ -238,6 +240,10 @@ real_t Vector3::distance_squared_to(const Vector3 &p_b) const { return (p_b - *this).length_squared(); } +Vector3 Vector3::project(const Vector3 &p_b) const { + return p_b * (dot(p_b) / p_b.dot(p_b)); +} + real_t Vector3::angle_to(const Vector3 &p_b) const { return Math::atan2(cross(p_b).length(), dot(p_b)); diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 80cb869db2..ea51419233 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -339,6 +339,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector2, is_normalized); VCALL_LOCALMEM1R(Vector2, distance_to); VCALL_LOCALMEM1R(Vector2, distance_squared_to); + VCALL_LOCALMEM1R(Vector2, project); VCALL_LOCALMEM1R(Vector2, angle_to); VCALL_LOCALMEM1R(Vector2, angle_to_point); VCALL_LOCALMEM2R(Vector2, linear_interpolate); @@ -395,6 +396,7 @@ struct _VariantCall { VCALL_LOCALMEM0R(Vector3, round); VCALL_LOCALMEM1R(Vector3, distance_to); VCALL_LOCALMEM1R(Vector3, distance_squared_to); + VCALL_LOCALMEM1R(Vector3, project); VCALL_LOCALMEM1R(Vector3, angle_to); VCALL_LOCALMEM1R(Vector3, slide); VCALL_LOCALMEM1R(Vector3, bounce); @@ -1551,6 +1553,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR2, BOOL, Vector2, is_normalized, varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, distance_to, VECTOR2, "to", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, distance_squared_to, VECTOR2, "to", varray()); + ADDFUNC1R(VECTOR2, VECTOR2, Vector2, project, VECTOR2, "b", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, angle_to, VECTOR2, "to", varray()); ADDFUNC1R(VECTOR2, REAL, Vector2, angle_to_point, VECTOR2, "to", varray()); ADDFUNC2R(VECTOR2, VECTOR2, Vector2, linear_interpolate, VECTOR2, "b", REAL, "t", varray()); @@ -1606,6 +1609,7 @@ void register_variant_methods() { ADDFUNC0R(VECTOR3, VECTOR3, Vector3, round, varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, distance_to, VECTOR3, "b", varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, distance_squared_to, VECTOR3, "b", varray()); + ADDFUNC1R(VECTOR3, VECTOR3, Vector3, project, VECTOR3, "b", varray()); ADDFUNC1R(VECTOR3, REAL, Vector3, angle_to, VECTOR3, "to", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, slide, VECTOR3, "n", varray()); ADDFUNC1R(VECTOR3, VECTOR3, Vector3, bounce, VECTOR3, "n", varray()); diff --git a/doc/classes/Vector2.xml b/doc/classes/Vector2.xml index 6ffeddf5c1..9b18962a6f 100644 --- a/doc/classes/Vector2.xml +++ b/doc/classes/Vector2.xml @@ -130,6 +130,15 @@ Returns the distance to vector [code]b[/code]. </description> </method> + <method name="project"> + <return type="Vector2"> + </return> + <argument index="0" name="b" type="Vector2"> + </argument> + <description> + Returns the vector projected onto the vector [code]b[/code]. + </description> + </method> <method name="dot"> <return type="float"> </return> diff --git a/doc/classes/Vector3.xml b/doc/classes/Vector3.xml index 62a480166a..22384c5012 100644 --- a/doc/classes/Vector3.xml +++ b/doc/classes/Vector3.xml @@ -99,6 +99,15 @@ Returns the distance to [code]b[/code]. </description> </method> + <method name="project_onto"> + <return type="Vector3"> + </return> + <argument index="0" name="b" type="Vector3"> + </argument> + <description> + Returns the vector projected onto the vector [code]b[/code]. + </description> + </method> <method name="dot"> <return type="float"> </return> diff --git a/servers/physics_2d/joints_2d_sw.cpp b/servers/physics_2d/joints_2d_sw.cpp index d49c1b8376..517dce0043 100644 --- a/servers/physics_2d/joints_2d_sw.cpp +++ b/servers/physics_2d/joints_2d_sw.cpp @@ -321,7 +321,7 @@ void GrooveJoint2DSW::solve(real_t p_step) { Vector2 jOld = jn_acc; j += jOld; - jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : xf_normal.project(j)).clamped(jn_max); + jn_acc = (((clamp * j.cross(xf_normal)) > 0) ? j : j.project(xf_normal)).clamped(jn_max); j = jn_acc - jOld; |