# Cocos2d-x 寻路算法之二 离目的地的距离优先

Figure 1

## 2.开始动手

Figure 2

bool comparebyWhichNearerGoalSimpleWay(Cell *c1, Cell *c2){
int distanceOfC1AndGoal = abs(g_goalX - c1->getX()) + abs(g_goalY - c1->getY());
int distanceOfC2AndGoal = abs(g_goalX - c2->getX()) + abs(g_goalY - c2->getY());
if(distanceOfC1AndGoal <= distanceOfC2AndGoal){
return false;
}else{
return true;
}
}

    typedef bool (*compareTwoCells)(Cell *c1, Cell *c2);
void HelloWorld::startPathFinding(compareTwoCells compareMethod, int startX,int startY,int goalX,int goalY){
Cell *startCell = _m_Map.Get(startX, startY);
vector<Cell*> vecCells;
vecCells.push_back(startCell);
make_heap(vecCells.begin(),vecCells.end(),compareMethod);
startCell->setMarked(true);
Cell *nowProcessCell;

while(vecCells.size() != 0){
pop_heap(vecCells.begin(),vecCells.end(),compareMethod);
nowProcessCell = vecCells.back();
vecCells.pop_back();

if(nowProcessCell->getX() == _goalX && nowProcessCell->getY() == _goalY){//the goal is reach
return;
}

for(int i = 0; i < 8; ++i){ //check eight direction

int indexX = nowProcessCell->getX() + DIRECTION[i][0];
int indexY = nowProcessCell->getY() + DIRECTION[i][1];

if(indexX >= 0 && indexX < xLineCount && indexY >= 0 && indexY < yLineCount
&& _m_Map.Get(indexX,indexY)->getPassable() == true){//check is a OK cell or not
Cell *cell = _m_Map.Get(indexX,indexY);
float beforeDistance = DISTANCE[i] * cell->getWeight() + _m_Map.Get(nowProcessCell->getX(),
nowProcessCell->getY())->getDistance();//calculate the distance
if(cell->getMarked() == false){
cell->setMarked(true);
cell->setLastX(nowProcessCell->getX());
cell->setLastY(nowProcessCell->getY());
cell->setDistance(beforeDistance);
vecCells.push_back(cell);//only push the unmarked cell into the vector
push_heap(vecCells.begin(),vecCells.end(),compareMethod);
}else{// if find a lower distance, update it
if(beforeDistance < cell->getDistance()){
cell->setDistance(beforeDistance);
cell->setLastX(nowProcessCell->getX());
cell->setLastY(nowProcessCell->getY());
make_heap(vecCells.begin(),vecCells.end(),compareMethod);//distance change,so make heap again
}
}
}

}
}
}
startPathFinding(comparebyWhichNearerGoalSimpleWay,_playerX,_playerY,_goalX,_goalY);//demo  

Figure 3

## 4.算法改进

float distanceBetweenTwoCells(float c1X,float c1Y, float c2X, float c2Y){

return sqrt(pow(c2X - c1X,2) + pow(c2Y - c1Y,2));
}

bool comparebyWhichNearerGoalPhysicWay(Cell *c1, Cell *c2){
float distanceOfC1AndGoal = distanceBetweenTwoCells((float)c1->getX(),(float)c1->getY(),(float)g_goalX,(float) g_goalY);

float distanceOfC2AndGoal = distanceBetweenTwoCells((float)c2->getX(),(float)c2->getY(),(float)g_goalX,(float) g_goalY);
if(distanceOfC1AndGoal <= distanceOfC2AndGoal){
return false;
}else{
return true;
}
}

## 5.该算法存在的问题

1.很容易想到的一个问题是，它没有考虑权重！如果目标在右侧，而右侧是一条非常难走的路，那么这个算法将毫无顾虑地走过去，丝毫不考虑就在不远处有条非常轻松的路。下面这个图就可以说明这个问题。

2.还有个问题，即使没有权重Cell的存在，只有可通过和不可通过Cell的存在，这个算法也有问题，我们可以人为地制造一个陷进，虽然目标在起点的下方，但是上面有条更近的路，这个算法应该会愚蠢地在往下找吧，这个就跟人一样，有时候目光短浅。下图是演示结果。

## 6.项目下载

(请用7z解压，开发工具vs2010)

http://www.waitingfy.com/?attachment_id=828

A*算法应用可以看下这篇文章《
• 本文已收录于以下专栏：

举报原因： 您举报文章：Cocos2d-x 寻路算法之二 离目的地的距离优先 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)