Ejemplo n.º 1
0
        public Path(
            PathManager pathManager,
            GameObject movingObject,
            Vector2 source,
            Vector2 destination,
            List <int> pathNodeIndices,
            SparseGraph graph)
        {
            PathEdgeList = new List <PathEdge>();
            Source       = source;
            Destination  = destination;
            PathManager  = pathManager;
            MovingObject = movingObject;

            int  fromNodeIndex = -1;
            int  toNodeIndex   = -1;
            Node fromNode      = null;
            Node toNode        = null;
            Edge currentEdge   = null;

            Vector2 from = source;

            foreach (int nodeIndex in pathNodeIndices)
            {
                fromNodeIndex = toNodeIndex;
                toNodeIndex   = nodeIndex;

                fromNode = toNode;
                toNode   = graph.GetNode(nodeIndex);

                Vector2 to = toNode.Position;
                if (from == to)
                {
                    // this could happen when source is exactly on a node
                    // position. In this case, skip this redundant edge
                    continue;
                }

                if (fromNodeIndex != -1 && toNodeIndex != -1)
                {
                    currentEdge = graph.GetEdge(fromNodeIndex, toNodeIndex);
                }

                PathEdgeList.Add(new PathEdge(from, to, fromNode, toNode, currentEdge, PathManager, MovingObject));
                from = to; // copy
            }

            if (from != destination)
            {
                PathEdgeList.Add(new PathEdge(from, destination, toNode, null, null, PathManager, MovingObject));
            }
        }
Ejemplo n.º 2
0
        public Path(
            PathManager pathManager,
            GameObject movingObject,
            Vector2 source,
            Vector2 destination,
            List<int> pathNodeIndices,
            SparseGraph graph)
        {
            PathEdgeList = new List<PathEdge>();
            Source = source;
            Destination = destination;
            PathManager = pathManager;
            MovingObject = movingObject;

            int fromNodeIndex = -1;
            int toNodeIndex = -1;
            Node fromNode = null;
            Node toNode = null;
            Edge currentEdge = null;

            Vector2 from = source;
            foreach (int nodeIndex in pathNodeIndices)
            {
                fromNodeIndex = toNodeIndex;
                toNodeIndex = nodeIndex;

                fromNode = toNode;
                toNode = graph.GetNode(nodeIndex);

                Vector2 to = toNode.Position;
                if (from == to)
                {
                    // this could happen when source is exactly on a node
                    // position. In this case, skip this redundant edge
                    continue;
                }

                if (fromNodeIndex != -1 && toNodeIndex != -1)
                {
                    currentEdge = graph.GetEdge(fromNodeIndex, toNodeIndex);
                }

                PathEdgeList.Add(new PathEdge(from, to, fromNode, toNode, currentEdge, PathManager, MovingObject));
                from = to; // copy
            }

            if (from != destination)
            {
                PathEdgeList.Add(new PathEdge(from, destination, toNode, null, null, PathManager, MovingObject));
            }
        }
Ejemplo n.º 3
0
        public AStarSearch(SparseGraph graph, int source, int target)
        {
            Graph = graph;
            Source = source;
            Target = target;
            ShortestPathTree = new List<Edge>(Graph.NumNodes);
            SearchFrontier = new List<Edge>(Graph.NumNodes);
            GCosts = new List<float>(Graph.NumNodes);
            FCosts = new List<float>(Graph.NumNodes);

            for (int i = 0; i < graph.NumNodes; i++)
            {
                ShortestPathTree.Add(null);
                SearchFrontier.Add(null);
                GCosts.Add(0);
                FCosts.Add(0);
            }

            Search(EuclideanDistance);
        }
Ejemplo n.º 4
0
        public AStarSearch(SparseGraph graph, int source, int target)
        {
            Graph            = graph;
            Source           = source;
            Target           = target;
            ShortestPathTree = new List <Edge>(Graph.NumNodes);
            SearchFrontier   = new List <Edge>(Graph.NumNodes);
            GCosts           = new List <float>(Graph.NumNodes);
            FCosts           = new List <float>(Graph.NumNodes);

            for (int i = 0; i < graph.NumNodes; i++)
            {
                ShortestPathTree.Add(null);
                SearchFrontier.Add(null);
                GCosts.Add(0);
                FCosts.Add(0);
            }

            Search(EuclideanDistance);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Calculate the straight line distance from node nd1 to node nd2.
 /// </summary>
 /// <param name="graph">The search graph.</param>
 /// <param name="nd1">The first node index.</param>
 /// <param name="nd2">The second node index.</param>
 /// <returns>The straight line distance from node nd1 to node nd2.</returns>
 public static float EuclideanDistance(SparseGraph graph, int nd1, int nd2)
 {
     return (graph.GetNode(nd1).Position - graph.GetNode(nd2).Position).magnitude;
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Calculate the straight line distance from node nd1 to node nd2.
 /// </summary>
 /// <param name="graph">The search graph.</param>
 /// <param name="nd1">The first node index.</param>
 /// <param name="nd2">The second node index.</param>
 /// <returns>The straight line distance from node nd1 to node nd2.</returns>
 public static float EuclideanDistance(SparseGraph graph, int nd1, int nd2)
 {
     return((graph.GetNode(nd1).Position - graph.GetNode(nd2).Position).magnitude);
 }