Example #1
0
        private void AddGraphObstacles(Simulator sim, INavmesh ng)
        {
            int[] uses = new int[3];
            Dictionary <int, int>  outline         = new Dictionary <int, int>();
            Dictionary <int, Int3> vertexPositions = new Dictionary <int, Int3>();
            HashSet <int>          hasInEdge       = new HashSet <int>();

            ng.GetNodes(delegate(GraphNode _node)
            {
                TriangleMeshNode triangleMeshNode = _node as TriangleMeshNode;
                uses[0] = (uses[1] = (uses[2] = 0));
                if (triangleMeshNode != null)
                {
                    for (int i = 0; i < triangleMeshNode.connections.Length; i++)
                    {
                        TriangleMeshNode triangleMeshNode2 = triangleMeshNode.connections[i].node as TriangleMeshNode;
                        if (triangleMeshNode2 != null)
                        {
                            int num = triangleMeshNode.SharedEdge(triangleMeshNode2);
                            if (num != -1)
                            {
                                uses[num] = 1;
                            }
                        }
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        if (uses[j] == 0)
                        {
                            int i2 = j;
                            int i3 = (j + 1) % triangleMeshNode.GetVertexCount();
                            outline[triangleMeshNode.GetVertexIndex(i2)] = triangleMeshNode.GetVertexIndex(i3);
                            hasInEdge.Add(triangleMeshNode.GetVertexIndex(i3));
                            vertexPositions[triangleMeshNode.GetVertexIndex(i2)] = triangleMeshNode.GetVertex(i2);
                            vertexPositions[triangleMeshNode.GetVertexIndex(i3)] = triangleMeshNode.GetVertex(i3);
                        }
                    }
                }
            });
            List <Vector3> vertices = ListPool <Vector3> .Claim();

            RVONavmesh.CompressContour(outline, hasInEdge, delegate(List <int> chain, bool cycle)
            {
                for (int i = 0; i < chain.Count; i++)
                {
                    vertices.Add((Vector3)vertexPositions[chain[i]]);
                }
                this.obstacles.Add(sim.AddObstacle(vertices.ToArray(), this.wallHeight, cycle));
                vertices.Clear();
            });
            ListPool <Vector3> .Release(vertices);
        }
Example #2
0
        public void AddGraphObstacles(Simulator sim, NavGraph graph)
        {
            if (this.obstacles.Count > 0 && this.lastSim != null && this.lastSim != sim)
            {
                Debug.LogError("Simulator has changed but some old obstacles are still added for the previous simulator. Deleting previous obstacles.");
                this.RemoveObstacles();
            }
            this.lastSim = sim;
            INavmesh navmesh = graph as INavmesh;

            if (navmesh == null)
            {
                return;
            }
            int[] uses = new int[20];
            navmesh.GetNodes(delegate(GraphNode _node)
            {
                TriangleMeshNode triangleMeshNode = _node as TriangleMeshNode;
                uses[0] = (uses[1] = (uses[2] = 0));
                if (triangleMeshNode != null)
                {
                    for (int i = 0; i < triangleMeshNode.connections.Length; i++)
                    {
                        TriangleMeshNode triangleMeshNode2 = triangleMeshNode.connections[i] as TriangleMeshNode;
                        if (triangleMeshNode2 != null)
                        {
                            int num = triangleMeshNode.SharedEdge(triangleMeshNode2);
                            if (num != -1)
                            {
                                uses[num] = 1;
                            }
                        }
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        if (uses[j] == 0)
                        {
                            Vector3 a = (Vector3)triangleMeshNode.GetVertex(j);
                            Vector3 b = (Vector3)triangleMeshNode.GetVertex((j + 1) % triangleMeshNode.GetVertexCount());
                            float val = Math.Abs(a.y - b.y);
                            val       = Math.Max(val, 5f);
                            this.obstacles.Add(sim.AddObstacle(a, b, this.wallHeight));
                        }
                    }
                }
                return(true);
            });
        }
