Example #1
0
		/** Adds obstacles for a graph */
		public void AddGraphObstacles (Simulator sim, NavGraph graph) {
			if (obstacles.Count > 0 && lastSim != null && lastSim != sim) {
				Debug.LogError ("Simulator has changed but some old obstacles are still added for the previous simulator. Deleting previous obstacles.");
				RemoveObstacles ();
			}
			
			//Remember which simulator these obstacles were added to
			lastSim = sim;
			
			INavmesh ng = graph as INavmesh;
			
			if (ng == null) return;
			
			//Assume less than 20 vertices per node (actually assumes 3, but I will change that some day)
			int[] uses = new int[20];
			
			ng.GetNodes (delegate(GraphNode _node) {
				TriangleMeshNode node = _node as TriangleMeshNode;
				
				uses[0] = uses[1] = uses[2] = 0;
				
				if (node != null) {
					
					//Find out which edges are shared with other nodes
					for (int j=0;j<node.connections.Length;j++) {
						TriangleMeshNode other = node.connections[j] as TriangleMeshNode;
						
						// Not necessarily a TriangleMeshNode
						if (other != null) {
							int a = node.SharedEdge(other);
							if (a != -1) uses[a] = 1;
						}
					}
					
					//Loop through all edges on the node
					for (int j=0;j<3;j++) {
						//The edge is not shared with any other node
						//I.e it is an exterior edge on the mesh
						if (uses[j] == 0) {
							//The two vertices of the edge
							Vector3 v1 = (Vector3)node.GetVertex(j);
							Vector3 v2 = (Vector3)node.GetVertex((j+1) % node.GetVertexCount());
							
							//I think node vertices always should be clockwise, but it's good to be certain
							/*if (!Polygon.IsClockwise (v1,v2,(Vector3)node.GetVertex((j+2) % node.GetVertexCount()))) {
								Vector3 tmp = v2;
								v2 = v1;
								v1 = tmp;
							}*/
							
		#if ASTARDEBUG
							Debug.DrawLine (v1,v2,Color.red);
							Debug.DrawRay (v1,Vector3.up*wallHeight,Color.red);
		#endif
							
							//Find out the height of the wall/obstacle we are about to add
							float height = System.Math.Abs(v1.y-v2.y);
							height = System.Math.Max (height,5);
							
							//Enqueue the edge as a line obstacle
							obstacles.Add (sim.AddObstacle (v1, v2, wallHeight));
						}
					}
				}
				
				return true;
			});
			
		}