1
\$\begingroup\$

What I want to achieve is the player moving to the touched position on a 2D grid-based game, stopping at the last reachable spot if the target position is unreachable. What is the strategy usually adopted in these cases? Should I implement a specific path finding algorithm for the player?

\$\endgroup\$
  • \$\begingroup\$ Not sure if there is a usual case which would fit all methods of getting something from point A to point B. It all depends really. for example, an implementation of A* could get you to the closest spot. tutorial here policyalmanac.org/games/aStarTutorial.htm \$\endgroup\$ – RobM Apr 30 '16 at 14:01
  • \$\begingroup\$ I was already thinking of an A* algorithm -for each node, calculate the distance to the target from the four nodes around, then move to the closest non obstructed of these nodes and repeat- but my main doubt was: If the target is unreachable (say the target is in the water and the player is already near the river) who tells the player to stop there and not continuosly move up and down the river on the two closest non-obstructed nodes? \$\endgroup\$ – user6245072 Apr 30 '16 at 14:24
  • \$\begingroup\$ Is the target unreachable due to changes after the path was found or was it unreachable at the beginning? \$\endgroup\$ – Pikalek Apr 30 '16 at 16:11
  • \$\begingroup\$ At the beginning \$\endgroup\$ – user6245072 Apr 30 '16 at 16:22
  • 1
    \$\begingroup\$ Possible duplicate of How to achieve partial pathfinding? \$\endgroup\$ – congusbongus Sep 8 '16 at 4:13
2
\$\begingroup\$

This question on partial path-finding presents one solution - modify your path finding algorithm to remember the node visited that had the closest estimated distance to the target. Eventually, the path-finding will fail to get a route to the target (since it was unreachable), but you will have a path to the closest point encountered while it searched.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.