Simple Pathfinding
Pathfinding automatically for NextBots
NextBots use the navmesh to calculate how to get from their current position to their target.
This is done using helper classes like PathFollower and most of the path construction is done internally.
You can however influence where the NextBots can and cannot go when computing the path with PathFollower:Compute by using its 3rd argument.
<!-- Maybe add examples here later -->
Below you will find examples on how to have complete control over the path generation and so that it is not limited to NextBots only.
Terminology
- navmesh - The navigation mesh.
- agent - The agent will use the generated path, for example a player bot or an NPC.
- path - The generated path, a set of nodes the agent must travel through to get to the goal
- node - Points which are connected with each other that signify the positions that can be reached
Pathfinding manually using A* and navmesh library
The CNavArea provides you with methods which allow you to quickly set up a completely custom A* pathfinding algorithm for your own purposes, such as pathfinding for player bots.
These methods are:
All of these are basic functions that are using in A* pathfinding algorithm.
Here's an example on how to use these methods to implement a basic A* algorithm from wikipedia:
In this example, Astar() fucntion takes 2 arguments: The starting CNavArea, for example where the player bot or NPC currently is, and the goal CNavArea, where the bot or NPC wishes to get to.
The Astar() function will return one of the 3 values:
- If it is physically impossible to get to the goal CNavArea, it returns false
- If the start CNavArea is the same as the goal CNavArea, it returns true
- Otherwise it will return an array of CNavAreas which the bot/NPC has to travel through (one after another, the goal CNavArea being the very first entry in the array, use table.Reverse to reverse the array order if necessary) to get to the goal.
Below you will find a few examples on how to use this example function to move player bots using pathfinding to a certain target.
Using vectors as start and goal positions
You can use the function navmesh.GetNearestNavArea to find closest CNavArea to your desired Vector position.
Here's an example function that takes 2 vectors are input for Astar() function:
A* works best with point nodes, not areas
In Garry's Mod, the navigation mesh is made of CNavAreas, not Vector positions. As a result, you will notice that examples here move the agent mostly from a center of one CNavArea to the center of another. This makes the generated paths look unnatural.
In order to achieve more natural paths, more advanced techniques will be needed when building positions the agent will need to traverse through, such as using the corners of CNavAreas, using Line of Sight checks to cut corners or discard certain positions, etc.
Pathfinding is not easy
Please keep in mind that all of these examples are kept as simple as possible for educational purposes. It may not generate the absolutely most efficient paths. More work will be necessary to be put into the agents movement code for the paths to feel natural or be efficient.
Example console command to test and visualize the generated path
This example code contains a function that will draw the generated path, directly from the Astar() function using the debugoverlay.
Please keep in mind that the debugoverlay only works in multiplayer and only when the developer console command is set to a non zero value.
This example shows how you can use the Astar() and drawThePath() functions together to test and debug your path generation.
Example usage with player bots
This example shows the most basic way to move a player bot (an agent) alongside the generated path:
This example will make all bots on the server move towards the first player on the server. The path generated is not pretty, but the point of this example to show how to use the path to move an agent alongside a generated path.