Compute path between two points - Terasology/TutorialPathfinding GitHub Wiki
The pathfinder class provides a nice interface to calculate paths based on hierarchical pathfinding.
You will now learn how to find the path between two points in the terrain.
Open up PathTestingSystem.java located in systems package of TutorialPathfinding. Navigate to endPath(Activated event, Entityref player).
@ReceiveEvent(components = {PathEndComponent.class})
public void endPath(ActivateEvent event, EntityRef entityRef) {
// logger.error(entityRef.toFullDescription());
Vector3f endPathPosition = event.getInstigatorLocation();
logger.error("path end at {}", (endPathPosition).toString());
logger.error("path start at {}", (startPathPosition).toString());
if (pathfinderSystem == null) {
setup();
logger.error("Pathfinder system is null here");
}
if (pathfinderSystem == null) {
logger.error("pathfinder is null again");
}
//code goes here
}
}startPathPosition here stores the location at which you activated item 1. endPathPosition stores the location at which you activated item 2. We will now compute the path between these two points.
You will first have to create a pathfinder object in order to use pathfinding functions. Add this line to your code
Pathfinder pathfinder = new Pathfinder(navGraphSystem, new LineOfSight2d());`The Pathfinder constructor would require a navGraphSystem and a lineOfSight object. (We'll come back to this later, but it basically allows to have paths that are not just zigzag).
The next step would be to find the Walkable Blocks at the start and end positions. We can do that once the pathfinderSystem system is set up by calling a pathfinderSystem.getBlock(location).
Add these two lines to the function.
WalkableBlock startwalkableBlock = pathfinderSystem.getBlock(startPathPosition);
WalkableBlock endWalkableBlock = pathfinderSystem.getBlock(endPathPosition);The final step would be to just compute it. Add these line.
Path path = pathfinder.findPath(endWalkableBlock, startwalkableBlock);
logger.info("Required path is {}", path.toString());A path is essentially a list of nodes, and since we have passed in a lineOfSight2D object into the pathfinder, it will return only the turning points. So, if you moved from one point in the node to the next in a straight line, that would be the required path. So congrats on making your first path!
There exists a nice decoupled path highlighting package within TutorialPathfinding that will allow you to easily highlight paths. However, the system accepts an ArrayList of Vector3fs instead of an ArrayList of nodes which is returned in findPath(). Luckily, there is a helper function that lets you do exactly that.
ArrayList<Vector3f> pathBlockPositions = convertPathToVectors(path);
pathBlockPositions.add(0, endWalkableBlock);Now call a HighlightPathEvent with pathBlockPositions.
`event.getInstigator().send(new HighlightPathEvent(pathBlockPositions));`At this point, you should be able to see the path that you just created in the world.