|
??? / ???
?????????Cocos2d-x ?????????? ???????Cocos2d-x ?????????? ??????????
??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.????
- float distanceBetweenTwoCells(float c1X,float c1Y, float c2X, float c2Y){
-
- return sqrt(pow(c2X - c1X,2) + pow(c2Y - c1Y,2));
- }
- bool comparebyDistanceBetweenStartAndGoal(Cell *c1, Cell *c2){
- float distanceOfC1AndGoal = c1->getDistance() +
- distanceBetweenTwoCells((float)c1->getX(),(float)c1->getY(),(float)g_goalX,(float) g_goalY);
-
- float distanceOfC2AndGoal = c2->getDistance() +
- distanceBetweenTwoCells((float)c2->getX(),(float)c2->getY(),(float)g_goalX,(float) g_goalY);
- if(distanceOfC1AndGoal <= distanceOfC2AndGoal){
- return false;
- }else{
- return true;
- }
- }
复制代码
?????????????????????heap????????startPathFinding????????????startPathFinding???
- 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(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 ????????
???????a????(for ???)
|
|