Cocos2d-x 寻路算法解析(三):A Star

1.A Star 寻路算法介绍:

看过前面两篇文章的读者知道,这两种寻路算法都有问题,前一个搜索太广了,资源浪费;后一个还不够聪明,有时候会找不到最佳路线。因为A Star 寻路算法就是前面两者的结合。同时考虑离起点的距离和离终点的距离。
 


究竟是C1还是C2这个格子是最佳路线点呢?因为我们这次离起点的距离和离终点的距离都考虑了,所以C1的价值是1.0 + 1.41 + 4.12 总共 6.53的长度。而C2的价值是 2.0 + 6.32 总共 8.32的长度。显然C1更佳。如果按照我们第一个寻路算法,只考虑离起点距离的话,C2还短些呢!但C2离终点远啊。

2.代码实现
 

  1. float distanceBetweenTwoCells(float c1X,float c1Y, float c2X, float c2Y){
  2.     return sqrt(pow(c2X - c1X,2) + pow(c2Y - c1Y,2));
  3. }
  4. bool comparebyDistanceBetweenStartAndGoal(Cell *c1, Cell *c2){
  5.     float distanceOfC1AndGoal = c1->getDistance() +
  6.         distanceBetweenTwoCells((float)c1->getX(),(float)c1->getY(),(float)g_goalX,(float) g_goalY);
  7.     float distanceOfC2AndGoal = c2->getDistance() +
  8.         distanceBetweenTwoCells((float)c2->getX(),(float)c2->getY(),(float)g_goalX,(float) g_goalY);
  9.     if(distanceOfC1AndGoal <= distanceOfC2AndGoal){
  10.         return false;
  11.     }else{
  12.         return true;
  13.     }
  14. }
复制代码


只要加上这个方法就行了,把这个方法作为heap的比较条件。手机号出售平台整个startPathFinding都不需要改,这里再次给出startPathFinding代码:

  1. typedef bool (*compareTwoCells)(Cell *c1, Cell *c2);     
  2. void HelloWorld::startPathFinding(compareTwoCells compareMethod, int startX,int startY,int goalX,int goalY){   
  3.     Cell *startCell = _m_Map.Get(startX, startY);   
  4.     vector<Cell*> vecCells;   
  5.     vecCells.push_back(startCell);   
  6.     make_heap(vecCells.begin(),vecCells.end(),compareMethod);   
  7.     startCell->setMarked(true);   
  8.     Cell *nowProcessCell;   
  9.     while(vecCells.size() != 0){   
  10.         pop_heap(vecCells.begin(),vecCells.end(),compareMethod);   
  11.         nowProcessCell = vecCells.back();   
  12.         vecCells.pop_back();   
  13.         if(nowProcessCell->getX() == _goalX && nowProcessCell->getY() == _goalY){//the goal is reach   
  14.             return;   
  15.         }   
  16.         for(int i = 0; i < 8; ++i){ //check eight direction   
  17.             int indexX = nowProcessCell->getX() + DIRECTION[i][0];   
  18.             int indexY = nowProcessCell->getY() + DIRECTION[i][1];   
  19.             if(indexX >= 0 && indexX < xLineCount && indexY >= 0 && indexY < yLineCount   
  20.                 && _m_Map.Get(indexX,indexY)->getPassable() == true){//check is a OK cell or not   
  21.                     Cell *cell = _m_Map.Get(indexX,indexY);   
  22.                     float beforeDistance = DISTANCE[i] * cell->getWeight() + _m_Map.Get(nowProcessCell->getX(),   
  23.                         nowProcessCell->getY())->getDistance();//calculate the distance   
  24.                     if(cell->getMarked() == false){     
  25.                         cell->setMarked(true);   
  26.                         cell->setLastX(nowProcessCell->getX());   
  27.                         cell->setLastY(nowProcessCell->getY());   
  28.                         cell->setDistance(beforeDistance);   
  29.                         vecCells.push_back(cell);//only push the unmarked cell into the vector   
  30.                         push_heap(vecCells.begin(),vecCells.end(),compareMethod);   
  31.                     }else{// if find a lower distance, update it     
  32.                         if(beforeDistance < cell->getDistance()){   
  33.                             cell->setDistance(beforeDistance);   
  34.                             cell->setLastX(nowProcessCell->getX());   
  35.                             cell->setLastY(nowProcessCell->getY());   
  36.                             make_heap(vecCells.begin(),vecCells.end(),compareMethod);//distance change,so make heap again   
  37.                         }   
  38.                     }   
  39.             }   
  40.         }   
  41.     }   
  42. }   
  43. startPathFinding(comparebyDistanceBetweenStartAndGoal,_playerX,_playerY,_goalX,_goalY);//A star path finding demo
复制代码


函数指针太酷了!

3.三种寻路算法的图片比较

3.1只考虑距离起点的距离,采用Breadth-First
 


特点:可以找到目标,但搜索Cell太多。

3.2 只考虑离目标距离
 


特点:搜索Cell大多情况会相对少些,但不一定能找到最短路线。

3.3 A Star ,同时考虑到起点和终点的距离
 

 


特点:能找到最短路线,搜索的Cell比第一种要少,看最后的搜索方式,也有第二种搜索的智慧。

4.A Star动态gif演示图
 


不知道为什么我对GIF这么热衷。

PS:Cocos2d-x A Star 源码下载见原文。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值