游戏开发论坛

 找回密码
 立即注册
搜索
查看: 7597|回复: 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-10-8 16:40

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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