Browse Source

Add Vector3::DistanceToPoint and DistanceToPlane math functions.

Eugene Kozlov 8 years ago
parent
commit
2c773e5bd5

+ 2 - 0
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -457,6 +457,8 @@ static void RegisterVector3(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Vector3", "float ProjectOntoAxis(const Vector3&in) const", asMETHOD(Vector3, ProjectOntoAxis), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 ProjectOntoPlane(const Vector3&in, const Vector3&in) const", asMETHOD(Vector3, ProjectOntoPlane), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 ProjectOntoLine(const Vector3&in, const Vector3&in) const", asMETHOD(Vector3, ProjectOntoLine), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Vector3", "float DistanceToPoint(const Vector3&in) const", asMETHOD(Vector3, DistanceToPoint), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Vector3", "float DistanceToPlane(const Vector3&in, const Vector3&in) const", asMETHOD(Vector3, DistanceToPlane), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 Orthogonalize(const Vector3&in) const", asMETHOD(Vector3, Orthogonalize), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 CrossProduct(const Vector3&in) const", asMETHOD(Vector3, CrossProduct), asCALL_THISCALL);
     engine->RegisterObjectMethod("Vector3", "Vector3 Abs() const", asMETHOD(Vector3, Abs), asCALL_THISCALL);

+ 2 - 0
Source/Urho3D/LuaScript/pkgs/Math/Vector3.pkg

@@ -28,6 +28,8 @@ class Vector3
     float ProjectOntoAxis(const Vector3& axis) const;
     Vector3 ProjectOntoPlane(const Vector3& origin, const Vector3& normal);
     Vector3 ProjectOntoLine(const Vector3& from, const Vector3& to, bool clamped = false);
+    float DistanceToPoint(const Vector3& point) const;
+    float DistanceToPlane(const Vector3& origin, const Vector3& normal) const;
     Vector3 Orthogonalize(const Vector3& axis) const;
     Vector3 CrossProduct(const Vector3& rhs) const;
     Vector3 Abs() const;

+ 6 - 0
Source/Urho3D/Math/Vector3.h

@@ -401,6 +401,12 @@ public:
         return from + direction * factor;
     }
 
+    /// Calculate distance to another position vector.
+    float DistanceToPoint(const Vector3& point) const { return (*this - point).Length(); }
+
+    /// Calculate distance to the plane with given origin and normal.
+    float DistanceToPlane(const Vector3& origin, const Vector3& normal) const { return (*this - origin).ProjectOntoAxis(normal); }
+
     /// Make vector orthogonal to the axis.
     Vector3 Orthogonalize(const Vector3& axis) const { return axis.CrossProduct(*this).CrossProduct(axis).Normalized(); }