I am sharing my one class called TMXPathFinding which will help you to find Path from start pos to goal pos avoiding obstacles. Its a implementation of A* algo. Hope it will help lot of newbies. Feedback always welcome.
It uses TMXTiledMap object.
It supports orthogonal, hexagonal and isometric tile maps.
During Path finding, it takes current GID values from TMXLayers.
Features:
Find Path between two Tile coordinates
DIRECTION support :
DIRECTION::FOUR - L, R, U, D
DIRECTION::EIGHT - L, R, U, D, TL, BR, TR, BL
DIRECTION::SIX - For Hexagonal maps
You can add multiple Obstacle tiles. e.g.- TILE_WALL, TILE_WATER
You can add multiple Walkable tiles. e.g.- TILE_ROAD, TILE_GRASS
You can find path using only Obstacle tiles
You can find path using only Walkable tiles
You can find path for single or combined TMXLayers
Limitations:
Multiple path finding is not possible
Only one shortest path will return
How to use?
You have to pass TMXTiledMap and TMXPathFinding::DIRECTION to create TMXPathFinding object
TMXTiledMap *tileMap = TMXTiledMap::create(“level_1.tmx”);
TMXPathFinding *pathFind = new TMXPathFinding(tileMap, TMXPathFinding::DIRECTION::FOUR);
Note : For Hexagonal you must pass DIRECTION::SIX
For Orthogonal & Isometric DIRECTION::FOUR or DIRECTION::EIGHT
You can set desired Layers list, [Note: If you don’t call this, it will check for all layers in given TMXTiledMap] pathFind->setTileLayers({"Layer_1", "Layer_2", "Layer_3"});
If you want to reset tile layers, means algo should check for all layers pathFind->setTileLayers( { } );
something wrong…
i guess that same case code~
…
case 6: // bottom-right
newNode->setLocX(1);
newNode->setLocY(-1);
break;
case 7: // top-right
newNode->setLocX(1);
newNode->setLocY(-1);
break;
Ohk, that result is correct only.
But you want straight line then i think by replacing Euclidean Distance with Manhatten Distance will solve your problem.
Just replace this line in euclideanDistance method
Hi,
i am a begineer in cocos2d.i am developed a ludo game .i have some doubts about this
is this path finder script useful for ludo game.
how to move startpos to stoppos using this script.
suggest any ideas about developing ludo based game.
pls help me
No, this path-finding is implementation of A* algo.
For Ludo game you have to code your own way.
You can take pre-defined array of pos to navigate through.
Thank you for replying…
is position is taken from TMX object layer or TileGID. pls mention to get pos from TMXobject.i see several times but i didnt get position from TMXObject so pls mention …
hey bro , i don’t understand line end of funcion getpath(…) , Its must return vector of open or close , why return dimmy ??? can you help me , please :((