游戏开发论坛

 找回密码
 立即注册
搜索
查看: 7484|回复: 0

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

[复制链接]

1万

主题

1万

帖子

3万

积分

论坛元老

Rank: 8Rank: 8

积分
36572
发表于 2015-11-12 16:18:23 | 显示全部楼层 |阅读模式
80896787ha50e4390d526&690.png

  文 / 易水寒

  接两篇文章:《Cocos2d-x 寻路算法解析(一): 距离优先》、《Cocos2d-x 寻路算法解析(二): 离目的地的距离优先

  1.A Star 寻路算法介绍:

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

1_副本.jpg

  究竟是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

2_副本.jpg

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

  3.2 只考虑离目标距离

3_副本.jpg

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

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

4_副本.jpg

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

  4.A Star动态gif演示图

5.gif

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

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

  相关阅读a寻路算法(for 初学者)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-2-25 13:51

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表