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)); } }
/// <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; }
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)); } }
/// <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); }