- Home /
Custom pathfinding and node-values
Hi! I'm working on a 2D platformer with a custom physics system. I use a grid-based layout for my map (procedural generation) and store all values in a two-dimensional integer (int[,]).
I've started on an AI, and I got walking, climbing and jumping already (if the AI walks on a linear path along teh x-axis he will know how to climb and jump to get to the destination on the x-axis).
So I've started on pathfinding. I started by copying my values from my int[,] and depending on if it is a solid or empty block I define different penalties.
I loop through all coordinates and assign a score based on this formula:
float score = penalty[i,u] + (new Vector3(i,u,0) - destination).sqrMagnitude + (new Vector3(i,u,0) - transform.position).sqrMagnitude;
The problem here is that when I order the List of nodes based on their score (nodes = nodes.OrderBy(x => x.score).ToList();), the node between my starting position and destination will always have the lowest score, and the nodes next to this node will have second to lowest, etc. So I end up with my AI going back and forth, never really reaching his destination.
How can I improve this formula or sorting of the list, so that the nodes will come in order depending on score and how close they are to transform.position? Is it possible to sort with two variables in Linq?
Answer by Salmjak · Jul 13, 2015 at 06:30 PM
Kind of solved this by doing more of an A* approach, checking neighbours of a tile and their distance to the destination (+ penalty) and then choosing the cheapest neighbour. Add that neighbour to a list of nodes, and check it's neighbours. Then I loop this until the node-position is at the destination.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Pathfinding through pairs of connections 2 Answers
C#, LINQ and Lists - help with 1 line of code? 1 Answer
A* pathfinding - incorrect nodes calculated 1 Answer
Sort List by string field 1 Answer