Game pathfinding algorithms


















Step through to see the expansion process:. But how do we find the shortest path? Move the cross to see how following the arrows gives you a reverse path back to the start position. The code to reconstruct paths is simple: follow the arrows backwards from the goal to the start. It works not only on grids as shown here but on any sort of graph structure. In a dungeon, graph locations could be rooms and graph edges the doorways between them.

In a platformer, graph locations could be locations and graph edges the possible actions such as move left, move right, jump up, jump down. In general, think of the graph as states and actions that change state. I have more written about map representation here [2]. Drag the around see how the frontier stops expanding as soon as it reaches the goal. There are lots of cool things you can do with early exit conditions. In some pathfinding scenarios there are different costs for different types of movement.

For example in Civilization, moving through plains or desert might cost 1 move-point but moving through forest or hills might cost 5 move-points. In the map at the top of the page, walking through water cost 10 times as much as walking through grass.

Another example is diagonal movement on a grid that costs more than axial movement. How does it differ from Breadth First Search? Less obviously, we may end up visiting a location multiple times, with different costs, so we need to alter the logic a little bit.

Using a priority queue instead of a regular queue changes the way the frontier expands. Contour lines are one way to see this. Start the animation to see how the frontier expands more slowly through the forests, finding the shortest path around the central forest instead of through it:. Movement costs other than 1 allow us to explore more interesting graphs, not only grids.

In the map at the top of the page, movement costs were based on the distance from room to room. Movement costs can also be used to avoid or prefer areas based on proximity to enemies or allies. Additionally, you might want to check out swarming and flocking behaviors to move groups of agents together, or ant colony optimization algorithms to find paths similar to how ants find food in real life. The algorithms we talked about so far are great for situations where you have a complex space with obstacles and multiple possible paths, but there are also many situations where something simpler will suffice.

This is how I moved the agent from one node to another in the above code. This approach combined with some randomness can be very effective for creating pseudo-intelligent behaviors. One more option to consider is using hard-coded paths for your agents.

Plenty of 2D games go with this approach. You can also combine different pathfinding algorithms to create more complex behaviors. Or you could default to a hard-coded path, and then use a swarming algorithm when the player gets close enough. Pathfinding is a huge topic with a ton of different options and algorithms to consider, but hopefully this tutorial introduced a few approaches that will come in handy.

I learned this the hard way! Happy Coding is a community of folks just like you learning about coding. Do you have a comment or question? Post it here! Tutorials p5. Breadth-First Search Instead of just going off in a random direction, we could be a little smarter with our path planning.

Did I find my friend yet? If not, what happens if I turn right from my house? If not, what happens if I turn left and then turn left again? What happens if I turn left and then turn right? What happens if I turn right and then left? What happens if I turn right and then right again? What happens if I turn left, then left, then left again?

Expand the shortest path, which would take us to the E node. The path to B is the shortest unexplored path, so expand to the B node. Expand the path with the shortest length and that gets us closest to Goal , which would take us to the E node. Repeat that process to expand the path to the H node. Graph represents the entire search space. Connection represents a path from one node to another. A Connection has a direction it goes from one node to another node and a cost the distance from the first node to the second node.

Heuristic defines a function that returns the estimated distance between a particular node and the goal node. BitmapFont ; import com. SpriteBatch ; import com. Keep track of it so we don't have to recalculate it later. Line ; shapeRenderer. Connection ; import com.

ShapeRenderer ; import com. Filled ; shapeRenderer. Heuristic ; import com. All of these algorithms have one common goal and that is to find the shortest possible path between two points on the map. But each one goes about doing that in their own different way. It gets the job done as well as you can expect, without causing your game to lag which is pretty common for a lot of algorithms. Each of these blocks represent a potential step that the player or the NPC can take to reach its target.

And lastly, it takes into account the sum of all these values, which is the F value. The way it works is the algorithm takes the next step in the direction which has the lowest F value compared to other blocks or potential steps. It continuously loops through all of its potential steps to calculate which path has the lowest sum, thereby figuring out the shortest possible path to reach the target.

Here is how the algorithm looks when calculating the shortest path to reach from Point A to Point B on the map. Although it looks fairly simple, there is a lot going on behind the scene, especially when there are lots of enemies chasing after you from many different starting points, and with many different obstacles to maneuver around. This is my favorite of the pathfinding algorithms, but maybe you have one that you like better?



0コメント

  • 1000 / 1000