Example #3
0
        public void AddGraphObstacles(Simulator sim, NavGraph graph)
        {
            if (this.obstacles.get_Count() > 0 && this.lastSim != null && this.lastSim != sim)
            {
                this.RemoveObstacles();
            }
            this.lastSim = sim;
            INavmesh navmesh = graph as INavmesh;

            if (navmesh == null)
            {
                return;
            }
            int[] uses = new int[20];
            navmesh.GetNodes(delegate(GraphNode _node)
            {
                TriangleMeshNode triangleMeshNode = _node as TriangleMeshNode;
                uses[0] = (uses[1] = (uses[2] = 0));
                if (triangleMeshNode != null)
                {
                    for (int i = 0; i < triangleMeshNode.connections.Length; i++)
                    {
                        TriangleMeshNode triangleMeshNode2 = triangleMeshNode.connections[i] as TriangleMeshNode;
                        if (triangleMeshNode2 != null)
                        {
                            int num = triangleMeshNode.SharedEdge(triangleMeshNode2);
                            if (num != -1)
                            {
                                uses[num] = 1;
                            }
                        }
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        if (uses[j] == 0)
                        {
                            VInt3 vertex  = triangleMeshNode.GetVertex(j);
                            VInt3 vertex2 = triangleMeshNode.GetVertex((j + 1) % triangleMeshNode.GetVertexCount());
                            this.obstacles.Add(sim.AddObstacle(vertex, vertex2, this.wallHeight));
                        }
                    }
                }
                return(true);
            });
        }
Example #4
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;
            }

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

                            //Add the edge as a line obstacle
                            obstacles.Add(sim.AddObstacle(v1, v2, wallHeight));
                        }
                    }
                }

                return(true);
            });
        }
Example #5
0
        public void AddGraphObstacles(Simulator sim, INavmesh ng)
        {
            if (this.obstacles.Count > 0 && this.lastSim != null && this.lastSim != sim)
            {
                Debug.LogError("Simulator has changed but some old obstacles are still added for the previous simulator. Deleting previous obstacles.");
                this.RemoveObstacles();
            }
            this.lastSim = sim;
            int[] uses = new int[20];
            Dictionary <int, int>  outline         = new Dictionary <int, int>();
            Dictionary <int, Int3> vertexPositions = new Dictionary <int, Int3>();
            HashSet <int>          hasInEdge       = new HashSet <int>();

            ng.GetNodes(delegate(GraphNode _node)
            {
                TriangleMeshNode triangleMeshNode = _node as TriangleMeshNode;
                uses[0] = (uses[1] = (uses[2] = 0));
                if (triangleMeshNode != null)
                {
                    for (int j = 0; j < triangleMeshNode.connections.Length; j++)
                    {
                        TriangleMeshNode triangleMeshNode2 = triangleMeshNode.connections[j] as TriangleMeshNode;
                        if (triangleMeshNode2 != null)
                        {
                            int num3 = triangleMeshNode.SharedEdge(triangleMeshNode2);
                            if (num3 != -1)
                            {
                                uses[num3] = 1;
                            }
                        }
                    }
                    for (int k = 0; k < 3; k++)
                    {
                        if (uses[k] == 0)
                        {
                            int i2 = k;
                            int i3 = (k + 1) % triangleMeshNode.GetVertexCount();
                            outline[triangleMeshNode.GetVertexIndex(i2)] = triangleMeshNode.GetVertexIndex(i3);
                            hasInEdge.Add(triangleMeshNode.GetVertexIndex(i3));
                            vertexPositions[triangleMeshNode.GetVertexIndex(i2)] = triangleMeshNode.GetVertex(i2);
                            vertexPositions[triangleMeshNode.GetVertexIndex(i3)] = triangleMeshNode.GetVertex(i3);
                        }
                    }
                }
                return(true);
            });
            for (int i = 0; i < 2; i++)
            {
                bool flag = i == 1;
                foreach (int num in new List <int>(outline.Keys))
                {
                    if (flag || !hasInEdge.Contains(num))
                    {
                        int            key  = num;
                        List <Vector3> list = new List <Vector3>();
                        list.Add((Vector3)vertexPositions[key]);
                        while (outline.ContainsKey(key))
                        {
                            int num2 = outline[key];
                            outline.Remove(key);
                            Vector3 item = (Vector3)vertexPositions[num2];
                            list.Add(item);
                            if (num2 == num)
                            {
                                break;
                            }
                            key = num2;
                        }
                        if (list.Count > 1)
                        {
                            sim.AddObstacle(list.ToArray(), this.wallHeight, flag);
                        }
                    }
                }
            }
        }
