// Return path public static List <Vector3> ComputePath(ReducedVisibility singleton, Vector2 start, Vector2 end) { Global.pathPlanned++; Stopwatch sw = Stopwatch.StartNew(); Vector2 dir = end - start; Vector3 pos = new Vector3(start.x, start.y, Global.obstacleZ); List <Vector3> path; // if you can reach it directly if (!Physics.BoxCast(pos, Agent.halfExtents, dir, Quaternion.identity, dir.magnitude)) { Vector3 vDest = new Vector3(end.x, end.y, Global.agentZ); pos.z = Global.agentZ; path = new List <Vector3>() { vDest, pos }; } else { var g = new Graph(singleton, start, end); path = g.AStarSearch(); } sw.Stop(); Global.total += sw.Elapsed; return(path); }
public Graph(ReducedVisibility singleton, Vector2 start, Vector2 end) { List <GraphNode> nodes = new List <GraphNode>(singleton.vertices.Count + 2); orig = new GraphNode(new Vertex { value = start }, 0); dest = new GraphNode(new Vertex { value = end }, 0); foreach (var v in singleton.vertices) { var node = new GraphNode(v, v.neighbors.Count + 1); v.curdownstream = node; nodes.Add(node); } Vector3 startPos = new Vector3(start.x, start.y, Global.obstacleZ); Vector3 endPos = new Vector3(end.x, end.y, Global.obstacleZ); Vector3 tmp = new Vector3(); foreach (var node in nodes) { tmp.x = node.v.value.x; tmp.y = node.v.value.y; tmp.z = Global.obstacleZ; if (!Physics.Linecast(startPos, tmp)) { var cost = (start - node.v.value).magnitude; orig.neighbors.Add((node, cost)); node.neighbors.Add((orig, cost)); } if (!Physics.Linecast(endPos, tmp)) { var cost = (end - node.v.value).magnitude; dest.neighbors.Add((node, cost)); node.neighbors.Add((dest, cost)); } foreach (var(n, cost) in node.v.neighbors) { node.neighbors.Add((n.curdownstream, cost)); } } }
void ShowReducedVisabilityGraph() { Graph = new ReducedVisibility(shapes); foreach (var vertex in Graph.vertices) { Instantiate(Point, vertex.getValue(Global.graphZ), Quaternion.identity); } foreach (var(v1, v2) in Graph.edges) { var ret = Instantiate(Line); LineRenderer lr = ret.GetComponent <LineRenderer>(); var pos = new Vector3[] { v1.getValue(Global.graphZ), v2.getValue(Global.graphZ) }; lr.positionCount = pos.Length; lr.SetPositions(pos); lr.startColor = Color.blue; lr.endColor = Color.yellow; lr.startWidth = 0.05f; lr.endWidth = 0.05f; } }
void Start() { if (graph == null) { graph = FindObjectOfType <LevelManager>().Graph; } // Set Color var color = Random.ColorHSV(); color.a = 1; Renderer.color = color; var pos = Global.RandomPosition(halfExtents); pos.z = Global.agentZ; destination = Instantiate(destinationPrefab, pos, Quaternion.identity); destination.GetComponent <SpriteRenderer>().color = Renderer.color; GoToDestination(); }