# 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*? – Alexandre 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

## 1 Answer

Usually A* is the choosen algorithm for pathfinding in videogames since it's easy to implement and yields a decent behaivour but in your case i think it's not necesary.

If you dissects your needs, you can realize that what you really want is to check the relative position of the target from the enemy, once you have this vector, since your enemy can break walls, you only have to break and advance in that direction.

Moving just toward the player's direction would be weird, so you can use a flood aproach to check what's the closest accessible tile to the player by checking the distance between both entities and move the enemy there. once you get the closest cell you can go, then you break and repeat.

EDIT: You asked for a formula but it's not that easy, we are here to help and support you, not to spoon-feed you. Is your task now to get the answers, learn some maths and implement your code understanding what you'r doing.

Keep trying mate :)

• Hello. Thanks for answer. My enemy can break walls only if there is a closed path. I looked at A*, and Dijkstra. And this flood aproach. So far seems A* is ok, still looking maybe exist something with less iterations. – mansim May 4 '17 at 11:38