Example #6
0
    private static void SearchReflectionPointFromIndex(Vector3d startPosi, int index, FixedABPath path, List <Vector3d> points)
    {
        var      graph          = AstarPath.active.graphs[0] as RecastGraph;
        bool     findStartPoint = true;
        Vector3d left           = new Vector3d();
        Vector3d right          = new Vector3d();
        int      leftIndex      = -1;
        int      rightIndex     = -1;
        int      startPosiIndex = -1;

        for (int i = index; i < path.path.Count; i++)
        {
            if (i != 0)
            {
                TriangleMeshNode prenode = path.path[i - 1] as TriangleMeshNode;
                TriangleMeshNode curnode = path.path[i] as TriangleMeshNode;
                int a, b;
                a = prenode.SharedEdge(curnode);
                b = a == 2?0:a + 1;
                Int3 l = new Int3(); Int3 r = new Int3();
                if (a != -1)
                {
                    l = prenode.GetVertex(a);
                }
                if (b != -1)
                {
                    r = prenode.GetVertex(b);
                }
                if (findStartPoint)
                {
                    left           = Int3.ToVector3D(l);
                    right          = Int3.ToVector3D(r);
                    leftIndex      = i;
                    rightIndex     = i;
                    findStartPoint = false;
                }
                else
                {
                    var tempL     = Int3.ToVector3D(l);
                    var tempR     = Int3.ToVector3D(r);
                    var relationL = GetRelation(left - startPosi, right - startPosi, tempL - startPosi);
                    var relationR = GetRelation(left - startPosi, right - startPosi, tempR - startPosi);
                    //寻找拐点
                    if (relationR == VectorRelation.Left && relationL == VectorRelation.Left)
                    {
                        bool add = AddPoint(left, points);
                        startPosi      = left;
                        startPosiIndex = leftIndex;
                        if (add)
                        {
                            SpawnGO(left, i);
                        }
                        break;
                    }
                    else if (relationR == VectorRelation.Right && relationL == VectorRelation.Right)
                    {
                        bool add = AddPoint(right, points);
                        startPosi      = right;
                        startPosiIndex = rightIndex;
                        if (add)
                        {
                            SpawnGO(right, i);
                        }
                        break;
                    }
                    else if (relationR == VectorRelation.Equal && relationL == VectorRelation.Right)
                    {
                        bool add = AddPoint(right, points);
                        startPosi      = right;
                        startPosiIndex = rightIndex;
                        if (add)
                        {
                            SpawnGO(right, i);
                        }
                        break;
                    }
                    else if (relationR == VectorRelation.Equal && relationL == VectorRelation.Left)
                    {
                        bool add = AddPoint(left, points);
                        startPosi      = left;
                        startPosiIndex = leftIndex;
                        if (add)
                        {
                            SpawnGO(left, i);
                        }
                        break;
                    }
                    //else if (relationL == VectorRelation.Equal && relationR == VectorRelation.Left)
                    //{
                    //    bool add = AddPoint(left, points);
                    //    startPosi = left;
                    //    startPosiIndex = i;
                    //    if (add)
                    //        SpawnGO(left, i);
                    //    break;
                    //}
                    //else if (relationL == VectorRelation.Equal && relationR == VectorRelation.Right)
                    //{
                    //    bool add = AddPoint(right, points);
                    //    startPosi = right;
                    //    startPosiIndex = i;
                    //    if(add)
                    //        SpawnGO(right, i);
                    //    break;
                    //}
                    //改变左右
                    if (relationR == VectorRelation.Inside && relationL == VectorRelation.Inside)
                    {
                        left       = tempL;
                        right      = tempR;
                        leftIndex  = i;
                        rightIndex = i;
                    }
                    if (relationL == VectorRelation.Inside || relationL == VectorRelation.Equal)
                    {
                        left      = tempL;
                        leftIndex = i;
                    }
                    if (relationR == VectorRelation.Inside || relationR == VectorRelation.Equal)
                    {
                        right      = tempR;
                        rightIndex = i;
                    }
                    if (i == path.path.Count - 1)
                    {
                        var relationEndPoint = GetRelation(left - startPosi, right - startPosi,
                                                           path.EndPoint - startPosi);
                        if (relationEndPoint == VectorRelation.Left)
                        {
                            AddPoint(left, points);
                            SpawnGO(left, i);
                        }
                        else if (relationEndPoint == VectorRelation.Right)
                        {
                            AddPoint(right, points);
                            SpawnGO(right, i);
                        }
                    }
                }
            }
        }
        if (startPosiIndex != -1)
        {
            startPosiIndex++;
            SearchReflectionPointFromIndex(startPosi, startPosiIndex, path, points);
        }
    }