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

As far as I know all tile based map editors export a JSON object containing one dimensional arrays. While most pathfinding libraries/tutorials are only provided for two dimensional arrays.

Also if for example I would like to do pathfinding in this one dimensional array and this array is huge i'm geussing this would cause performance issues.

So why is it that most tile based map editors output a one dimensional and how should I handle those regarding pathfinding?

example tile editor:

http://elias-schuett.de/git/Online-Tile-Map-Editor/#

Just google pathfinding to find all the two dimensional patfhfinding tutorials

• Cross-posted: stackoverflow.com/questions/17604573 Jul 11 '13 at 23:28
• Converting a 1D array to a 2D array requires, at most, 4 lines of code. I don't think that's worth worrying about. Aug 23 '13 at 6:35
• I took a look at the online editor you posted, and I believe it's defective; it's not exporting map dimensions. Without that, it's impossible to tell a 3x2 map from a 2x3 map without rendering the map and checking by a human. Rest assured it's not your fault; sane map editors would either output the dimensions, or a 2D array. Aug 24 '13 at 3:15

Well, that's a mildly silly way to represent a map.

In order to use path-finding and such on those maps - or to use them for most anything, really - you need a method for addressing the contents using your 2D coordinates. Assuming you know the dimensions of the map (25x19 in the example you linked) you can translate between the [x,y] coordinates and 1D array index fairly simply:

index = y * 25 + x;


Or in reverse:

int x = index % 25;
int y = index / 25;


And once you can do that, you're off and running. Scan the foreground layer's array for blocking (or non-blocking, depending on your path-finding algorithm) items, converting the resulting list of indices to a list of [x,y] nodes

Depending on your path-finding algorithm you can either generate a list of unblocked or block nodes by scanning the 1D array, then converting indices to [x,y] coordinates.

• I'm still courious why anyone would call point (1,2) point "28". It's making things a bit complicated. Aug 23 '13 at 17:20
• Single-dimension arrays are simpler to work with in a lot of scenarios. I'm more interested in why they're not specifying the row length. Sure, it's using a prime row count, which helps figure it out, but a simple row length value would make using the data slightly simpler. Aug 23 '13 at 21:43