Unity-A-Star寻路算法
最短路径
将地图存成二维数组,通过行列查找;
每一步都查找周围四个方向,或者八方向的所有点,放入开表;
- 是否边缘
- 是否障碍
- 是否已经在确定的路线中
计算每个方向上路径消耗,一般斜着走消耗小,收益大;
开表排序,筛选出最小消耗的点放入闭表,并将该点设置为新的起点,从开表中去除该点;
重复上面操作,直到起点=终点退出;
中间查找八个方向可移动点位时,需要将这些可移动点的父节点设置为起点,这样可通过最后筛选出的点,一层层找出最短路径;
x步内可移动所有点位
将所有方向上可以移动的节点放入开表,同时放入闭表;
遍历闭表,将每个节点周围可移动的节点放入开表,同时放入新表;
清空闭表,新表赋值给闭表(实际代码不这么写);
重复上面操作,可移动几步,就循环几次;
最终返回开表;
代码实现:
public enum ObsType
{
None,
Wall,
Water,
Cliff
}
public class AStarNode
{
public ObsType type;
public int x;
public int y;
public float g;
public float h;
public float f;
public AStarNode father;
public AStarNode(int _x,int _y,ObsType _type)
{
type = _type;
x = _x;
y = _y;
}
}
[Serializable]
public class GridPos
{
public GridPos(int col, int row)
{
Col = col;
Row = row;
}
public int Col;
public int Row;
}
public class AStarMgr
{
//地图的宽高
private int mapRow;
private int mapCol;
//地图相关的所有的格子容器
public AStarNode[,] nodes;
//开启列表
private List<AStarNode> openList = new List<AStarNode>();
//关闭列表
private List<AStarNode> closeList = new List<AStarNode>();
//初始化地图信息
public void InItMapInfo(int col, int row)
{
this.mapCol = col;
this.mapRow = row;
nodes = new AStarNode[col, row];
for (int i = 0; i < col; i++)
{
for (int j = 0; j < row; j++)
{
AStarNode node = new AStarNode(i, j,
Random.Range(0, 100) < 20 ? ObsType.Wall : Ob
nodes[i, j] = node;
}
}
}
//寻路的方法
public List<AStarNode> FindPath(GridPos startPos, GridPos end
{
//判断起始点是不是在地图的范围内
if (startPos.Col < 0 || startPos.Col >= mapCol
|| startPos.Row < 0 || startPos.Row
|| endPos.Col < 0 || endPos.Col >= m
|| endPos.Row < 0 || endPos.Row >= m
)
return null;
//判断起始点是不是不能通行的点
AStarNode start = nodes[startPos.Col, startPos.Row];
AStarNode end = nodes[endPos.Col, endPos.Row];
if (start.type != ObsType.None || end.type != ObsType.Non
return null;
closeList.Clear();
openList.Clear();
//开始点放入关闭列表中
start.father = null;
start.f = 0;
start.g = 0;
start.h = 0;
closeList.Add(start);
while (true)
{
//周围的点
//FindNearlyToOpenList(start.x - 1, start.y - 1, 1.4f
FindNearlyToOpenList(start.x, start.y - 1, 1.4f, star
//FindNearlyToOpenList(start.x + 1, start.y - 1, 1.4f
FindNearlyToOpenList(start.x - 1, start.y, 1.4f, star
FindNearlyToOpenList(start.x + 1, start.y, 1.4f, star
//FindNearlyToOpenList(start.x - 1, start.y + 1, 1.4f
FindNearlyToOpenList(start.x, start.y + 1, 1.4f, star
//FindNearlyToOpenList(start.x + 1, start.y + 1, 1.4f
if (openList.Count == 0)
return null;
//排序选出最小的点
openList.Sort(SortOpenList);
//放入关闭列表,然后从开启列表中移除
closeList.Add(openList[0]);
//找到这个点,进行下一次寻路
start = openList[0];
openList.RemoveAt(0);
if (start == end)
{
//结束
List<AStarNode> path = new List<AStarNode>();
path.Add(end);
while (end.father != null)
{
path.Add(end.father);
end = end.father;
}
path.Reverse();
return path;
}
}
}
//找出周围几步之内可到达的格子
public List<AStarNode> FindRange(GridPos startPos, int step)
{
if (startPos.Col < 0 || startPos.Col >= mapCol
|| startPos.Row < 0 || startPos.Row
return null;
AStarNode start = nodes[startPos.Col, startPos.Row];
if (start.type != ObsType.None)
return null;
closeList.Clear();
openList.Clear();
closeList.Add(start);
while (step-- > 0)
{
AddNearOneStep(closeList.ToArray());
}
return openList;
}
//排序函数
private int SortOpenList(AStarNode a, AStarNode b)
{
if (a.f > b.f)
return 1;
else if (a.f == b.f)
return 1;
else
return -1;
}
//临近的点放入开启列表
private void FindNearlyToOpenList(int x, int y, float g, ASta
{
if (x < 0 || x >= mapCol || y < 0 || y >= mapRow)
return;
AStarNode node = nodes[x, y];
if (node == null || node.type != ObsType.None
|| closeList.Contains(node)
|| openList.Contains(node)
)
return;
//计算f值 f=g+h;
node.father = father;
node.g = father.g + g;
node.h = Mathf.Abs(end.x - node.x) + Mathf.Abs(end.y - no
node.f = node.g + node.h;
openList.Add(node);
}
//当前步数的所有Node放入open
private void AddNearOneStep(AStarNode[] curStepNodes)
{
closeList.Clear();
foreach (var it in curStepNodes)
{
AddNearNode(it.x - 1, it.y);
AddNearNode(it.x, it.y - 1);
AddNearNode(it.x + 1, it.y);
AddNearNode(it.x, it.y + 1);
}
}
//单个格子周围的点加入open
private void AddNearNode(int x, int y)
{
if (x < 0 || x >= mapCol || y < 0 || y >= mapRow)
return;
AStarNode node = nodes[x, y];
if (node == null || node.type != ObsType.None)
return;
openList.Add(node);
closeList.Add(node);
}
}
Unity-A-Star寻路算法的更多相关文章
- Unity实现A*寻路算法学习1.0
一.A*寻路算法的原理 如果现在地图上存在两点A.B,这里设A为起点,B为目标点(终点) 这里为每一个地图节点定义了三个值 gCost:距离起点的Cost(距离) hCost:距离目标点的Cost(距 ...
- Unity实现A*寻路算法学习2.0
二叉树存储路径节点 1.0中虽然实现了寻路的算法,但是使用List<>来保存节点性能并不够强 寻路算法学习1.0在这里:https://www.cnblogs.com/AlphaIcaru ...
- A星寻路算法入门(Unity实现)
最近简单学习了一下A星寻路算法,来记录一下.还是个萌新,如果写的不好,请谅解.Unity版本:2018.3.2f1 A星寻路算法是什么 游戏开发中往往有这样的需求,让玩家控制的角色自动寻路到目标地点, ...
- 基于Unity的A星寻路算法(绝对简单完整版本)
前言 在上一篇文章,介绍了网格地图的实现方式,基于该文章,我们来实现一个A星寻路的算法,最终实现的效果为: 项目源码已上传Github:AStarNavigate 在阅读本篇文章,如果你对于里面提到的 ...
- A*(也叫A star, A星)寻路算法Java版
寻路算法有非常多种,A*寻路算法被公觉得最好的寻路算法. 首先要理解什么是A*寻路算法,能够參考这三篇文章: http://www.gamedev.net/page/resources/_/techn ...
- 算法:Astar寻路算法改进
早前写了一篇<RCP:gef智能寻路算法(A star)> 出现了一点问题. 在AStar算法中,默认寻路起点和终点都是N x N的方格,但如果用在路由上,就会出现问题. 如果,需要连线的 ...
- 无递归 A星寻路算法
整理硬盘的时候,发现我早些年写的A星寻路算法.特放上来,待有缘人拿去,无递归噢,性能那是杠杠的. 码上伺候 public class Node { public int X { get; set; } ...
- JS算法之A*(A星)寻路算法
今天写一个连连看的游戏的时候,接触到了一些寻路算法,我就大概讲讲其中的A*算法. 这个是我学习后的一点个人理解,有错误欢迎各位看官指正. 寻路模式主要有三种:广度游戏搜索.深度优先搜索和启发式搜索. ...
- 一种高效的寻路算法 - B*寻路算法
在此把这个算法称作B* 寻路算法(Branch Star 分支寻路算法,且与A*对应),本算法适用于游戏中怪物的自动寻路,其效率远远超过A*算法,经过测试,效率是普通A*算法的几十上百倍. 通过引入该 ...
- 数据结构和算法总结(三):A* 寻路算法
前言 复习下寻路相关的东西,而且A star寻路在游戏开发中应用挺多的,故记录下. 正文 迪杰斯特拉算法 说起A*得先谈谈Dijkstra算法,它是在BFS基础上的一种带权值的两点最短寻路贪心算法. ...
随机推荐
- Educational Codeforces Round 113 (Rated for Div. 2)
多拿纸画画 ! ! ! Problem - B - Codeforces 题意 给出n个数字(数字为1或2), 1代表这第i个选手没有输过, 2代表这第i个选手至少赢一次 输出为n*n矩阵( i行j ...
- innodb源码解析 - mem0_.c - 基本内存管理
The basic element of the memory management is called a memoryheap. A memory heap is conceptually ast ...
- Java语言学习day02--6月29日
Java语言学习day02###01常用的DOS命令 * A: 常用的DOS命令 * a: 打开Dos控制台 * win+r--cmd--回车 * b: 常用dos命令 * cd.. : 退回到上一级 ...
- 【LeetCode】49. 字母异位词分组
49. 字母异位词分组 知识点:字符串:哈希表 题目描述 给你一个字符串数组,请你将 字母异位词 组合在一起.可以按任意顺序返回结果列表. 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源 ...
- docker入门_docker安装
docker入门_docker安装 ubuntu 安装 curl -sSL https://get.daocloud.io/docker | sh # 官方安装脚本自动安装 systemctl ena ...
- Blazor Bootstrap 组件库 Toast 轻量弹窗组件介绍
轻量级 Toast 弹窗 DEMO https://www.blazor.zone/toasts 基础用法: 用户操作时,右下角给予适当的提示信息 <ToastBox class="d ...
- linux 手动挂载硬盘没有移到回收站解决方法
linux 手动挂载硬盘没有移到回收站解决方法 修改挂载硬盘的文件夹权限为当前用户即可 或者 修改读写权限 chmod 777 mount-disk-path
- 安装黑苹果 、 Mac OS虚拟机
Mac OS 虚拟机 所需文件地址 unlocker 为VMware 新增Apple Mac OS X操作系统 Install_macOS_Monterey_12.0.1_21A559.iso 提取码 ...
- 再见 FTP/SFTP!是时候拥抱下一代文件传输利器了!
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 两台电脑之间该如何传送档案,其实方法有超多种的,像是 FTP 或透过 SSH 方式来传送档案, ...
- KD-Tree及希尔伯特空间填充曲线的应用
引言 我们可能会有这样的一种需求,像是打车软件中呼叫附近的车来接送自己,或者是在qq中查看附近的人.我们都需要知道距离自己一定范围内的其它目标的集合.如果将上面举例的功能抽象出来,就是要实现以某个点为 ...