亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

A*算法,偽代碼,源碼

2019-11-11 05:46:40
字體:
來源:轉載
供稿:網友
轉載自博主 GottaYiWanLiu[點擊查看原文](http://blog.csdn.net/gottayiwanliu/article/details/54881256)A*   AStar   A星2d游戲,或者網格游戲中Cost f 總消耗Cost g 距離起點的消耗Cost h 距離終點的消耗默認消耗,直走消耗10,斜著走消耗14開啟列表關閉列表父節點//開啟列表關閉列表開始循環(開啟列表有值) 當前點 = 開啟列表中最小的f_Cost  把當前點從開啟列表刪除 把當前點添加到關閉列表  If當前點是終點,跳出循環 點開一個紅點,周圍的點會得到一個新的花費循環周圍的點 這個點不能走或者在關閉列表中,跳過這個點 如果新花費小于原來的花費, 替換成新的花費 將這個點(周圍的點里面的)的父節點設置為當前點(新點開的紅點)這個點不再開啟列表中 這個點添加到開啟列表中  直接替換成新的花費  將這個點(周圍的點里面的)的父節點設置為當前點(新點開的紅點)源碼[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當中用的*/      public bool canWalk;      //網格的下標      public int gridX;      public int gridY;      //節點的位置      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當中用的*/    public bool canWalk;    //網格的下標    public int gridX;    public int gridY;    //節點的位置    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  {      //存放點節點的數組      public Node[,] grid;        //網格的大小      public Vector2 gridSize;      //節點的大小      public float nodeRadius;      public float nodeDiameter;      //一個層,代表可不可以通過      public LayerMask cantLayer;        //x和y方向上各有多少個格子      public int gridContX;      public int gridContY;        //起點      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 ()      {            }      //創建格子      void CreatGrid()      {          //網格起點          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));              }          }            //畫出起點的位置          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));              }          }      }        //通過位置得到在哪一個格子      public Node FindWithPosition(Vector3 position)      {          //在x方向的占比          float percentX = (position.x + gridSize.x / 2) / gridSize.x;          float percentY = (position.z + gridSize.y / 2) / gridSize.y;            //算出在哪個格子          int x = Mathf.RoundToInt((gridContX - 1) * percentX);          int y = Mathf.RoundToInt((gridContY - 1) * percentY);            return grid[x, y];      }        //通過一個點尋找周圍的點      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++)              {                  //傳進來的點的下標  跳過                  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{    //存放點節點的數組    public Node[,] grid;    //網格的大小    public Vector2 gridSize;    //節點的大小    public float nodeRadius;    public float nodeDiameter;    //一個層,代表可不可以通過    public LayerMask cantLayer;    //x和y方向上各有多少個格子    public int gridContX;    public int gridContY;    //起點    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 ()    {    }    //創建格子    void CreatGrid()    {        //網格起點        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));            }        }        //畫出起點的位置        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));            }        }    }    //通過位置得到在哪一個格子    public Node FindWithPosition(Vector3 position)    {        //在x方向的占比        float percentX = (position.x + gridSize.x / 2) / gridSize.x;        float percentY = (position.z + gridSize.y / 2) / gridSize.y;        //算出在哪個格子        int x = Mathf.RoundToInt((gridContX - 1) * percentX);        int y = Mathf.RoundToInt((gridContY - 1) * percentY);        return grid[x, y];    }    //通過一個點尋找周圍的點    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++)            {                //傳進來的點的下標  跳過                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>();          //關閉列表          List<Node> closeSet = new List<Node>();            //起點格子          Node startNode = grid.FindWithPosition(startPos);          //終點格子          Node endNode = grid.FindWithPosition(endPos);            //把起點加入開啟列表          opentSet.Add(startNode);            //開始循環(開啟列表有值)          while (opentSet.Count > 0)          {              //當前點              Node currentNode = opentSet[0];              //開啟列表中最小的f_Cost              for (int i = 0; i < opentSet.Count; i++)              {                  //如果總花費最小,并且離目標點最近                  if (opentSet[i].fCost <= currentNode.fCost && opentSet[i].hCost < currentNode.fCost)                  {                      currentNode = opentSet[i];                  }              }                //把這個點 點紅              //把當前點從開啟列表刪除              opentSet.Remove(currentNode);              //把當前點添加到關閉列表              closeSet.Add(currentNode);                //If當前點是終點,跳出循環              if (currentNode == endNode)              {                  GetPath(startNode, endNode);                  return;              }                //周圍的點              List<Node> around = grid.GetAroundNode(currentNode);              //循環周圍的點              foreach (Node node in around)              {                  //這個點不能走或者在關閉列表中,跳過這個點                  if (!node.canWalk || closeSet.Contains(node))                  {                      continue;                  }                  //點開一個紅點,周圍的點會得到一個新的花費g                  int newCost_g = currentNode.gCost + GetCost(currentNode, node);                  //比較新花費和原來的花費,誰更小(誰離我們起點近) || 這個點不再開啟列表中                  if (newCost_g < node.gCost || !opentSet.Contains(node))                  {                      //替換成新的花費                      node.gCost = newCost_g;                      node.hCost = GetCost(node, endNode);                      //將這個點(周圍的點里面的)的父節點設置為當前點(新點開的紅點)                      node.parent = currentNode;                        //這個點不再開啟列表中                      if (!opentSet.Contains(node))                      {                          opentSet.Add(node);                      }                  }              }          }      }        //計算花費      int GetCost(Node a, Node b)      {          //等到兩點之間的一個距離(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;          }          //列表轉置          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>();        //關閉列表        List<Node> closeSet = new List<Node>();        //起點格子        Node startNode = grid.FindWithPosition(startPos);        //終點格子        Node endNode = grid.FindWithPosition(endPos);        //把起點加入開啟列表        opentSet.Add(startNode);        //開始循環(開啟列表有值)        while (opentSet.Count > 0)        {            //當前點            Node currentNode = opentSet[0];            //開啟列表中最小的f_Cost            for (int i = 0; i < opentSet.Count; i++)            {                //如果總花費最小,并且離目標點最近                if (opentSet[i].fCost <= currentNode.fCost && opentSet[i].hCost < currentNode.fCost)                {                    currentNode = opentSet[i];                }            }            //把這個點 點紅            //把當前點從開啟列表刪除            opentSet.Remove(currentNode);            //把當前點添加到關閉列表            closeSet.Add(currentNode);            //If當前點是終點,跳出循環            if (currentNode == endNode)            {                GetPath(startNode, endNode);                return;            }            //周圍的點            List<Node> around = grid.GetAroundNode(currentNode);            //循環周圍的點            foreach (Node node in around)            {                //這個點不能走或者在關閉列表中,跳過這個點                if (!node.canWalk || closeSet.Contains(node))                {                    continue;                }                //點開一個紅點,周圍的點會得到一個新的花費g                int newCost_g = currentNode.gCost + GetCost(currentNode, node);                //比較新花費和原來的花費,誰更小(誰離我們起點近) || 這個點不再開啟列表中                if (newCost_g < node.gCost || !opentSet.Contains(node))                {                    //替換成新的花費                    node.gCost = newCost_g;                    node.hCost = GetCost(node, endNode);                    //將這個點(周圍的點里面的)的父節點設置為當前點(新點開的紅點)                    node.parent = currentNode;                    //這個點不再開啟列表中                    if (!opentSet.Contains(node))                    {                        opentSet.Add(node);                    }                }            }        }    }    //計算花費    int GetCost(Node a, Node b)    {        //等到兩點之間的一個距離(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;        }        //列表轉置        path.Reverse();        grid.path = path;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
97在线精品视频| 欧美午夜精品久久久久久人妖| 欧美黑人国产人伦爽爽爽| 久久国产精品电影| 精品在线小视频| 78m国产成人精品视频| 久久久免费观看| 在线精品播放av| 日韩一区二区在线视频| 亚洲a∨日韩av高清在线观看| 国产精品久久久91| 国外视频精品毛片| 欧美大成色www永久网站婷| 国产婷婷色综合av蜜臀av| 欧美成人免费网| 日韩av在线直播| 欧美精品久久久久久久| 亚洲精品日韩在线| 国产欧美日韩91| 欧美午夜片欧美片在线观看| 91国产精品电影| 国产日韩视频在线观看| 成人黄色免费看| 亚洲美女又黄又爽在线观看| 日本一本a高清免费不卡| 日韩有码在线电影| 亚洲国产一区二区三区四区| 日本一区二区在线免费播放| 亚洲区中文字幕| 亚洲欧美视频在线| 91亚洲人电影| 国产乱人伦真实精品视频| 日韩av网站大全| 亚洲美腿欧美激情另类| 亚洲一区二区少妇| 欧美激情欧美狂野欧美精品| 日韩美女在线观看| 亚洲免费电影一区| 国产精品三级美女白浆呻吟| 欧美大码xxxx| 国产原创欧美精品| 亚洲电影第1页| 一本大道久久加勒比香蕉| 日韩精品免费在线视频观看| 欧美丰满少妇xxxx| 欧美性开放视频| 久久久久久久国产精品| 亚洲欧美日韩一区二区三区在线| 日本精品久久电影| 成人亚洲综合色就1024| 一本色道久久综合狠狠躁篇的优点| 国产免费一区二区三区在线能观看| 亚洲国产精品美女| 91高潮精品免费porn| 亚洲国产精品99久久| 欧美日韩高清在线观看| 久久精品电影一区二区| 久久国产精品影片| 久久久久99精品久久久久| 亚洲成年人影院在线| 国产美女91呻吟求| 国产原创欧美精品| 亚洲加勒比久久88色综合| 日韩精品免费综合视频在线播放| 成人h视频在线| 美女性感视频久久久| 国产成+人+综合+亚洲欧美丁香花| 成人国产亚洲精品a区天堂华泰| 欧美日韩亚洲成人| 亚洲成在人线av| 精品调教chinesegay| 日韩精品视频在线观看免费| 91免费视频网站| 激情成人中文字幕| 亚洲a级在线观看| 精品视频在线导航| 日本一欧美一欧美一亚洲视频| 国产精品美乳在线观看| 国模精品一区二区三区色天香| 91精品久久久久久久久久入口| 日韩在线视频播放| 欧美精品videos性欧美| 日韩av免费一区| 色视频www在线播放国产成人| 日本一区二三区好的精华液| 久久久欧美一区二区| 日韩精品在线播放| 国产精品第一第二| 久久精品国产精品| 久久777国产线看观看精品| 亚洲精品一区二区三区婷婷月| 日韩在线观看精品| 欧美猛交ⅹxxx乱大交视频| 精品美女永久免费视频| 国产综合视频在线观看| 一区二区在线免费视频| 一区二区欧美激情| 国产91精品久久久久久| 日韩免费看的电影电视剧大全| 91青草视频久久| 欧美在线一级va免费观看| 欧美日韩国产黄| 68精品国产免费久久久久久婷婷| 91精品国产色综合| 日韩精品免费一线在线观看| 国产一区二区黑人欧美xxxx| 亚洲免费一级电影| 欧美电影免费观看网站| 亚洲曰本av电影| 亚洲性视频网址| 亚洲电影免费观看高清| 日本不卡高字幕在线2019| 国产在线拍揄自揄视频不卡99| 欧美日韩成人免费| 亚洲国产又黄又爽女人高潮的| 亚洲精品一区久久久久久| 亚洲欧美精品一区| 亚洲一区二区中文字幕| 亚洲精品国产suv| 97久久国产精品| 日韩电影视频免费| 日韩人体视频一二区| 亚洲精品中文字幕av| 国产精品香蕉国产| 久久综合亚洲社区| 久久精品免费播放| 91精品久久久久| 国产精品一区久久| 精品久久香蕉国产线看观看gif| 成人久久一区二区三区| 日本久久久久久久| 成人字幕网zmw| 国产精品成人久久久久| 国产亚洲精品一区二555| 色婷婷综合久久久久中文字幕1| 久久亚洲国产精品成人av秋霞| 国产精品视频中文字幕91| 色综合男人天堂| 久久精品国产亚洲7777| 欧美日韩加勒比精品一区| 久久九九有精品国产23| 亚洲精品成人网| 亚洲自拍高清视频网站| 日本伊人精品一区二区三区介绍| 国内精品久久久久影院 日本资源| 亚洲乱码av中文一区二区| 久久天天躁狠狠躁老女人| 国产精品一区二区av影院萌芽| 日韩激情av在线播放| 97视频人免费观看| 欧美视频二区36p| 国产精品视频色| 国产v综合ⅴ日韩v欧美大片| 亚洲一区二区免费在线| 亚洲一区二区在线播放| 欧美日韩在线第一页| 国产精品久久一区| 精品偷拍一区二区三区在线看| 亚洲第一福利网站| 岛国视频午夜一区免费在线观看| 久久99亚洲精品| 国产精品福利网站| 欧美最猛性xxxxx(亚洲精品)| 欧美激情奇米色|