AStar寻路算法

简易地图


        如图所示简易地图, 其中绿色方块的是起点 (用 A 表示), 中间蓝色的是障碍物, 红色的方块 (用
B 表示) 是目的地. 为了可以用一个二维数组来表示地图, 我们将地图划分成一个个的小方块.
        二维数组在游戏中的应用是很多的, 比如贪吃蛇和俄罗斯方块基本原理就是移动方块而已. 而大型
游戏的地图, 则是将各种"地貌"铺在这样的小方块上.


寻路步骤


        1. 从起点 A 开始, 把它作为待处理的方格存入一个"开启列表", 开启列表就是一个等待检查方格的列表.即存的是待检测的点
        2. 寻找起点 A 周围可以到达的方格, 将它们放入"开启列表", 并设置它们的"父方格"为 A.
        3. 从"开启列表"中删除起点 A, 并将起点 A 加入"关闭列表", "关闭列表"中存放的都是不需要再
次检查的方格.即存的是已检测的点


        图中浅绿色描边的方块表示已经加入 "开启列表" 等待检查. 淡蓝色描边的起点 A 表示已经放入"关闭列表" , 它不需要再执行检查.
        从 "开启列表" 中找出相对最靠谱的方块, 什么是最靠谱? 它们通过公式 F=G+H 来计算.
        F = G + H
        G 表示从父节点移动到网格上指定方格的移动耗费 (可沿斜方向移动)。初始父节点为A
        H 表示从指定的方格移动到终点 B 的预计耗费 (H 有很多计算方法, 这里我们设定只可以
上下左右移动)。


我们假设横向移动一个格子的耗费为 10, 为了便于计算, 沿斜方向移动一个格子耗费是 14. 为了
更直观的展示如何运算 FGH, 图中方块的左上角数字表示 F, 左下角表示 G, 右下角表示 H. 看看是否跟你心里想的结果一样?
        从 "开启列表" 中选择 F 值最低的方格 C (绿色起始方块 A 右边的方块), 然后对它进行如下处
理:
        4. 把它从 "开启列表" 中删除, 并放到 "关闭列表" 中。
        5. 检查它所有相邻并且可以到达 (障碍物和 "关闭列表" 的方格都不考虑) 的方格. 如果这些方格还不在 "开启列表" 里的话, 将它们加入 "开启列表", 计算这些方格的 G, H 和 F 值各是多少, 并设置它们的 "父方格" 为 C。G值是父节点到自己的权值,而不是起点到自己的权值,这点要注意。
        6. 如果某个相邻方格 D 已经在 "开启列表" 里了, 检查如果用新的路径 (就是经过 C 的路径) 到
达它的话, G 值是否会更低一些, 如果新的 G 值更低, 那就把它的 "父方格" 改为目前选中的方格 C, 然后重新计算它的 F 值和 G 值 (H 值不需要重新计算, 因为对于每个方块, H 值是不变的). 如果新的 G值比较高, 就说明经过 C 再到达 D 不是一个明智的选择, 因为它需要更远的路, 这时我们什么也不做。
        如图, 我们选中了 C 因为它的 F 值最小, 我们把它从 "开启列表" 中删除, 并把它加入 "关闭列
表". 它右边上下三个都是墙, 所以不考虑它们. 它左边是起始方块, 已经加入到 "关闭列表" 了, 也不考虑. 所以它周围的候选方块就只剩下 4 个. 让我们来看看 C 下面的那个格子, 它目前的 G 是 14, 如果通过 C 到达它的话, G 将会是 10 + 10, 这比 14 要大, 因此我们什么也不做。
        然后我们继续从 "开启列表" 中找出 F 值最小的, 但我们发现 C 上面的和下面的同时为 54, 这
时怎么办呢? 这时随便取哪一个都行, 比如我们选择了 C 下面的那个方块 D。


        D 右边已经右上方的都是墙, 所以不考虑, 但为什么右下角的没有被加进 "开启列表" 呢? 因为如果 C 下面的那块也不可以走, 想要到达 C 右下角的方块就需要从 "方块的角" 走了, 在程序中设置是否允许这样走。(图中的示例不允许这样走)


就这样, 我们从 "开启列表" 找出 F 值最小的, 将它从 "开启列表" 中移掉, 添加到 "关闭列表".
再继续找出它周围可以到达的方块, 如此循环下去...
        那么什么时候停止呢? —— 当我们发现 "开始列表" 里出现了目标终点方块的时候, 说明路径已经被找到。


如何找回路径


        如上图所示, 除了起始方块, 每一个曾经或者现在还在 "开启列表" 里的方块, 它都有一个 "父方
块", 通过 "父方块" 可以索引到最初的 "起始方块", 这就是路径。


将整个过程抽象


