看了几天的A*算法,感觉要成为一个游戏开发者,有必要把这个著名的算法拿到手。

网上有此算法的代码片段,但主要还是些模板类的伪代码,所以想分享一段完整的A*算法代码供大家更好的理解!(这里使用的是js语言和cocos2d游戏引擎)

对于A*算法概念性的描述,请看这里,本篇主要以代码为主。

下面是我的学习成果,有晦涩和需改进的地方欢迎吐槽!

var A_STAR_DISTANCE = 7;                    //像素大小,越小越精确,同时越耗时
var A_STAR_G_EXPEND_MIN = 10; //上下左右G值消耗数
var A_STAR_G_EXPEND_MAX = 14; //斜角G值消耗数 var HelloWorldLayer = cc.Layer.extend({
sprite:null, //角色
aStarPathArray:[], //最终角色要行走的路径
aStarBarrierArray:[], //地图障碍物
testNumber:1,
ctor:function () {
//////////////////////////////
// 1. super init first
this._super();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// ask director the window size
var size = cc.director.getWinSize(); this.sprite = cc.Sprite.create(res.Plane_png); //角色初始化
this.sprite.attr({
x: 150,
y: 50,
rotation: 90
});
this.addChild(this.sprite, 0); var barrier1 = cc.rect(200,50,50,350); //绘制障碍物
var barrier2 = cc.rect(250,50,350,50);
var barrier3 = cc.rect(250,350,350,50);
this.aStarBarrierArray.push(barrier1);
this.aStarBarrierArray.push(barrier2);
this.aStarBarrierArray.push(barrier3); var drawBarrier = cc.DrawNode.create(); //在屏幕上显示障碍物
this.addChild(drawBarrier, 10);
var vertices = [cc.p(200,50),cc.p(600,50),cc.p(600,100),cc.p(250,100),cc.p(250,350),cc.p(600,350),cc.p(600,400),cc.p(200,400)];
drawBarrier.drawPoly(vertices,null,2,cc.color(255,0,0,255)); if ('mouse' in cc.sys.capabilities)
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseUp:function(event){
if(event.getButton() != undefined)
{
var t = new Date().getTime();
event.getCurrentTarget().getStartAndEndPoint(event); //A*算法开始
cc.log("算法耗时:"+(new Date().getTime() - t)+"ms"); //计算起点到终点的算法耗时
}
}
}, this); return true;
},
getStartAndEndPoint:function (event) { //得到起始点和终点坐标
var sp = {coordX:parseInt(this.sprite.x,10),coordY:parseInt(this.sprite.y,10)};
var ep = {coordX:parseInt(event.getLocation().x,10),coordY:parseInt(event.getLocation().y,10)};
var endPointIsObstacle = false; //判断终点是否在障碍物上,是的话就提示路径走不了
for (var theBarrierIndex=0; theBarrierIndex<this.aStarBarrierArray.length; theBarrierIndex++){
if(cc.rectContainsPoint(this.aStarBarrierArray[theBarrierIndex],cc.p(ep.coordX,ep.coordY)))
{
endPointIsObstacle = true;
cc.log("你要去的位置有障碍物,请换条路线");
break;
}
}
if(!endPointIsObstacle)
this.findingPath(sp,ep);
},
findingPath:function(startPoint,endPoint){ //A*算法
var openList = []; //初始化开启列表
var closeList = []; //初始化关闭列表
startPoint.ag = 0;
startPoint.ah = 0;
startPoint.af = startPoint.ag + startPoint.ah; //起点的G,H,F值为0
openList.push(startPoint); //起点加入开启列表
var findTheWay = false;
do{
var centerNode = this.findMinNode(openList); //寻找F值最低的节点
openList.remove(centerNode); //将此节点从开启列表中删除,为了下次遍历开启列表的时候不再出现此节点
closeList.push(centerNode); //并将此节点加入到关闭列表
for(var i=0;i<8;i++) //遍历此节点周围的节点,并给这些节点加入坐标属性和G值
{
var aroundNode = {};
switch (i){
case 0:
aroundNode.coordX = centerNode.coordX+A_STAR_DISTANCE; //坐标属性
aroundNode.coordY = centerNode.coordY+A_STAR_DISTANCE;
break;
case 1:
aroundNode.coordX = centerNode.coordX+A_STAR_DISTANCE;
aroundNode.coordY = centerNode.coordY;
break;
case 2:
aroundNode.coordX = centerNode.coordX+A_STAR_DISTANCE;
aroundNode.coordY = centerNode.coordY-A_STAR_DISTANCE;
break;
case 3:
aroundNode.coordX = centerNode.coordX;
aroundNode.coordY = centerNode.coordY-A_STAR_DISTANCE;
break;
case 4:
aroundNode.coordX = centerNode.coordX-A_STAR_DISTANCE;
aroundNode.coordY = centerNode.coordY-A_STAR_DISTANCE;
break;
case 5:
aroundNode.coordX = centerNode.coordX-A_STAR_DISTANCE;
aroundNode.coordY = centerNode.coordY;
break;
case 6:
aroundNode.coordX = centerNode.coordX-A_STAR_DISTANCE;
aroundNode.coordY = centerNode.coordY+A_STAR_DISTANCE;
break;
case 7:
aroundNode.coordX = centerNode.coordX;
aroundNode.coordY = centerNode.coordY+A_STAR_DISTANCE;
break;
}
for (var barrierIndex=0; barrierIndex<this.aStarBarrierArray.length; barrierIndex++){
aroundNode.isOb = cc.rectContainsPoint(this.aStarBarrierArray[barrierIndex],cc.p(aroundNode.coordX,aroundNode.coordY)); //判断当前节点是否在障碍物形成的方框里
if(aroundNode.isOb)
break;
}
if (aroundNode.isOb){ //如果是障碍物,跳过 }
else if(closeList.hasObject(aroundNode)){ //如果在关闭列表里,跳过 }
else if(!openList.hasObject(aroundNode)){ //如果不在开启列表里,加入到开启列表
aroundNode.parentPath = centerNode;
if (Math.abs(aroundNode.coordX-endPoint.coordX)<=A_STAR_DISTANCE/2 && Math.abs(aroundNode.coordY-endPoint.coordY)<=A_STAR_DISTANCE/2) //如果节点和终点的值相近,那么A*算法结束,得到路径
{
findTheWay = true;
var pathArry = [];
this.gettingAStarPath(aroundNode,pathArry); //寻找路径
pathArry.splice(0,0,{starX:endPoint.coordX,starY:endPoint.coordY}); //加终点到数组头部
pathArry.splice(pathArry.length-1,1); //删一项数组底部的起点数据,此时的数组是最终的路径数组 this.aStarPathArray = [];
this.aStarPathArray = pathArry;
this.aStarPathArray.theIndex = this.aStarPathArray.length; this.unschedule(this.thePathSelector);
this.schedule(this.thePathSelector,null,pathArry.length-1);
break; //找到最短路径并跳出循环
}
if (aroundNode.coordX!=centerNode.coordX && aroundNode.coordY!=centerNode.coordY) //确定中心节点和周围节点形成的角度,正交G值消耗10*像素,斜角G值消耗14*像素
aroundNode.ag = centerNode.ag + A_STAR_G_EXPEND_MAX*A_STAR_DISTANCE;
else
aroundNode.ag = centerNode.ag + A_STAR_G_EXPEND_MIN*A_STAR_DISTANCE;
aroundNode.af = this.getAF(aroundNode,endPoint);
openList.push(aroundNode);
}
else if(openList.hasObject(aroundNode)){ //如果在开启列表里
var newExpend = A_STAR_G_EXPEND_MIN*A_STAR_DISTANCE;
if (aroundNode.coordX!=centerNode.coordX && aroundNode.coordY!=centerNode.coordY) //确定中心节点和周围节点形成的角度,正交G值消耗10*像素,斜角G值消耗14*像素
newExpend = A_STAR_G_EXPEND_MAX*A_STAR_DISTANCE;
if (centerNode.ag + newExpend < aroundNode.ag){ //如果新的g值小于周围节点本身的g值,那么周围节点的父节点改为当前中心节点,并重新计算其F值
aroundNode.parentPath = centerNode;
aroundNode.ag = centerNode.ag + newExpend;
aroundNode.af = this.getAF(aroundNode,endPoint);
}
}
}
}while(!findTheWay) },
findMinNode:function(openListArray){
var minNode = openListArray[0];
for (var i=0;i<openListArray.length;i++)
{
if (minNode.af>openListArray[i].af) minNode=openListArray[i];
}
return minNode;
},
getAF:function(thisNode,endNode){
var aHExpend = (Math.abs(thisNode.coordX-endNode.coordX) + Math.abs(thisNode.coordY-endNode.coordY))*A_STAR_G_EXPEND_MIN;
return aHExpend+thisNode.ag;
},
gettingAStarPath:function(laseNode,array){
if(laseNode.parentPath != null)
{
array.push({starX:laseNode.parentPath.coordX,starY:laseNode.parentPath.coordY});
this.gettingAStarPath(laseNode.parentPath,array);
}
},
thePathSelector:function(){
this.roleRunThePath(this.aStarPathArray);
},
roleRunThePath:function(array){
this.sprite.x = array[--array.theIndex].starX;
this.sprite.y = array[array.theIndex].starY;
}
}); var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new HelloWorldLayer();
this.addChild(layer);
}
}); //这里给Array数组添加3个实例方法
Array.prototype.aStarIndexOf = function(val) { //通过对象寻找index值
for (var i = 0; i < this.length; i++) {
if (this[i].coordX==val.coordX && this[i].coordY==val.coordY) return i;
}
return -1;
};
Array.prototype.remove = function(val) { //删除相应的对象
var index = this.aStarIndexOf(val);
if (index > -1) {
this.splice(index, 1);
}
}; Array.prototype.hasObject = function(val){ //判断是否是同一个对象
for (var i = 0; i < this.length; i++){
if (this[i].coordX==val.coordX && this[i].coordY==val.coordY)
return true;
}
return false;
};

