# What algorithm should I use for 2D pathfinding?

In my game I want to spawn a monster which would immediately calculate a path to its target. If the monster faces any new obstacles, he destroys it and goes further on the calculated way. If there is no path to the target (the way is fully blocked) he destroys everything and goes straight to the target. The target is this Yellow cube. Maze is made from black cubes. And available path is in white. The monster can't go crosswise.

The total area width and height can be different. Here now is 12x14.

Could you help me please with formula? Given maze_width + maze_height + starting_point + target_point + all area black cubes x/y coordinates.

This is too complex for my non-mathematical head and I really want to implement this pathfinding formula in my game :)

So I need only one formula for the first once-made pathfinding, nothing more. I will use it with JavaScript.

Thanks in advance for any help!

const MAZE_WIDTH = 12;
const MAZE_HEIGHT = 14;
const STARTING_POINT = [0, 1];
const TARGET_POINT = [6, 6];

function get_path(maze_blocks) {
..
}

var maze_blocks = [[2, 1], [8, 1], [1, 7] ... ]; // not sorted
get_path(maze_blocks) // returns [[0, 1], [1, 1], [1, 2], [1, 3] ...]


• Did you look into something like A*? – Vaillancourt May 2 '17 at 10:12
• We don't support questions asking for technology to use,vplease refer to our help center – Bálint May 2 '17 at 10:39
• It is not an ask for technology to use. why did you think that?? I am asking for help with formula. – mansim May 2 '17 at 12:30
• Dijkstra's algorithm is even a step simpler than A*. – Philipp May 2 '17 at 22:18
• @Philipp in the absence of different edge costs, you could go a step simpler still and just run breadth-first search to get the same results as Dijkstra's. ;) – DMGregory May 3 '17 at 11:59