|
Posted on 2016-11-08 21:15 點點滴滴 閱讀(466) 評論(0) 編輯 收藏 引用 所屬分類: 19 源碼收集
翻譯:kun 2014.12.4
The navigation mesh query is the most important class to understand since most navigation clients will interact with the query rather than the navigation mesh itself. The mesh contains the data, but the query provides almost all of the features necessary for pathfinding. 用戶和navigation mesh query(Navmesh查詢器)打交道的次數(shù)比Navmes本身多的多。網(wǎng)格包含數(shù)據(jù),但是查詢器提供了幾乎全部的尋路特性。 Core Class: NavmeshQuery核心類:NavmeshQueryQuery features fall into two general categories: Pathfinding and local search.查詢器的特性包括兩個大的方面:尋路和局部搜索。Pathfinding involves standard A* and Dijkstra searches that find the best path(s) between two points. Paths are made up of a list polygon references that represent a polygon corridor from the start to the end position. Path straightening is used to convert the path into a list of waypoints. (I.e. String pulling.)尋路使用標準的A*和Dijkstra算法,用于找出兩點之間最好的路徑(可能不止一條)。Path(路徑)是由一組Polygon的引用(1)組成數(shù)據(jù),從開始點到結束點。路徑矯正是將一個Path數(shù)據(jù)轉(zhuǎn)為一組路點數(shù)據(jù)(即String pulling(繩子拉直))(2)。The local search features offer various methods for locating polygons and points on polygons, and for querying the local environment. I.e. Raycasting, finding the distance to the nearest wall, etc.局部搜索功能提供多種方式進行多邊形和多邊形上的點的定位,以及查詢局部的一些環(huán)境信息。比如射線查詢、計算離最近的墻的距離之類的。Many of the query methods require a NavmeshQueryFilter. Filters define area traversal costs as well as flags used for including/excluding polygons and off-mesh connections from results.許多查詢方法要求一個NavmeshQueryFilter(查詢過濾器)。過濾器定義了Polygon和off-mesh connections的穿越代價,代價值可以參與啟發(fā)式的計算,用來決定是否在最終路徑里包含/排除某個Polygon或off-mesh connections。The best way to understand the query class is to play around with it. The Sample Pack includes the Query Explorer demo that permits experimentation with all of the main query features.理解查詢器的最好辦法就是用一用。【Sample Pack(示例包)】里的【Query Explorer(查詢演示)】demo展示了查詢器一些主要的特性。Common Operations通用操作This section contains some simple examples of common query operations.Finding a Point in the Navigation MeshYou can't do much without first getting a valid point on the navigation mesh. So the first step is to find one.GetNearestPoint(Vector3, Vector3, NavmeshQueryFilter, NavmeshPoint)本節(jié)是例子查詢一個在Navmesh上的點如果你沒有一個Navmesh上的起始點的話,你能做的不多。所以第一部就是找一個點。GetNearestPoint(Vector3, Vector3, NavmeshQueryFilter, NavmeshPoint)函數(shù)提供此功能。CopyC#// Where 'query' is a NavmeshQuery object and 'filter' is a NavmeshQueryFilter object.// 'position' is a Vector3 indicating the world position of the client.// 'query'是一個 NavmeshQuery對象,'filter'是一個NavmeshQueryFilter對象。// 'position' 是一個Vector3對象,值為角色的世界坐標。NavmeshPoint result;Vector3 extents = new Vector3(1, 1, 1); // Keep this to the minimum extents practical. // 范圍越小越好。NavStatus status = query.GetNearestPoly(position, extents, filter , out result);if (result.polyRef == Navmesh.NullPoly){ // Handle error. Could not find a result. // The status can be checked to see if there was an error. If not, then // the cause is that the search extents did not overlap any polygons. // 錯誤處理。找不到結果。 // 可以檢查狀態(tài)看看是什么問題。如果沒有問題,說明指定范圍里不包含多邊形。}// Use the result point, which includes a vector point and the reference of // the polygon that contains the point.//使用結果點。包括一個Vector3的點和包含這個點的Polygon的引用。Basic PathfindingEven if you are planning to use PathCorridor or CrowdManager, you'll always need to do long distance planning using the basic NavmeshQuery features. First, get a path, then optionally straighten it.基礎的尋徑即使你打算使用pathcorridor或crowdmanager,你也會需要NavmeshQuery的功能來完成一些長距離路徑規(guī)劃。第一,獲取一條路徑,然后選擇性的弄直它(3)。CopyC#// Where 'query' is a NavmeshQuery object and 'filter' is a NavmeshQueryFilter object.// 'start' and 'end' are NavmeshPoints known to be on the navigation mesh.// 'query'是一個 NavmeshQuery對象,'filter'是一個NavmeshQueryFilter對象;// 'start' 和 'end' 是已知的在Navmesh上的點;int pathCount;// The path will be a list of polygon references.// path是一組polygon的引用;uint[] path = new uint[100]; // Size for maximum allowed path length. // 路徑的最大長度;NavStatus status;if (start.polyRef == end.polyRef){ // No need to do any planning. // 開始點和結束點在同一個多邊形內(nèi),不需要進行路徑規(guī)劃; pathCount = 1; path[0] = start.polyRef;}else{ status = query.FindPath(start, end, filter, path , out pathCount); if (NavUtil.Failed(status) || path.pathCount == 0) { // Handle pathfinding failure. // 處理尋路失敗; } else if (end.polyRef != path[pathCount - 1]) { // Handle a partial path. // The query either could not reach the end point, // or the path buffer was too small to hold the // entire path. (A check of 'status' will reveal if // the buffer was too small.) // 處理只有一部分路徑的情況; // 可能是結束點不可達; // 或者路徑的buffer長度太小,裝不下整條路徑; // 如果是buffer太小,可以檢查state; }}// If you need to straighten the path...// 如果你需要拉直路徑;const int MaxStraightPath = 4; // Just getting the first 4 waypoints. // 只處理前4個路點;int wpCount;// The waypoints.// 路點列表;Vector3[] wpPoints = new Vecotr3[MaxStraightPath];// A list of polygon references. (The polygon being entered at each waypoint.)// 一個多邊形引用列表;(路點是多邊形的入口點)uint[] wpPath = new uint[MaxStraightPath];// The type of each waypoint. (Start, end, off-mesh connection.)// 每一個路點的類型信息.(開始點,結束點, 連接);WaypointFlag[] wpFlags = new WaypointFlag[MaxStraightPath];status = query.GetStraightPath(start.point , goal.point , path , 0 // The index of the start of the path. // 路徑的開始點; , pathCount // The length of the path. // 路徑的長度; , wpPoints , wpFlags , wpPath , out wpCount);if (NavUtil.Failed(status) || wpCount == 0){ // Handle the failure. There should always be at least one waypoint // (the goal) for a valid point/path combination, // 處理失敗。應該總是存在一個點(目標點)用于路徑合并.}// Use the path and waypoints.// 使用路徑和路點;(1)可以理解為句柄,或索引。(2)Path上每個多邊形中心之間的連線一般不會是直線,路徑如果有拐角也會造成一些美觀上的問題,這個時候需要使用特定的方法將路徑變得盡量筆直,就好像將一根繩子拉直。
|