Node based pathfinding, how to calculate shortest path?

Hello,

ATM. every npc has a list with all nodes within the distance of the npc to the target(player).
Sorted that list on distance, so closest is first. Well this is where my understanding of grabbing those items ends.

At this point the npc follows last entry in list to the closest node near player list.pop(-1), then to player itself. But it goes trough every node within that distance, while i want it to follow the shortest path to the player with those nodelist.

I got this now:

with some distance sorting i ended up with this:

Got any ideas on how to tackle that one?

1 Like

Mine is grid based so it’s a little different (plus it’s still a WIP), but here’s how I do.

  1. Get list of adjacent tiles
  2. Loop thru the list, get distance to starting point and goal
  3. Build a dict. reach = {“nodes”:list of tiles,“dist”: list of tuples (dist1,dist2) }
  4. Pass the dict to a function that determines which point is closer.
#(...)
index_min = min(range(len(reach["dist"])), key=reach["dist"].__getitem__)
nearest_point = reach["nodes"][index_min]
nearest_dist = reach["dist"][index_min]
i = 0
#-------
for point in reach["nodes"]:
    node,dist = reach["nodes"][i],reach["dist"][i]
    #------- 
    # dist[0] = distance to start. dist[1] = distance to goal
    # I initially got which tuple the sum of is a smaller number
    # for now I check if another node is closer to the end
    # absolutely **not** optimal in some situations involving obstacles,
    # working on a better solution at the moment.
    if (dist[1] < nearest_dist[1]):
        nearest_point = node
        nearest_dist = dist
    #-------
    i += 1
return nearest_point

Repeat until we find a path to the goal or realize the goal is unreachable (but that’s another story c:)

Thing you could try with nodes, is linking them together – e.g., node A to B, C to B, B to AC.
So then you can build path out of a list of neighboring nodes.
Tricky part might be building this list programmatically; unless you use meshes.
Then you can just check if two vertices share the same coordinates and be done with it.

I posted a pathfinder that used quads from bpy so it even works in upbge

it uses quads that overlap to define nodes should do what you need.

break the world up into objects that are under a certain number of leaves

if goal and end are on same object, just use object nodes

if object is on object connected to start object - path start and end object

if outside the range - pathfind objects-> return all objects visited -> build graph of just those objects
pathfind that.

you can take a look at this node based implementation of mine

http://15b.dk/blendfiles/astar-kdtree.blend

@Liebranca
Thank you for that example, i will try to create something based on your example.

@BluePrintRandom
Since when can we use Bpy in game engine? Anyway i will look it up and see how it’s done.

@edderkop
Interesting class, i will defently look deeper into it, thanks for the example.

I will try a few things out later this week, very busy atm, i’ll report back.

How BPR manages to pull off that sweet black magic is anyone’s guess c:

There’s a whole lot more going on with my code (setting up the grid, evaluating whether a path is viable, excluding tiles that lead to dead ends, aborting the search if there’s no way to reach the end goal, etc), I had to simplify the example quite a bit.

The pathfinding itself is just around 150~ lines total, but since it’s dependent on other systems (and written with those systems in mind) it’d take some rewriting to make it work on a different environment, so I just shared the basic idea behind it. I can send you the whole thing later if you want to take a look at it anyway.

Cheers.

Yes of course.

i got stuck figuring out how to even find the closest point. Now if i am right i can just implement the suggestion you said with the distances, and calculate the shortest path, because i already got all the nodes between the start- and endpoint.

And that is exactly what i needed. If i try to hard i get tunnel vision and getting stuck on a few ways and always end up the same. This example let me look at it from a different way then i was trying.

I’ll report back somewhere this week.

I’m a little late here, but feel free to check out my simple a* example: http://gogs.shuvit.org:3000/shuvit/astar

Thanks, it’s never to late, i like to dive into more then one example, so i can see what steps are taking to create it.

you should start of by doing some reading.

Dijkstra’s algorithm

A* search algorithm

@edderkop I use A* in my example

I use BPY to get quads (UPBGE has no quads once it starts) and then build my graphs

https://www.redblobgames.com/pathfinding/a-star/implementation.html

I altered this implimentation