Search Unity

Natural/realistic pathfinding?

Discussion in 'Navigation' started by kiel814, Sep 20, 2021.

  1. kiel814

    kiel814

    Joined:
    Aug 30, 2021
    Posts:
    17
    I have a 2D scene, which is supposed to be a theme park, so there are very wide paths for people to walk on.
    I've successfully implemented an A* pathfinding algorithm, but the problem is that since these algorithms look for the optimal path, the character's movement does not look very natural. For example, they stick to the inner walls when making turns, and stuff like that.
    I tested with a waypoint system, but all characters move in line, so it is even weirder.
    I started experimenting with adding some randomness to the optimal path, but so far I do not like the results.
    I'm trying to find a way that makes characters move in a way that looks natural.
    Has anyone struggled with this? Any suggestions?
     
    Last edited: Sep 20, 2021
  2. SunnyValleyStudio

    SunnyValleyStudio

    Joined:
    Mar 24, 2017
    Posts:
    67
    Hey!

    Implementing A* is usually only half of the work. There are some ways to make the path more optimal but overall it is a lot of tweaking to make it work as expected:
    1) generally you can change the way you calculate the distance between 2 points. I have found that Manhattan distance measurements (https://en.wikipedia.org/wiki/Taxicab_geometry)
    Code (CSharp):
    1. private static float ManhattanDiscance(Point endPos, Point point)
    2.     {
    3.         return Math.Abs(endPos.X - point.X) + Math.Abs(endPos.Y - point.Y);
    4.     }
    Works really good when it comes to the path looking more natural. Instead of meandering around Agents walks longer in a straight path (red or yellow line):


    2) are you using weights (travel cost) in your calculations? The easiest way to create an illusion of "intelligent" agents is to predefine the terrain that is easy to walk on - ex players place paths, or paths are autoreacted around some structures. Now this can be tricky because you usually need to include the ROADS in your game design. Still it makes things very easy.
    upload_2021-9-24_9-21-50.png
    Ex here the trees and rocks represent difficult terrain weight = 2. Road is easy with weight = 0.5. Grass is default weight = 1.
    The character can move much further using the road and the cost of the movement to any square near the road is much lower using the road compared to using the default or difficult terrain.
    You can also tweak the weights to make it extreamly good deal to use the roads to "enforce" the movement on the roads.

    I hope that it gave you some ideas of what you can do to improve your system!
     
  3. kiel814

    kiel814

    Joined:
    Aug 30, 2021
    Posts:
    17
    Sorry for the late answer.
    Your ideas seem interesting, I will definitely throw some of that into my code.

    Just in case anyone else is dealing with this, what I'm trying to do is to have an area attached to each node, so any point within that area is a valid location. Then when I have to move to a node as part of a path, instead of using the node's exact coordinates, I randomly select any point in the area as the location.
    The problem is (programatically) finding the correct area for each node, because even if two nodes can connect to each other, if I select a random point from each node's area, there may not be a valid path between those.

    Unfortunately these past weeks the project has been on hold so I don't have any results to share.
     
  4. kiel814

    kiel814

    Joined:
    Aug 30, 2021
    Posts:
    17
    Your suggestions were awesome, I've been tweaking the pathfinding algorithm a lot.
    - Removed the fixed wayponts and created nodes automatically on start.
    - Added a flag to switch between square/diamond grid.
    - Added a flag to switch between manhattan/straight distance calculations.
    - Added weight to some random nodes before each path calculation.

    I very much like the result so far:
    path.png