GetNearest() public method

public GetNearest ( Vector3 position, NNConstraint constraint, Pathfinding.Node hint ) : NNInfo
position UnityEngine.Vector3
constraint NNConstraint
hint Pathfinding.Node
return NNInfo
Esempio n. 1
0
        //GraphUndo undoState;
        //byte[] savedBytes;

        public override void OnSceneGUI(NavGraph target)
        {
            Event e = Event.current;



            GridGraph graph = target as GridGraph;

            Matrix4x4 matrixPre = graph.matrix;

            graph.GenerateMatrix();

            if (e.type == EventType.MouseDown)
            {
                isMouseDown = true;
            }
            else if (e.type == EventType.MouseUp)
            {
                isMouseDown = false;
            }

            if (!isMouseDown)
            {
                savedMatrix = graph.boundsMatrix;
            }

            Handles.matrix = savedMatrix;

            if ((graph.GetType() == typeof(GridGraph) && graph.nodes == null) || (graph.uniformWidthDepthGrid && graph.depth * graph.width != graph.nodes.Length) || graph.matrix != matrixPre)
            {
                //Rescan the graphs
                if (AutoScan())
                {
                    GUI.changed = true;
                }
            }

            Matrix4x4 inversed = savedMatrix.inverse;

            Handles.color = AstarColor.BoundsHandles;

            Handles.DrawCapFunction cap = Handles.CylinderCap;

            Vector2 extents = graph.unclampedSize * 0.5F;

            Vector3 center = inversed.MultiplyPoint3x4(graph.center);


        #if UNITY_3_3
            if (Tools.current == 3)
            {
        #else
            if (Tools.current == Tool.Scale)
            {
        #endif

                Vector3 p1 = Handles.Slider(center + new Vector3(extents.x, 0, 0), Vector3.right, 0.1F * HandleUtility.GetHandleSize(center + new Vector3(extents.x, 0, 0)), cap, 0);
                Vector3 p2 = Handles.Slider(center + new Vector3(0, 0, extents.y), Vector3.forward, 0.1F * HandleUtility.GetHandleSize(center + new Vector3(0, 0, extents.y)), cap, 0);
                //Vector3 p3 = Handles.Slider (center+new Vector3 (0,extents.y,0),	Vector3.up,			0.1F*HandleUtility.GetHandleSize (center+new Vector3 (0,extents.y,0)),cap,0);

                Vector3 p4 = Handles.Slider(center + new Vector3(-extents.x, 0, 0), -Vector3.right, 0.1F * HandleUtility.GetHandleSize(center + new Vector3(-extents.x, 0, 0)), cap, 0);
                Vector3 p5 = Handles.Slider(center + new Vector3(0, 0, -extents.y), -Vector3.forward, 0.1F * HandleUtility.GetHandleSize(center + new Vector3(0, 0, -extents.y)), cap, 0);

                Vector3 p6 = Handles.Slider(center, Vector3.up, 0.1F * HandleUtility.GetHandleSize(center), cap, 0);

                Vector3 r1 = new Vector3(p1.x, p6.y, p2.z);
                Vector3 r2 = new Vector3(p4.x, p6.y, p5.z);

                //Debug.Log (graph.boundsMatrix.MultiplyPoint3x4 (Vector3.zero)+" "+graph.boundsMatrix.MultiplyPoint3x4 (Vector3.one));

                //if (Tools.viewTool != ViewTool.Orbit) {

                graph.center = savedMatrix.MultiplyPoint3x4((r1 + r2) / 2F);

                Vector3 tmp = r1 - r2;
                graph.unclampedSize = new Vector2(tmp.x, tmp.z);

                //}

        #if UNITY_3_3
            }
            else if (Tools.current == 1)
            {
        #else
            }
            else if (Tools.current == Tool.Move)
            {
        #endif

                if (Tools.pivotRotation == PivotRotation.Local)
                {
                    center = Handles.PositionHandle(center, Quaternion.identity);

                    if (Tools.viewTool != ViewTool.Orbit)
                    {
                        graph.center = savedMatrix.MultiplyPoint3x4(center);
                    }
                }
                else
                {
                    Handles.matrix = Matrix4x4.identity;

                    center = Handles.PositionHandle(graph.center, Quaternion.identity);

                    if (Tools.viewTool != ViewTool.Orbit)
                    {
                        graph.center = center;
                    }
                }
        #if UNITY_3_3
            }
            else if (Tools.current == 2)
            {
        #else
            }
            else if (Tools.current == Tool.Rotate)
            {
        #endif
                //The rotation handle doesn't seem to be able to handle different matrixes of some reason
                Handles.matrix = Matrix4x4.identity;

                Quaternion rot = Handles.RotationHandle(Quaternion.Euler(graph.rotation), graph.center);

                if (Tools.viewTool != ViewTool.Orbit)
                {
                    graph.rotation = rot.eulerAngles;
                }
            }

            //graph.size.x = Mathf.Max (graph.size.x,1);
            //graph.size.y = Mathf.Max (graph.size.y,1);
            //graph.size.z = Mathf.Max (graph.size.z,1);

            Handles.matrix = Matrix4x4.identity;



        #if ASTARDEBUG
            //Draws some info over the node closest to the mouse
            Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

            Vector3 p = ray.GetPoint(100);


            if (Event.current.shift)
            {
                GraphNode close = graph.GetNearest(p).node;

                if (close != null)
                {
                    node1 = close;
                }

                if (node1 == null)
                {
                    return;
                }

                Handles.SphereCap(0, (Vector3)node1.position, Quaternion.identity, graph.nodeSize * 0.5F);


                //Node node = node1;

                GUI.color = Color.white;
                //Handles.Label((Vector3)node.position + Vector3.up*2,"G : "+node.+"\nH : "+node.h+"\nF : "+node.f+"\nPosition : "+node.position.ToString (),EditorStyles.whiteBoldLabel);
            }
        #endif
        }
Esempio n. 2
0
    // GetPotentialMap calculate the potential map based on generated grid from A* and target position
    // Input:
    //      Vector3 target
    //      Pathfing.GridGraph
    // Output:
    //      int[,] resultMap

    public int[,] GetPotentialMap(Vector3 target, Pathfinding.GridGraph gridGraph)
    {
        int[,] resultMap = new int[gridGraph.Depth, gridGraph.Width];
        for (int i = 0; i < resultMap.GetLength(0); i++)
        {
            for (int j = 0; j < resultMap.GetLength(1); j++)
            {
                resultMap[i, j] = largePenalty;
            }
        }

        int totalAvailable = 0;

        stepPerGrid = (gridGraph.nodeSize);

        float target_w = ((Vector3)gridGraph.GetNearest(target).node.position).x;  // x-coordinate
        float target_d = ((Vector3)gridGraph.GetNearest(target).node.position).z;  // y-coordinate

        int targetIndex_w = (int)((target_w) / stepPerGrid + gridGraph.width / 2);
        int targetIndex_d = (int)((-1 * target_d) / stepPerGrid + gridGraph.depth / 2);

        //Debug.Log("X in unit of grid size = " + ((target_w) / stepPerGrid).ToString() + ", and actual grid index = " + ((target_w) / stepPerGrid + gridGraph.width / 2).ToString());
        //Debug.Log("Calculated targetIndex:" + "row: " + targetIndex_d.ToString() + ", column: " + targetIndex_w);

        if (targetIndex_w >= gridGraph.width || targetIndex_w < 0 || targetIndex_d >= gridGraph.depth || targetIndex_d < 0)
        {
            Debug.Log("The target index is out of range of the map...");
            return(null);
        }
        // depth -> row, width -> col
        int current_potential = 0;

        resultMap[targetIndex_d, targetIndex_w] = current_potential;

        //searched HashSet
        HashSet <int> nodeIndicesSearched = new HashSet <int>();

        nodeIndicesSearched.Add(gridGraph.GetNearest(target).node.NodeIndex);

        //Queue<GraphNode> nodesQueue = new Queue<GraphNode>();
        //nodesQueue.Enqueue(gridGraph.GetNearest(target).node);


        Queue <List <float> > coordinateQueue = new Queue <List <float> >();

        coordinateQueue.Enqueue(new List <float> {
            target_w, target_d
        });


        List <List <int> > grid_Direct = new List <List <int> >();

        grid_Direct.Add(new List <int> {
            -1, 0
        });                                       // w-1 : left
        grid_Direct.Add(new List <int> {
            1, 0
        });                                       // w+1 : right
        grid_Direct.Add(new List <int> {
            0, -1
        });                                       // d-1 : up
        grid_Direct.Add(new List <int> {
            0, 1
        });                                       // d+1 : down

        while (coordinateQueue.Count != 0)
        {
            var   currentCoord        = coordinateQueue.Dequeue();
            float currentCoordinate_w = currentCoord[0];
            float currentCoordinate_d = currentCoord[1];

            int currentIndex_w = (int)((currentCoordinate_w) / stepPerGrid + gridGraph.width / 2);
            int currentIndex_d = (int)((-1 * currentCoordinate_d) / stepPerGrid + gridGraph.depth / 2);

            current_potential = resultMap[currentIndex_d, currentIndex_w];
            //Debug.Log("current location's potential = " + current_potential.ToString());

            foreach (var dir in grid_Direct)
            {
                int newIndex_w = currentIndex_w + dir[0];
                int newIndex_d = currentIndex_d + dir[1];

                if (newIndex_w >= 0 && newIndex_w < gridGraph.Width && newIndex_d >= 0 && newIndex_d < gridGraph.Depth)
                {
                    float newCoordinate_w = currentCoordinate_w + stepPerGrid * dir[0];
                    float newCoordinate_d = currentCoordinate_d - stepPerGrid * dir[1]; //note the minus sign for y direction

                    GraphNode newNode      = gridGraph.GetNearest(new Vector3(newCoordinate_w, 0, newCoordinate_d)).node;
                    int       newNodeIndex = newNode.NodeIndex;

                    if (!nodeIndicesSearched.Contains(newNodeIndex))
                    {
                        if (newNode.Walkable)
                        {
                            // if new grid is not visited before and is walkable
                            resultMap[newIndex_d, newIndex_w] = current_potential + 1;

                            // put the newly calculated point into queue
                            coordinateQueue.Enqueue(new List <float> {
                                newCoordinate_w, newCoordinate_d
                            });
                            totalAvailable += 1;
                        }

                        nodeIndicesSearched.Add(newNodeIndex);
                    }
                }
            }
        }
        //Debug.Log("If everything goes as expected, the size of nodeIndicesSearched should be slightly bigger than the walkable total area: " + nodeIndicesSearched.Count);
        //Debug.Log("Total available points should be this number (check the A* grid info)" + totalAvailable);

        return(resultMap);
    }