Game Development Reference
In-Depth Information
9. Finally, we tell the thread to sleep for one second until trying again, as shown in
the following code:
Thread.sleep(1000);
That's the first step of the pathfinding handling. Next, we'll define a class that will use
this. This will be implemented by performing the following steps:
1. We create a new class that extends
AbstractControl
called
NavMeshNav-
igationControl
.
2. It needs two fields, a
PathfinderThread
called
pathfinderThread
and
a
Vector3f
called
waypointPosition
.
3. Its constructor should take a node as input, and we use this to extract a
NavMesh
from, and pass it on to
pathfinderThread
, which is instantiated in the con-
structor as follows:
public NavMeshNavigationControl(Node world) {
Mesh mesh = ((Geometry)
world.getChild("NavMesh")).getMesh();
NavMesh navMesh = new NavMesh(mesh);
pathfinderThread = new PathfinderThread(navMesh);
pathfinderThread.start();
}
4. Now, we create a method to pass a position it should pathfind to using the follow-
ing code:
public void moveTo(Vector3f target) {
pathfinderThread.setTarget(target);
}
5. The
controlUpdate
method is what does the bulk of the work.
6. We start by checking whether
waypointPosition
is
null
.
7. If it is not null, we project
waypointPosition
and the spatials
worldTranslation
onto a 2D plane (by removing the
y
value), to see how
far apart they are as follows:
Vector2f aiPosition = new Vector2f(spatialPosition.x,
spatialPosition.z);