javascript - Tiled based pathfinding in a 1- or 2-dimensional array -


as far know tile based map editors export json object containing 1 dimensional arrays. while pathfinding libraries/tutorials provided 2 dimensional arrays.

also if example pathfinding in 1 dimensional array , array huge i'm geussing cause performance issues.

so why tile based map editors output 1 dimensional , how should handle regarding pathfinding?

example tile editor

just google pathfinding find 2 dimensional patfhfinding tutorials

depending on orientation converted 1-d array;

function convert(x, y, height, width) {     return x + y * width;  // rows     /* return y + x * height; // cols */ }  function reverse(i, height, width) {     var x, y;     // rows     x = % width     y = (i - x) / width     /* // cols     y = % height;     x = (i - y) % height; */     return [x, y]; } 

now, have 6 wide 3 high map

         1-d             |                   2-d  0   1   2   3   4   5   |   x0y0  x1y0  x2y0  x3y0  x4y0  x5y0  6   7   8   9  10  11   |   x0y1  x1y1  x2y1  x3y1  x4y1  x5y1 12  13  14  15  16  17   |   x0y2  x1y2  x2y2  x3y2  x4y2  x5y2 

pick index in 1-d array, e.g. i = 8, convert it's 2-d co-ordinates can use reverse

reverse(8, 3, 6); // [2, 1] //      i, h, w   =  [x, y] 

or picked co-ordinates x = 2, y = 1 in our 2-d array, can convert index in 1-d array convert

convert(2, 1, 3, 6); // 8 //      x, y, h, w   =  

once can convert between 2 systems can path finding normal. can name these functions like, wrote them more can see how switch between 2 systems.

depending on how made, y axis may have 0 @ bottom, not top, or entire thing mirrored across diagonal (which called cols in above functions). depends on how done, long consistent conversion , have correct height , width (read maximum y , maximum x respectively), should not matter.


Comments