posts - 311, comments - 0, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

翻译: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查询器)打交道的次数比Navmes本身多的多。网格包含数据,但是查询器提供了几乎全部的寻路特性。



Core Class: NavmeshQuery
核心类:NavmeshQuery


Query 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)组成数据,从开始点到结束点。路径矫正是将一个Path数据转为一组路点数据(即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.
局部搜索功能提供多种方式进行多边形和多边形上的点的定位,以及查询局部的一些环境信息。比如射线查询、计算离最近的墙的距离之类的。


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的穿越代价,代价值可以参与启发式的计算,用来决定是否在最终路径里包含/排除某个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 Mesh
You 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)
本节是例子
查询一个在Navmesh上的点
如果你没有一个Navmesh上的起始点的话,你能做的不多。所以第一部就是找一个点。
GetNearestPoint(Vector3, Vector3, NavmeshQueryFilter, NavmeshPoint)函数提供此功能。


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.
        // 错误处理。找不到结果。
        // 可以检查状态看看是什么问题。如果没有问题,说明指定范围里不包含多边形。
}


// Use the result point, which includes a vector point and the reference of 
// the polygon that contains the point.
//使用结果点。包括一个Vector3的点和包含这个点的Polygon的引用。


Basic Pathfinding
Even 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的功能来完成一些长距离路径规划。第一,获取一条路径,然后选择性的弄直它(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.
        // 开始点和结束点在同一个多边形内,不需要进行路径规划;
        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上每个多边形中心之间的连线一般不会是直线,路径如果有拐角也会造成一些美观上的问题,这个时候需要使用特定的方法将路径变得尽量笔直,就好像将一根绳子拉直。