Ejemplo n.º 1
0
    /** Adds obstacles for a graph */
    public void AddGraphObstacles(Pathfinding.RVO.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;

        Node[] nodes = graph.nodes;

        Int3[] vertices = ng.vertices;

        int[] uses = new int[3];

        for (int i=0;i<nodes.Length;i++) {
            MeshNode node = nodes[i] as MeshNode;

            uses[0] = uses[1] = uses[2] = 0;

            if (node != null) {

                for (int j=0;j<node.connections.Length;j++) {
                    MeshNode other = node.connections[j] as MeshNode;
                    if (other == null) continue;

                    int first = -1;
                    int second = -1;

                    for (int x=0;x<3;x++) {
                        for (int y=0;y<3;y++) {
                            if (node[x] == other[y] && first < 0) {
                                first = x;
                                break;
                            } else if (node[x] == other[y]) {
                                second = x;
                                break;
                            }
                        }
                        if (second >= 0) break;
                    }

                    //Only shared one vertex
                    if (second == -1) continue;

                    if ((first+1) % 3 == second) {
                        uses[first]++;
                    } else { //if ((second+1) % 3 == first) {
                        uses[second]++;
                    }
                }
            }

            for (int j=0;j<3;j++) {
                if (uses[j] == 0) {
                    Vector3 v1 = (Vector3)vertices[node[j]];
                    Vector3 v2 = (Vector3)vertices[node[(j+1) % 3]];

                    //I think node vertices always should be clockwise, but it's good to be certain
                    if (!Polygon.IsClockwise (v1,v2,(Vector3)vertices[node[(j+2) % 3]])) {
                        Vector3 tmp = v2;
                        v2 = v1;
                        v1 = tmp;
                    }

        #if ASTARDEBUG
                    Debug.DrawLine (v1,v2,Color.red);
                    Debug.DrawRay (v1,Vector3.up*wallHeight,Color.red);
        #endif

                    float height = System.Math.Abs(v1.y-v2.y);
                    height = System.Math.Max (height,5);

                    obstacles.Add (sim.AddObstacle (v1, v2, wallHeight));
                }
            }
        }
    }