Browse Source

Fix return result of FindNearestPoint to transform back into world space, add method MoveAlongSurface and associated script bindings.

vertexnormal 12 years ago
parent
commit
e70eb17dcb

+ 1 - 0
Source/Engine/LuaScript/pkgs/Navigation/NavigationMesh.pkg

@@ -29,6 +29,7 @@ class NavigationMesh : public Component
     bool Build(const BoundingBox& boundingBox);
     
 	Vector3 FindNearestPoint(const Vector3& point, const Vector3& extents = Vector3::ONE);
+	Vector3 MoveAlongSurface(const Vector3& start, const Vector3& end, const Vector3& extents=Vector3::ONE, int maxVisited=3);
     // void FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, const Vector3& extents = Vector3::ONE);
     tolua_outside PODVector<Vector3> NavigationMeshFindPath @ FindPath(const Vector3& start, const Vector3& end, const Vector3& extents = Vector3::ONE);
     

+ 24 - 1
Source/Engine/Navigation/NavigationMesh.cpp

@@ -477,7 +477,30 @@ Vector3 NavigationMesh::FindNearestPoint(const Vector3& point, const Vector3& ex
 	if (!pointRef)
 		return point;
 		
-	return nearestPoint;
+	return transform*nearestPoint;
+}
+
+Vector3 NavigationMesh::MoveAlongSurface(const Vector3& start, const Vector3& end, const Vector3& extents, int maxVisited)
+{
+	if (!InitializeQuery())
+        return end;
+    
+    const Matrix3x4& transform = node_->GetWorldTransform();
+    Matrix3x4 inverse = transform.Inverse();
+    
+    Vector3 localStart = inverse * start;
+    Vector3 localEnd = inverse * end;
+    
+    dtPolyRef startRef;
+	navMeshQuery_->findNearestPoly(&localStart.x_, &extents.x_, queryFilter_, &startRef, 0);
+	if (!startRef)
+		return end;
+		
+	Vector3 resultPos;
+	int visitedCount=0;
+	dtPolyRef visited[maxVisited];
+	navMeshQuery_->moveAlongSurface(startRef, &localStart.x_, &localEnd.x_, queryFilter_, &resultPos.x_, visited, &visitedCount, maxVisited);
+	return transform*resultPos;
 }
 
 void NavigationMesh::FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, const Vector3& extents)

+ 2 - 0
Source/Engine/Navigation/NavigationMesh.h

@@ -102,6 +102,8 @@ public:
     bool Build(const BoundingBox& boundingBox);
 	/// Find the nearest point on the navigation mesh to a given point. Extens specifies how far out from the specified point to check along each axis.
 	Vector3 FindNearestPoint(const Vector3& point, const Vector3& extents=Vector3::ONE);
+	/// Try to move along the surface from one point to another
+	Vector3 MoveAlongSurface(const Vector3& start, const Vector3& end, const Vector3& extents=Vector3::ONE, int maxVisited=3);
     /// Find a path between world space points. Return non-empty list of points if successful. Extents specifies how far off the navigation mesh the points can be.
     void FindPath(PODVector<Vector3>& dest, const Vector3& start, const Vector3& end, const Vector3& extents = Vector3::ONE);
     /// Return a random point on the navigation mesh.

+ 1 - 0
Source/Engine/Script/NavigationAPI.cpp

@@ -49,6 +49,7 @@ void RegisterNavigationMesh(asIScriptEngine* engine)
     engine->RegisterObjectMethod("NavigationMesh", "bool Build()", asMETHODPR(NavigationMesh, Build, (void), bool), asCALL_THISCALL);
     engine->RegisterObjectMethod("NavigationMesh", "bool Build(const BoundingBox&in)", asMETHODPR(NavigationMesh, Build, (const BoundingBox&), bool), asCALL_THISCALL);
 	engine->RegisterObjectMethod("NavigationMesh", "Vector3 FindNearestPoint()", asMETHOD(NavigationMesh, FindNearestPoint), asCALL_THISCALL);
+	engine->RegisterObjectMethod("NavigationMesh", "Vector3 MoveAlongSurface()", asMETHOD(NavigationMesh, MoveAlongSurface), asCALL_THISCALL);
     engine->RegisterObjectMethod("NavigationMesh", "Array<Vector3>@ FindPath(const Vector3&in, const Vector3&in, const Vector3&in extents = Vector3(1.0, 1.0, 1.0))", asFUNCTION(NavigationMeshFindPath), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("NavigationMesh", "Vector3 GetRandomPoint()", asMETHOD(NavigationMesh, GetRandomPoint), asCALL_THISCALL);
     engine->RegisterObjectMethod("NavigationMesh", "Vector3 GetRandomPointInCircle(const Vector3&in, float, const Vector3&in extents = Vector3(1.0, 1.0, 1.0))", asMETHOD(NavigationMesh, GetRandomPointInCircle), asCALL_THISCALL);