Game Development Reference
In-Depth Information
How to do it...
We start by defining the class that will generate the paths for us. This part will be imple-
mented by performing the following steps:
1. We create a new class called PathfinderThread , which extends the Thread
class.
2. It needs a couple of fields, a Vector3f called target , a
NavMeshPathfinder called pathfinder , and two Booleans, pathfind-
ing and running , where running should be set to true by default.
3. The constructor should take a NavMesh object as input, and we instantiate the
pathfinder with the same, as shown in the following code:
public PathfinderThread(NavMesh navMesh) {
pathfinder = new NavMeshPathfinder(navMesh);
this.setDaemon(true);
}
4. We override the run method to handle pathfinding . While running is true ,
the following logic should apply:
if (target != null) {
pathfinding = true;
pathfinder.setPosition(getSpatial().getWorldTranslation());
boolean success = pathfinder.computePath(target);
if (success) {
target = null;
}
pathfinding = false;
}
5. If target is not null , we set pathfinding to true .
6. Then we set the start position of the pathfinder to the AI's current position, as
shown in the following code:
pathfinder.setPosition(getSpatial().getWorldTranslation());
7. If the pathfinder can find a path, we set target to null .
8. In either case, pathfinding is done, and pathfinding is set to false .
Search WWH ::




Custom Search