0

Here's an example image:

enter image description here

The blue tile at the top is the starting location and the blue tile at the bottom is the goal location. The green path is the path found between the two.

Astar doesn't seem to work well for this because I don't want the shortest path, but rather an L-shaped path. That is, the minimum edge length should always be 5, and it should look to minimize turns.

My current idea is something along these lines, but I'm not sure if it's ideal or if it will really work:

  1. Move straight 5 tiles
  2. Calculate the vector from the current location to the goal location
  3. If, for example, the direction is mostly south, then travel straight south.
  4. Every tile after 5 tiles straight, evaluate again which direction we should head.
  5. If we decide to change directions, then continue in that direction for at least another 5 tiles until checking again

And then pretty much repeat steps 4 and 5 on loop until we're at the goal location.

It's okay for the pathfinding to sometimes fail if a path cannot be made, or for it to fail if the path exceeds a certain length. The above approach was the first thing that came to mind, but I'm not sure if it's ideal, or if it would even work without sometimes encountering an infinite loop condition where the path keeps going back and forth by getting stuck.

Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
  • Do the black-and-gray rectangles in your example image have any significance? (In particular: does the path need to avoid intersecting them?) If so, then we need more information about what inputs are possible. For example: Can there be lots of black-and-gray rectangles everywhere? Will the blue tiles always be at the edge of such rectangles? Always different rectangles? Always facing opposite directions? (Etc.) – ruakh Mar 14 '21 at 22:20
  • Yeah, sorry, if it helps the black rectangles are "rooms" and the green tiles are "doors", and I'm looking to connect pairs of doors. So yeah, the black rectangles cannot be collided with. There are two types of tiles: walls and open. The black rectangles are filled with "wall" tiles that cannot be collided with, and the rest of the map is completely free and open. The only "wall" tiles are the tiles that fill the rooms. And this entire thing is just a grid (a 1D array of 1's and 0's where 0's are walls and 1's are free spaces). And yeah, always different rectangles. – Ryan Peschel Mar 14 '21 at 22:21
  • Also, blue tiles will always be at the edge of these rectangles, yes. – Ryan Peschel Mar 14 '21 at 22:28

0 Answers0