示例#1
0
        /**
         * Returns the nearest node to a position using the specified NNConstraint.
         * Searches through all graphs for their nearest nodes to the specified position and picks the closest one.
         * The NNConstraint can be used to specify constraints on which nodes can be chosen such as only picking walkable nodes.
         * \see Pathfinding.NNConstraint
         */
        public static NNInfo GetNearest(Vector3 position, NNConstraint constraint, GraphNode hint)
        {
            // Cache property lookup
            var localGraphs = GetConfig().graphs;

            float          minDist      = float.PositiveInfinity;
            NNInfoInternal nearestNode  = new NNInfoInternal();
            int            nearestGraph = -1;

            if (localGraphs != null)
            {
                for (int i = 0; i < localGraphs.Length; i++)
                {
                    NavGraph graph = localGraphs[i];

                    // Check if this graph should be searched
                    if (graph == null || !constraint.SuitableGraph(i, graph))
                    {
                        continue;
                    }

                    NNInfoInternal nnInfo;
                    if (GetConfig().fullGetNearestSearch)
                    {
                        // Slower nearest node search
                        // this will try to find a node which is suitable according to the constraint
                        nnInfo = graph.GetNearestForce(position, constraint);
                    }
                    else
                    {
                        // Fast nearest node search
                        // just find a node close to the position without using the constraint that much
                        // (unless that comes essentially 'for free')
                        nnInfo = graph.GetNearest(position, constraint);
                    }

                    GraphNode node = nnInfo.node;

                    // No node found in this graph
                    if (node == null)
                    {
                        continue;
                    }

                    // Distance to the closest point on the node from the requested position
                    float dist = ((Vector3)nnInfo.clampedPosition - position).magnitude;

                    if (GetConfig().prioritizeGraphs&& dist < GetConfig().prioritizeGraphsLimit)
                    {
                        // The node is close enough, choose this graph and discard all others
                        minDist      = dist;
                        nearestNode  = nnInfo;
                        nearestGraph = i;
                        break;
                    }
                    else
                    {
                        // Choose the best node found so far
                        if (dist < minDist)
                        {
                            minDist      = dist;
                            nearestNode  = nnInfo;
                            nearestGraph = i;
                        }
                    }
                }
            }

            // No matches found
            if (nearestGraph == -1)
            {
                return(new NNInfo());
            }

            // Check if a constrained node has already been set
            if (nearestNode.constrainedNode != null)
            {
                nearestNode.node            = nearestNode.constrainedNode;
                nearestNode.clampedPosition = nearestNode.constClampedPosition;
            }

            if (!GetConfig().fullGetNearestSearch&& nearestNode.node != null && !constraint.Suitable(nearestNode.node))
            {
                // Otherwise, perform a check to force the graphs to check for a suitable node
                NNInfoInternal nnInfo = localGraphs[nearestGraph].GetNearestForce(position, constraint);

                if (nnInfo.node != null)
                {
                    nearestNode = nnInfo;
                }
            }

            if (!constraint.Suitable(nearestNode.node) || (constraint.constrainDistance &&
                                                           (nearestNode.clampedPosition - position).sqrMagnitude > GetConfig().maxNearestNodeDistanceSqr))
            {
                return(new NNInfo());
            }

            // Convert to NNInfo which doesn't have all the internal fields
            return(new NNInfo(nearestNode));
        }
示例#2
0
 /** Searches for a node which contains the specified point.
  * If there are multiple nodes that contain the point any one of them
  * may be returned.
  *
  * \see TriangleMeshNode.ContainsPoint
  */
 public TriangleMeshNode QueryInside(Vector3 p, NNConstraint constraint)
 {
     return(count != 0 && tree[0].Contains(p) ? SearchBoxInside(0, p, constraint) : null);
 }
示例#3
0
 /**
  * Returns the nearest node to a position using the specified NNConstraint.
  * Searches through all graphs for their nearest nodes to the specified position and picks the closest one.
  * The NNConstraint can be used to specify constraints on which nodes can be chosen such as only picking walkable nodes.
  *
  * \snippet MiscSnippets.cs AstarPath.GetNearest2
  *
  * \snippet MiscSnippets.cs AstarPath.GetNearest3
  *
  * \see Pathfinding.NNConstraint
  */
 public static NNInfo GetNearest(Vector3 position, NNConstraint constraint)
 {
     return(GetNearest(position, constraint, null));
 }
示例#4
0
 /** Queries the tree for the closest node to \a p constrained by the NNConstraint.
  * Note that this function will only fill in the constrained node.
  * If you want a node not constrained by any NNConstraint, do an additional search with constraint = NNConstraint.None
  */
 public NNInfoInternal QueryClosest(Vector3 p, NNConstraint constraint, out float distance)
 {
     distance = float.PositiveInfinity;
     return(QueryClosest(p, constraint, ref distance, new NNInfoInternal(null)));
 }