c# - A* Pathfinding over multiple grids -
i'm attempting implement a* pathfinding around cube, cube made of 6 grids , keep simple have 4 methods getxplus, getxminus, getyplus, getyminus. each method checks see if next tile within current grid space, if not switches appropriate grid.
the problem i'm having when attempting tile grid flipped other way current grid, tile returned on opposite side. there way or approach allow me avoid writing unique logic every single origin grid , direction?
to articulate problem, in have originated (purple) grid , using getxplus method :

a snippit of current implementation (each grid 64 64):
public tile getxplus( int currentx, int currenty ) { var newx = currentx + 1; var tile = gettile( newx , currenty ); if( newx > 64 ) //get adjacent xplus grid { currentgrid = setcurrentgrid( xplusgridindex ); tile = gettile( newx - 64, currenty ); } return tile; } background
this implementation originated excellent answer different question suggested here: https://gamedev.stackexchange.com/questions/53866/pathfinding-on-a-uneven-planetary-surface
i suggest go further suggested previous answer. create cube represents tiles, , cache neighbours of every tile. relations between tiles fixed, safe lot of time.
afterwords can use double[,,] or int[,,,] keep track of processed tiles, , based on add neighbours queue<tile>.
if needed can implement getdirection(tile tile) too. function have search @ directions dictionary.
public class cube { private tile[,,] tiles;
public cube(int size) { tiles = new tile[size, size, 6]; // initialize. (var side = 0; side < 6; side++) { (var x = 0; x < size; x++) { (var y = 0; y < size; y++) { tiles[x, y, side] = new tile(x, y, side); } } } // set directions & neighbors (var side = 0; side < 6; side++) { (var x = 0; x < size; x++) { (var y = 0; y < size; y++) { // todo: implement. } } } } public tile this[int x, int y, int side] { { return tiles[x, y, side]; } } } public class tile { private dictionary<directiontype, tile> directions = new dictionary<directiontype, tile>(); private tile[] neighbors = new tile[4]; public tile(int x, int y, int side) { this.x = x; this.y = y; this.side = side; } public int x { get; private set; } public int y { get; private set; } public int side { get; private set; } public tile this[directiontype dir] { { return directions[dir]; } } public tile[] neighbors { { return neighbors; } } } public enum directiontype { // delta: +1, 0 e, // delta: 0, +1 n, // delta: -1, 0 w, // delta: 0, -1 s, // delta: 0, 0 x }
Comments
Post a Comment