0

I have x,y coordinates board like this:

enter image description here

the board is up to 100x100.

Where myPosition is gold, destinations is green and collisions is red. myPosition is object destinations and collisions are array of objects:

let myPosition={x:0,y:0};
let destinations = [{x: 0, y: 5}, {x: 2, y: 0}, {x: 2, y: 2}];
let collisions = [{x: 1, y: 0},{x: 1, y: 1},{x: 1, y: 2},{x: 1, y: 3},{x: 1, y: 4},{x: 2, y: 1},{x: 2, y: 0},{x: 2, y: 1}]

With this code (live demo) I'm able to find nearest destination but it doesn't know about collisions at all. I can not figure how to write algorithm which additionally would check for collision and give ouput 0,5 in above scenerio.

There is also assumption that we can not move diagonally.

I found this SO answer which seems to provide answer to my question but I can not get it to work with my input arrays.

BT101
  • 3,666
  • 10
  • 41
  • 90
  • This sounds to me like you're looking for a pathing solution, the first that comes to mind is a-star ( a* ) see: http://buildnewgames.com/astar/ . a* is also mentioned in the SO answer you referenced – Steven Stark Mar 22 '19 at 17:44
  • even your current solution is wrong as it calculates squared distance so it allows diagonal moves. You want to do something like bfs – juvian Mar 22 '19 at 17:46
  • I found A* library but in usage https://github.com/prettymuchbryce/easystarjs#usage there is grid with 0 and 1 where I believe 0 is walkable and 1 is not. Can I somehow convert my object/arrays to grid like this? – BT101 Mar 22 '19 at 18:46

1 Answers1

0

I used pathFinding.js library and finding is very simple:

currentPath = finder.findPath(heroCoords.x, 
                              heroCoords.y, 
                              monstersCoords[i].x, 
                              monstersCoords[i].y,
                              currentGrid);
BT101
  • 3,666
  • 10
  • 41
  • 90