把起始格添加到 "开启列表"
do
{
        寻找开启列表中 F 值最低的格子, 我们称它为当前格.
        把它切换到关闭列表.
        对当前格相邻的 8 格中的每一个
        if (它不可通过 || 已经在 "关闭列表" 中)
        {
                什么也不做.
        }
        if (它不在开启列表中)
        {
                把它添加进 "开启列表", 把当前格作为这一格的父节点, 计算这一格的 FGH
        if (它已经在开启列表中)
        {
        if (用 G 值为参考检查新的路径是否更好, 更低的 G 值意味着更好的路径)
        {
                把这一格的父节点改成当前格, 并且重新计算这一格的 GF 值.
        }
} while( 目标格已经在 "开启列表", 这时候路径被找到)
如果开启列表已经空了, 说明路径不存在.
最后从目标格开始, 沿着每一格的父节点移动直到回到起始格, 这就是路径.


主要代码


//地图以左下角为(0,0)位置,朝右上角增加行Row和列Col
public class Point
{ 
    public Point Parent { get; set; }
    public int Row { get; set; }//位于哪一行
    public int Col { get; set; }//位于哪一列
    public float F { get; set; }
    public float G { get; set; }
    public float H { get; set; }
    public bool IsWall { get; set; }//是否是障碍物

    public Point(int row,int col, Point parent = null)
    {
        this.Row = row;
        this.Col = col;
        this.Parent = parent;
        this.IsWall = false;
    }

    //更新父节点和G、F的值
    public void UpdateParent(Point parent,float g)
    {
        this.Parent = parent;
        this.G = g;
        this.F = this.G + this.H;
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AStar : MonoBehaviour
{
    public const int m_MaxRow = 12;
    public const int m_MaxCol = 15;

    private Point[,] m_Map = new Point[m_MaxRow, m_MaxCol];//地图 以左下角为[0,0]位置
    private Point m_StartPoint;//开始点
    private Point m_EndPoint;//结束点


    void Awake()
    {
        InitMap();
        m_StartPoint = m_Map[11, 1];
        CreateCube(1, 1, 0, Color.green);
        m_EndPoint = m_Map[0, 13];
        CreateCube(13, 0, 0, Color.red);

        //设置背景方块
        for (int i = 0; i < m_MaxRow; i++)
        {
            for (int j = 0; j < m_MaxCol; j++)
            {
                CreateCube(j, i, 10, new Color(0.1f, 1, 1));
            }
        }
        
        FindPath(m_StartPoint, m_EndPoint);
        List<Point> path = ShowPath();
        foreach (Point point in path)
        {
            //Debug.Log(point.Row + "," + point.Col);
            CreateCube(point.Col, point.Row, 5, new Color(1, 0, 1));
        }
        

        //测试获取某个点周围的点集合是否正确
        //List<Point> surroundPoints = GetSurroundPoints(m_Map[4, 4]);
        //for (int i = 0; i < surroundPoints.Count; i++)
        //{
        //    Debug.Log(surroundPoints[i].Row + "," + surroundPoints[i].Col);
        //    CreateCube(surroundPoints[i].Col, surroundPoints[i].Row, 0, new Color(1, 0.6f, 0));
        //}
    }

    private void InitMap()
    {
        for (int i = 0; i < m_MaxRow; i++)
        {
            for (int j = 0; j < m_MaxCol; j++)
            {
                m_Map[i, j] = new Point(i, j);
            }
        }
        //设置第一堵墙
        m_Map[3, 5].IsWall = true;
        m_Map[4, 5].IsWall = true;
        m_Map[5, 5].IsWall = true;
        m_Map[6, 5].IsWall = true;
        m_Map[7, 5].IsWall = true;
        m_Map[8, 5].IsWall = true;
        m_Map[9, 5].IsWall = true;
        CreateCube(5, 3, 0, Color.blue);
        CreateCube(5, 4, 0, Color.blue);
        CreateCube(5, 5, 0, Color.blue);
        CreateCube(5, 6, 0, Color.blue);
        CreateCube(5, 7, 0, Color.blue);
        CreateCube(5, 8, 0, Color.blue);
        CreateCube(5, 9, 0, Color.blue);

        //设置第二堵墙
        m_Map[0, 9].IsWall = true;
        m_Map[1, 9].IsWall = true;
        m_Map[2, 9].IsWall = true;
        m_Map[3, 9].IsWall = true;
        m_Map[4, 9].IsWall = true;
        m_Map[5, 9].IsWall = true;
        m_Map[6, 9].IsWall = true;
        CreateCube(9, 0, 0, Color.blue);
        CreateCube(9, 1, 0, Color.blue);
        CreateCube(9, 2, 0, Color.blue);
        CreateCube(9, 3, 0, Color.blue);
        CreateCube(9, 4, 0, Color.blue);
        CreateCube(9, 5, 0, Color.blue);
        CreateCube(9, 6, 0, Color.blue);
    }

    /// <summary>
    /// 创建方块
    /// </summary>
    /// <param name="x">x坐标位置,对应地图的Col</param>
    /// <param name="y">y坐标位置,对应地图的Row</param>
    /// <param name="color"></param>
    private void CreateCube(int x, int y, int z, Color color)
    {
        GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
        obj.transform.position = new Vector3(x, y, z);
        obj.GetComponent<Renderer>().material.color = color;
    }

    private List<Point> ShowPath()
    {
        List<Point> path = new List<Point>();
        Point temp = m_EndPoint;
        while (temp.Parent != null)
        {
            path.Add(temp);
            temp = temp.Parent;
        }

        return path;
    }

    //查找路径
    private void FindPath(Point start, Point end)
    {
        List<Point> openList = new List<Point>();//开启列表
        List<Point> closeList = new List<Point>();//关闭列表

        openList.Add(start);
        while (openList.Count > 0)
        {
            Point point = FindMinFPoint(openList);//查找开启列表中F值最小的点
            openList.Remove(point);
            closeList.Add(point);
            List<Point> surroundPoints = GetSurroundPoints(point);
            PointsFilter(surroundPoints, closeList);
            foreach (Point surroundPoint in surroundPoints)
            {
                if (openList.Contains(surroundPoint))//在开启列表中
                {
                    float nowG = CalcG(surroundPoint, point);
                    if (nowG < surroundPoint.G)
                        surroundPoint.UpdateParent(point, nowG);
                }
                else 
                {
                    surroundPoint.Parent = point;
                    CalcF(surroundPoint, end);
                    openList.Add(surroundPoint);
                }
            }
            //查找路径结束
            if (openList.Contains(end))
                break;
        }
    }

    //计算某个点的G
    private float CalcG(Point point, Point parent)
    {
        return Vector2.Distance(new Vector2(point.Row, point.Col), new Vector2(parent.Row, parent.Col)) + parent.G;
    }

    private void CalcF(Point point, Point end)
    {
        //F = G + H
        float h = Mathf.Abs(point.Row - end.Row) + Mathf.Abs(point.Col - end.Col);
        float g = 0;
        if (point.Parent == null)//起始点
            g = 0;
        else{
            g = Vector2.Distance(new Vector2(point.Row, point.Col), new Vector2(point.Parent.Row, point.Parent.Col)) + point.Parent.G;
        }
        float f = g + h;
        point.F = f;
        point.G = g;
        point.H = h;
    }

    //过滤在关闭列表中的点
    private void PointsFilter(List<Point> src, List<Point> closeList)
    {
        foreach (Point point in closeList)
        {
            if (src.Contains(point))
            {
                src.Remove(point);
            }
        }
    }

    //获取当前点周围所有的可行走点
    private List<Point> GetSurroundPoints(Point cur)
    {
        List<Point> points = new List<Point>();
        Point up = null, down = null, left = null, right = null;//上、下、左、右   4个点
        Point lu = null, ld = null, ru = null, rd = null;//左上、左下、右上、右下  4个点

        if (cur.Row + 1 <= m_MaxRow - 1)
        {
            up = m_Map[cur.Row + 1, cur.Col];
        }
        if (cur.Row - 1 >= 0)
        {
            down = m_Map[cur.Row - 1, cur.Col];
        }
        if (cur.Col - 1 >= 0)
        {
            left = m_Map[cur.Row, cur.Col - 1];
        }
        if (cur.Col + 1 <= m_MaxCol - 1)
        {
            right = m_Map[cur.Row, cur.Col + 1];
        }
        if (left != null && up != null)
        {
            lu = m_Map[cur.Row + 1, cur.Col - 1];
        }
        if (left != null && down != null)
        {
            ld = m_Map[cur.Row - 1, cur.Col - 1];
        }
        if (right != null && up != null)
        {
            ru = m_Map[cur.Row + 1, cur.Col + 1];
        }
        if (right != null && down != null)
        {
            rd = m_Map[cur.Row - 1, cur.Col + 1];
        }

        if (up != null && up.IsWall == false)
        {
            points.Add(up);
        }
        if (down != null && down.IsWall == false)
        {
            points.Add(down);
        }
        if (left != null && left.IsWall == false)
        {
            points.Add(left);
        }
        if (right != null && right.IsWall == false)
        {
            points.Add(right);
        }
        if (lu != null && lu.IsWall == false && left.IsWall == false && up.IsWall == false)//左上
        {
            points.Add(lu);
        }
        if (ld != null && ld.IsWall == false && left.IsWall == false && down.IsWall == false)//左下
        {
            points.Add(ld);
        }
        if (ru != null && ru.IsWall == false && right.IsWall == false && up.IsWall == false)//右上
        {
            points.Add(ru);
        }
        if (rd != null && rd.IsWall == false && right.IsWall == false && down.IsWall == false)//右下
        {
            points.Add(rd);
        }
        return points;
    }

    //查找开启列表中F值最小的点
    private Point FindMinFPoint(List<Point> openList)
    {
        float minF = float.MaxValue;
        Point minPoint = null;
        foreach (Point p in openList)
        {
            if (p.F < minF)
            {
                minF = p.F;
                minPoint = p;
            }
        }
        return minPoint;
    }
}

测试结果:

说明:地图为12行15列,1为起点(绿色),2为终点(红色),3为障碍物(蓝色),4为路径点(紫色)

1、起点[5,3],终点[6,9]

2、起点[7,2],终点[4,12]

3、起点[8,1],终点[0,13],在InitMap中加上另外两堵墙

 

4、起点[10,1],终点[0,13],跟3相比起点往上移了两格。

  • 15
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值