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

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)
GetNearestPoint(Vector3, Vector3, NavmeshQueryFilter, NavmeshPoint)函数提供此功能。

// 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.

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.

// 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;
        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.
// 使用路径和路点;