如下图,飞机在寻找路径的时候会避开红色区域。

A*寻路算法 (cocos2d-js详细代码)的更多相关文章

  1. Python——EM(期望极大算法)教学(附详细代码与注解)

    今天,我们详细的讲一下EM算法. 前提准备 Jupyter notebook 或 Pycharm 火狐浏览器或谷歌浏览器 win7或win10电脑一台 网盘提取csv数据 需求分析 实现高斯混合模型的 ...

  2. cocos2d JS 使用代码判断对象类型

    changeAtlasScoreString : function (score,tfScore) { if(tfScore.getDescription() == "LabelAtlas& ...

  3. 如何在Cocos2D游戏中实现A*寻路算法(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  4. js实现A*寻路算法

    这两天在做百度前端技术学院的题目,其中有涉及到寻路相关的,于是就找来相关博客进行阅读. 看了Create Chen写的理解A*寻路算法具体过程之后,我很快就理解A*算法的原理.不得不说作者写的很好,通 ...

  5. 【转载】 A* 寻路算法 (个人认为最详细,最通俗易懂的一个版本)

    原文地址: http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html =============================== ...

  6. 如何在Cocos2D游戏中实现A*寻路算法(八)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  7. 如何在Cocos2D游戏中实现A*寻路算法(一)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  8. 6. EM算法-高斯混合模型GMM+Lasso详细代码实现

    1. 前言 我们之前有介绍过4. EM算法-高斯混合模型GMM详细代码实现,在那片博文里面把GMM说涉及到的过程,可能会遇到的问题,基本讲了.今天我们升级下,主要一起解析下EM算法中GMM(搞事混合模 ...

  9. 4. EM算法-高斯混合模型GMM详细代码实现

    1. EM算法-数学基础 2. EM算法-原理详解 3. EM算法-高斯混合模型GMM 4. EM算法-高斯混合模型GMM详细代码实现 5. EM算法-高斯混合模型GMM+Lasso 1. 前言 EM ...

  10. 数据挖掘领域十大经典算法之—C4.5算法(超详细附代码)

    https://blog.csdn.net/fuqiuai/article/details/79456971 相关文章: 数据挖掘领域十大经典算法之—K-Means算法(超详细附代码)        ...

随机推荐

  1. 使用 javascript 来实现 观察者模式

    以[猫叫.老鼠跑.主人醒]为例子,使用 javascript 来实现 观察者模式 (有在线演示) 2013-06-24 08:35 by 金色海洋(jyk)阳光男孩, 572 阅读, 4 评论, 收藏 ...

  2. JSTL(JSP Standard Tag Library ,JSP标准标签库)

    JSTL标签之核心标签   JSTL(JSP Standard Tag Library ,JSP标准标签库)是一个实现 Web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断.数据管 ...

  3. java自动生成略缩图

    当你要做一个图库的项目时,对图片大小.像素的控制是首先需要解决的难题. 本篇文章,在前辈的经验基础上,分别对单图生成略缩图和批量生成略缩图做个小结. 一.单图生成略缩图 单图经过重新绘制,生成新的图片 ...

  4. Android——另外一种增删查改的方式(ContentProvider常用)

    以下介绍另外一种增删查改的方式 package com.njupt.sqllist; import java.util.ArrayList; import java.util.List; import ...

  5. JavaScript中null和undefined

    JavaScript的数据类型大体分为两类:原始类型和对象类型.其中,原始类型包括数字.字符串和布尔值.此外,JavaScript中还有两个特殊的原始值:null(空)和undefined(未定义), ...

  6. There is no getter for property named 'userSpAndSp' in 'class com.uauth.beans.UserSpAndSp'

    mybatis 报错There is no getter for property named 'userSpAndSp' in 'class com.uauth.beans.UserSpAndSp' ...

  7. es6笔记2^_^array

    一.Array.from() Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括ES6新增的数据结构Se ...

  8. 【翻译】Asp.net Core介绍

    ASP.NET Core is a significant redesign of ASP.NET. This topic introduces the new concepts in ASP.NET ...

  9. PostScript学习:另一种缩写为PS的技术

    1.前言 PostScript是一种编程语言,直译为"后处理脚本"[相对印刷过程而言],学名为页面描述语言.更为详细的解释见维基百科,以及其翻译版百度百科. 值得一提的是,Post ...

  10. (十一)if...else&for循环&while循环

    ----------------------------------if else------------------------------1.最基本的if语句:if name =="Al ...