a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

A*算法,偽代碼,源碼

2019-11-11 05:11:49
字體:
供稿:網(wǎng)友
轉(zhuǎn)載自博主 GottaYiWanLiu[點(diǎn)擊查看原文](http://blog.csdn.net/gottayiwanliu/article/details/54881256)A*   AStar   A星2d游戲,或者網(wǎng)格游戲中Cost f 總消耗Cost g 距離起點(diǎn)的消耗Cost h 距離終點(diǎn)的消耗默認(rèn)消耗,直走消耗10,斜著走消耗14開啟列表關(guān)閉列表父節(jié)點(diǎn)//開啟列表關(guān)閉列表開始循環(huán)(開啟列表有值) 當(dāng)前點(diǎn) = 開啟列表中最小的f_Cost  把當(dāng)前點(diǎn)從開啟列表刪除 把當(dāng)前點(diǎn)添加到關(guān)閉列表  If當(dāng)前點(diǎn)是終點(diǎn),跳出循環(huán) 點(diǎn)開一個(gè)紅點(diǎn),周圍的點(diǎn)會(huì)得到一個(gè)新的花費(fèi)循環(huán)周圍的點(diǎn) 這個(gè)點(diǎn)不能走或者在關(guān)閉列表中,跳過這個(gè)點(diǎn) 如果新花費(fèi)小于原來的花費(fèi), 替換成新的花費(fèi) 將這個(gè)點(diǎn)(周圍的點(diǎn)里面的)的父節(jié)點(diǎn)設(shè)置為當(dāng)前點(diǎn)(新點(diǎn)開的紅點(diǎn))這個(gè)點(diǎn)不再開啟列表中 這個(gè)點(diǎn)添加到開啟列表中  直接替換成新的花費(fèi)  將這個(gè)點(diǎn)(周圍的點(diǎn)里面的)的父節(jié)點(diǎn)設(shè)置為當(dāng)前點(diǎn)(新點(diǎn)開的紅點(diǎn))源碼[csharp] view plain copy PRint?using UnityEngine;  using System.Collections;    public class Node  {      /*邏輯中用的*/      public int gCost;      public int hCost;      public int fCost      {          get { return gCost + hCost; }      }      public Node parent;        /*在Unity當(dāng)中用的*/      public bool canWalk;      //網(wǎng)格的下標(biāo)      public int gridX;      public int gridY;      //節(jié)點(diǎn)的位置      public Vector3 worldPos;        public Node(bool _canWalk, Vector3 position, int x, int y)      {          canWalk = _canWalk;          worldPos = position;          gridX = x;          gridY = y;      }  }  
using UnityEngine;using System.Collections;public class Node{    /*邏輯中用的*/    public int gCost;    public int hCost;    public int fCost    {        get { return gCost + hCost; }    }    public Node parent;    /*在Unity當(dāng)中用的*/    public bool canWalk;    //網(wǎng)格的下標(biāo)    public int gridX;    public int gridY;    //節(jié)點(diǎn)的位置    public Vector3 worldPos;    public Node(bool _canWalk, Vector3 position, int x, int y)    {        canWalk = _canWalk;        worldPos = position;        gridX = x;        gridY = y;    }}[csharp] view plain copy print?using UnityEngine;  using System.Collections;  using System.Collections.Generic;    public class Grid : MonoBehaviour  {      //存放點(diǎn)節(jié)點(diǎn)的數(shù)組      public Node[,] grid;        //網(wǎng)格的大小      public Vector2 gridSize;      //節(jié)點(diǎn)的大小      public float nodeRadius;      public float nodeDiameter;      //一個(gè)層,代表可不可以通過      public LayerMask cantLayer;        //x和y方向上各有多少個(gè)格子      public int gridContX;      public int gridContY;        //起點(diǎn)      public Transform start;        //用來保存路徑的列表      public List<Node> path = new List<Node>();          void Start ()      {          cantLayer = LayerMask.GetMask(”CantWalk”);          nodeDiameter = nodeRadius * 2;          //gridContX = (int)(gridSize.x / nodeDiameter);          gridContX = Mathf.RoundToInt(gridSize.x / nodeDiameter);          gridContY = Mathf.RoundToInt(gridSize.y / nodeDiameter);            grid = new Node[gridContX, gridContY];          CreatGrid();      }            void Update ()      {            }      //創(chuàng)建格子      void CreatGrid()      {          //網(wǎng)格起點(diǎn)          Vector3 startPoint = transform.position - gridSize.y / 2 * Vector3.forward - gridSize.x / 2 * Vector3.right;            for (int i = 0; i < gridContX; i++)          {              for (int j = 0; j < gridContY; j++)              {                  Vector3 worldPos = startPoint + Vector3.right * (nodeDiameter * i + nodeRadius) + Vector3.forward * (nodeDiameter * j + nodeRadius);                  //檢測有沒有碰到不能走的層上的物體                  bool canwalk = !Physics.CheckSphere(worldPos, nodeRadius, cantLayer);                    grid[i, j] = new Node(canwalk, worldPos, i, j);              }          }      }        //Unity中的輔助類      void OnDrawGizmos()      {          if (grid == null)          {              return;          }          foreach (Node node in grid)          {              if (node.canWalk)              {                  Gizmos.color = Color.yellow;                  Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));              }              else              {                  Gizmos.color = Color.red;                  Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));              }          }            //畫出起點(diǎn)的位置          Node startNode = FindWithPosition(start.position);          if (startNode.canWalk)          {              Gizmos.color = Color.black;              Gizmos.DrawCube(startNode.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));          }              //畫路徑          if(path != null)          {              foreach (var node in path)              {                  Gizmos.color = Color.blue;                  Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));              }          }      }        //通過位置得到在哪一個(gè)格子      public Node FindWithPosition(Vector3 position)      {          //在x方向的占比          float percentX = (position.x + gridSize.x / 2) / gridSize.x;          float percentY = (position.z + gridSize.y / 2) / gridSize.y;            //算出在哪個(gè)格子          int x = Mathf.RoundToInt((gridContX - 1) * percentX);          int y = Mathf.RoundToInt((gridContY - 1) * percentY);            return grid[x, y];      }        //通過一個(gè)點(diǎn)尋找周圍的點(diǎn)      public List<Node> GetAroundNode(Node node)      {          List<Node> aroundNodes = new List<Node>();            for (int i = -1; i <= 1; i++)          {              for (int j = -1; j <= 1; j++)              {                  //傳進(jìn)來的點(diǎn)的下標(biāo)  跳過                  if(i == 0 && j == 0)                  {                      continue;                  }                    int tempX = node.gridX + i;                  int tempY = node.gridY + j;                    //判斷有沒有越界                  if (tempX >= 0 && tempX < gridContX && tempY >= 0 && tempY < gridContY)                  {                      aroundNodes.Add(grid[tempX, tempY]);                  }              }          }            return aroundNodes;      }  }  
using UnityEngine;using System.Collections;using System.Collections.Generic;public class Grid : MonoBehaviour{    //存放點(diǎn)節(jié)點(diǎn)的數(shù)組    public Node[,] grid;    //網(wǎng)格的大小    public Vector2 gridSize;    //節(jié)點(diǎn)的大小    public float nodeRadius;    public float nodeDiameter;    //一個(gè)層,代表可不可以通過    public LayerMask cantLayer;    //x和y方向上各有多少個(gè)格子    public int gridContX;    public int gridContY;    //起點(diǎn)    public Transform start;    //用來保存路徑的列表    public List<Node> path = new List<Node>();    void Start ()    {        cantLayer = LayerMask.GetMask("CantWalk");        nodeDiameter = nodeRadius * 2;        //gridContX = (int)(gridSize.x / nodeDiameter);        gridContX = Mathf.RoundToInt(gridSize.x / nodeDiameter);        gridContY = Mathf.RoundToInt(gridSize.y / nodeDiameter);        grid = new Node[gridContX, gridContY];        CreatGrid();    }    void Update ()    {    }    //創(chuàng)建格子    void CreatGrid()    {        //網(wǎng)格起點(diǎn)        Vector3 startPoint = transform.position - gridSize.y / 2 * Vector3.forward - gridSize.x / 2 * Vector3.right;        for (int i = 0; i < gridContX; i++)        {            for (int j = 0; j < gridContY; j++)            {                Vector3 worldPos = startPoint + Vector3.right * (nodeDiameter * i + nodeRadius) + Vector3.forward * (nodeDiameter * j + nodeRadius);                //檢測有沒有碰到不能走的層上的物體                bool canwalk = !Physics.CheckSphere(worldPos, nodeRadius, cantLayer);                grid[i, j] = new Node(canwalk, worldPos, i, j);            }        }    }    //Unity中的輔助類    void OnDrawGizmos()    {        if (grid == null)        {            return;        }        foreach (Node node in grid)        {            if (node.canWalk)            {                Gizmos.color = Color.yellow;                Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));            }            else            {                Gizmos.color = Color.red;                Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));            }        }        //畫出起點(diǎn)的位置        Node startNode = FindWithPosition(start.position);        if (startNode.canWalk)        {            Gizmos.color = Color.black;            Gizmos.DrawCube(startNode.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));        }        //畫路徑        if(path != null)        {            foreach (var node in path)            {                Gizmos.color = Color.blue;                Gizmos.DrawCube(node.worldPos, (nodeDiameter - 0.02f) * new Vector3(1, 0.2f, 1));            }        }    }    //通過位置得到在哪一個(gè)格子    public Node FindWithPosition(Vector3 position)    {        //在x方向的占比        float percentX = (position.x + gridSize.x / 2) / gridSize.x;        float percentY = (position.z + gridSize.y / 2) / gridSize.y;        //算出在哪個(gè)格子        int x = Mathf.RoundToInt((gridContX - 1) * percentX);        int y = Mathf.RoundToInt((gridContY - 1) * percentY);        return grid[x, y];    }    //通過一個(gè)點(diǎn)尋找周圍的點(diǎn)    public List<Node> GetAroundNode(Node node)    {        List<Node> aroundNodes = new List<Node>();        for (int i = -1; i <= 1; i++)        {            for (int j = -1; j <= 1; j++)            {                //傳進(jìn)來的點(diǎn)的下標(biāo)  跳過                if(i == 0 && j == 0)                {                    continue;                }                int tempX = node.gridX + i;                int tempY = node.gridY + j;                //判斷有沒有越界                if (tempX >= 0 && tempX < gridContX && tempY >= 0 && tempY < gridContY)                {                    aroundNodes.Add(grid[tempX, tempY]);                }            }        }        return aroundNodes;    }}[csharp] view plain copy print?using UnityEngine;  using System.Collections;  using System.Collections.Generic;    public class FindPath_AStar : MonoBehaviour  {      public Transform startPoint;      public Transform endPoint;          private Grid grid;      // Use this for initialization      void Start ()      {          grid = GetComponent<Grid>();        }            void Update ()      {          FindPath(startPoint.position, endPoint.position);        }        //      void FindPath(Vector3 startPos, Vector3 endPos)      {          //開啟列表          List<Node> opentSet = new List<Node>();          //關(guān)閉列表          List<Node> closeSet = new List<Node>();            //起點(diǎn)格子          Node startNode = grid.FindWithPosition(startPos);          //終點(diǎn)格子          Node endNode = grid.FindWithPosition(endPos);            //把起點(diǎn)加入開啟列表          opentSet.Add(startNode);            //開始循環(huán)(開啟列表有值)          while (opentSet.Count > 0)          {              //當(dāng)前點(diǎn)              Node currentNode = opentSet[0];              //開啟列表中最小的f_Cost              for (int i = 0; i < opentSet.Count; i++)              {                  //如果總花費(fèi)最小,并且離目標(biāo)點(diǎn)最近                  if (opentSet[i].fCost <= currentNode.fCost && opentSet[i].hCost < currentNode.fCost)                  {                      currentNode = opentSet[i];                  }              }                //把這個(gè)點(diǎn) 點(diǎn)紅              //把當(dāng)前點(diǎn)從開啟列表刪除              opentSet.Remove(currentNode);              //把當(dāng)前點(diǎn)添加到關(guān)閉列表              closeSet.Add(currentNode);                //If當(dāng)前點(diǎn)是終點(diǎn),跳出循環(huán)              if (currentNode == endNode)              {                  GetPath(startNode, endNode);                  return;              }                //周圍的點(diǎn)              List<Node> around = grid.GetAroundNode(currentNode);              //循環(huán)周圍的點(diǎn)              foreach (Node node in around)              {                  //這個(gè)點(diǎn)不能走或者在關(guān)閉列表中,跳過這個(gè)點(diǎn)                  if (!node.canWalk || closeSet.Contains(node))                  {                      continue;                  }                  //點(diǎn)開一個(gè)紅點(diǎn),周圍的點(diǎn)會(huì)得到一個(gè)新的花費(fèi)g                  int newCost_g = currentNode.gCost + GetCost(currentNode, node);                  //比較新花費(fèi)和原來的花費(fèi),誰更小(誰離我們起點(diǎn)近) || 這個(gè)點(diǎn)不再開啟列表中                  if (newCost_g < node.gCost || !opentSet.Contains(node))                  {                      //替換成新的花費(fèi)                      node.gCost = newCost_g;                      node.hCost = GetCost(node, endNode);                      //將這個(gè)點(diǎn)(周圍的點(diǎn)里面的)的父節(jié)點(diǎn)設(shè)置為當(dāng)前點(diǎn)(新點(diǎn)開的紅點(diǎn))                      node.parent = currentNode;                        //這個(gè)點(diǎn)不再開啟列表中                      if (!opentSet.Contains(node))                      {                          opentSet.Add(node);                      }                  }              }          }      }        //計(jì)算花費(fèi)      int GetCost(Node a, Node b)      {          //等到兩點(diǎn)之間的一個(gè)距離(x方向和y方向)          int coutX = Mathf.Abs(a.gridX - b.gridX);          int coutY = Mathf.Abs(a.gridY - b.gridY);            if(coutX > coutY)          {              return (coutX - coutY) * 10 + coutY * 14;          }          else          {              return (coutY - coutX) * 10 + coutX * 14;          }      }          //得到路徑      void GetPath(Node startNode, Node endNode)      {          List<Node> path = new List<Node>();          Node temp = endNode;          while(temp != startNode)          {              path.Add(temp);              temp = temp.parent;          }          //列表轉(zhuǎn)置          path.Reverse();          grid.path = path;      }  }  
using UnityEngine;using System.Collections;using System.Collections.Generic;public class FindPath_AStar : MonoBehaviour{    public Transform startPoint;    public Transform endPoint;    private Grid grid;    // Use this for initialization    void Start ()    {        grid = GetComponent<Grid>();    }    void Update ()    {        FindPath(startPoint.position, endPoint.position);    }    //    void FindPath(Vector3 startPos, Vector3 endPos)    {        //開啟列表        List<Node> opentSet = new List<Node>();        //關(guān)閉列表        List<Node> closeSet = new List<Node>();        //起點(diǎn)格子        Node startNode = grid.FindWithPosition(startPos);        //終點(diǎn)格子        Node endNode = grid.FindWithPosition(endPos);        //把起點(diǎn)加入開啟列表        opentSet.Add(startNode);        //開始循環(huán)(開啟列表有值)        while (opentSet.Count > 0)        {            //當(dāng)前點(diǎn)            Node currentNode = opentSet[0];            //開啟列表中最小的f_Cost            for (int i = 0; i < opentSet.Count; i++)            {                //如果總花費(fèi)最小,并且離目標(biāo)點(diǎn)最近                if (opentSet[i].fCost <= currentNode.fCost && opentSet[i].hCost < currentNode.fCost)                {                    currentNode = opentSet[i];                }            }            //把這個(gè)點(diǎn) 點(diǎn)紅            //把當(dāng)前點(diǎn)從開啟列表刪除            opentSet.Remove(currentNode);            //把當(dāng)前點(diǎn)添加到關(guān)閉列表            closeSet.Add(currentNode);            //If當(dāng)前點(diǎn)是終點(diǎn),跳出循環(huán)            if (currentNode == endNode)            {                GetPath(startNode, endNode);                return;            }            //周圍的點(diǎn)            List<Node> around = grid.GetAroundNode(currentNode);            //循環(huán)周圍的點(diǎn)            foreach (Node node in around)            {                //這個(gè)點(diǎn)不能走或者在關(guān)閉列表中,跳過這個(gè)點(diǎn)                if (!node.canWalk || closeSet.Contains(node))                {                    continue;                }                //點(diǎn)開一個(gè)紅點(diǎn),周圍的點(diǎn)會(huì)得到一個(gè)新的花費(fèi)g                int newCost_g = currentNode.gCost + GetCost(currentNode, node);                //比較新花費(fèi)和原來的花費(fèi),誰更小(誰離我們起點(diǎn)近) || 這個(gè)點(diǎn)不再開啟列表中                if (newCost_g < node.gCost || !opentSet.Contains(node))                {                    //替換成新的花費(fèi)                    node.gCost = newCost_g;                    node.hCost = GetCost(node, endNode);                    //將這個(gè)點(diǎn)(周圍的點(diǎn)里面的)的父節(jié)點(diǎn)設(shè)置為當(dāng)前點(diǎn)(新點(diǎn)開的紅點(diǎn))                    node.parent = currentNode;                    //這個(gè)點(diǎn)不再開啟列表中                    if (!opentSet.Contains(node))                    {                        opentSet.Add(node);                    }                }            }        }    }    //計(jì)算花費(fèi)    int GetCost(Node a, Node b)    {        //等到兩點(diǎn)之間的一個(gè)距離(x方向和y方向)        int coutX = Mathf.Abs(a.gridX - b.gridX);        int coutY = Mathf.Abs(a.gridY - b.gridY);        if(coutX > coutY)        {            return (coutX - coutY) * 10 + coutY * 14;        }        else        {            return (coutY - coutX) * 10 + coutX * 14;        }    }    //得到路徑    void GetPath(Node startNode, Node endNode)    {        List<Node> path = new List<Node>();        Node temp = endNode;        while(temp != startNode)        {            path.Add(temp);            temp = temp.parent;        }        //列表轉(zhuǎn)置        path.Reverse();        grid.path = path;    }}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 色综合天天射 | 浴室洗澡偷拍一区二区 | 欧美激情一区二区三级高清视频 | 久久在线视频 | 亚洲无吗天堂 | 成人午夜免费网站 | 成人在线播放 | 精品国产一区二区三区免费 | 亚洲网站免费观看 | 国产精品久久久久久久久久久杏吧 | 国产xxx护士爽免费看 | 国产精品乱码一区二区三区 | 成人午夜视频在线观看 | 午夜a级理论片915影院 | 精品久久久久久久久久久久包黑料 | 五月激情六月婷婷 | 国产精品国产自产拍高清 | 亚洲一区二区三区免费视频 | 成人五月网 | 国产精品一区二区在线 | 欧洲精品| www.久久久 | 欧美涩涩| 另类免费视频 | 国产成人精品亚洲777人妖 | 亚洲一区二区三区在线播放 | 日韩精品一区二区三区中文在线 | 久久久一二三 | 精品在线一区二区三区 | 亚洲精品在 | 成人国产精品免费网站 | 蜜桃视频在线观看www社区 | 亚洲电影一区二区三区 | 毛片在线看片 | 国产福利91精品一区二区三区 | 久久精品黄 | 久热久| 中文字幕亚洲自拍 | 国内精品一区二区 | 久草不卡视频 | 午夜精品一区二区三区在线播放 |