首页 > 代码库 > cocos2dx A*算法
cocos2dx A*算法
头文件和源文件拷贝到项目中就能用了! have fun
使用cocos2dx 3.2 原理都一样
淡蓝色的点是地图
深蓝色的点是障碍物
绿色的点是路径
暗绿色的点是搜寻过的点
红色的点是按路径行走的点
dijkstra算法 会发现路径最短,但寻找过的路径比較多(计算速度慢)
最佳优先搜索算法会发现寻找过的路径少了(计算速度提高了),但走了很多弯路
A星算法 结合了上面2种算法 即寻找到了最短路径, 搜寻过的路径也比較少
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "vector" using namespace std; USING_NS_CC; class PathSprite : public cocos2d::Sprite//继承Sprite类, 由于要在里面加些其它变量 { PathSprite():Sprite() { m_parent = NULL; m_child = NULL; m_costToSource = 0; m_FValue = http://www.mamicode.com/0;>#include "HelloWorldScene.h" vector<PathSprite*> PathSearchInfo::m_openList; vector<PathSprite*> PathSearchInfo::m_inspectList; vector<PathSprite*> PathSearchInfo::m_pathList; int PathSearchInfo::m_startX; int PathSearchInfo::m_startY; int PathSearchInfo::m_endX; int PathSearchInfo::m_endY; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size winSize = Director::getInstance()->getWinSize(); ///////////////////////////// // 2. add a menu item with "X" image, which is clicked to quit the program // you may modify it. // add a "close" icon to exit the progress. it's an autorelease object auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this); listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); //模拟一张地图 左上角 为(0,0) 主要是模拟tiledmap 每块的宽度为1 int _width = 25; int _heigth = 15; for (int i = 0; i < _heigth; i++) { for (int j = 0; j < _width; j++) { PathSprite* _sp = PathSprite::create("CloseNormal.png"); _sp->m_x = j; _sp->m_y = i; Size _size = _sp->getContentSize(); _sp->setPosition(CCPoint(j * _size.width + 100, - i * _size.height + 600)); m_mapList.push_back(_sp); this->addChild(_sp); } } //设置障碍物 // for (int i = 0; i < _heigth*_width/2; i++) // { // // int _x = CCRANDOM_0_1()*_width; // int _y = CCRANDOM_0_1()*_heigth; // if (_x ==0 && _y == 0) { // continue; // } // PathSearchInfo::barrierTest(m_mapList,_x,_y); // } for (int i = 0; i < 10; i++) { PathSearchInfo::barrierTest(m_mapList,5+i,10); PathSearchInfo::barrierTest(m_mapList,15,i+1); } //PathSprite::getObjByPointOfMapCoord(m_inspectList, 2, 5)->removeFromParent(); //设置起始和终点 PathSearchInfo::m_startX =0; PathSearchInfo::m_startY = 0; PathSearchInfo::m_endX = 4; PathSearchInfo::m_endY = 9; m_player = PathSprite::create("CloseSelected1.png"); m_player->setColor(Color3B::RED); this->addChild(m_player); m_player->m_x = PathSearchInfo::m_startX; m_player->m_y = PathSearchInfo::m_startY; m_player->setPosition(PathSearchInfo::getObjByPointOfMapCoord(m_mapList, PathSearchInfo::m_startX, PathSearchInfo::m_startY)->getPosition()); return true; } void HelloWorld::calculatePath() { //得到開始点的节点 PathSprite* _sp = PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList, PathSearchInfo::m_startX, PathSearchInfo::m_startY); //得到開始点的节点 PathSprite* _endNode = PathSearchInfo::getObjByPointOfMapCoord(PathSearchInfo::m_inspectList, PathSearchInfo::m_endX, PathSearchInfo::m_endY); //由于是開始点 把到起始点的距离设为0 _sp->m_costToSource = 0; _sp->m_FValue = http://www.mamicode.com/0;>
cocos2dx A*算法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。