In a game context, Recast.js is meant to be used with a file describing a scene geometry. Using the recastnavigation library, it will deduce a navigation mesh - a set of polygons on which your characters can move.
Once this navigation mesh is computed, it is possible to use built-in pathfinding operations like "find the shortest path between point A and point B", taking into account various user-defined variables like obstacles, slopes, and off-mesh connections.
If you decide to use it to animate your scene characters, it also provides a complete crowd system capable of managing all your characters movements using per-characters settings (speed, radius, ...)
Assuming you've got an .obj file describing your scene geometry, you can get a Recast instance with:
var recast = new Recast();
recast.OBJLoader( 'path/to/geometry.obj', function () {
// get a random navigable point A
recast.getRandomPoint( function (x, y, z) {
// find the shortest route from origin to point A
// we asume 0,0,0 is a navigable point
recast.findPath( 0, 0, 0, x, y, z, function ( route ) {
console.log( "The shortest route contains", route.length, "corners" );
console.log( "These corners are", route );
})
})
})
Computing the navigation mesh from your level can be quite cpu-intensive. When this happens, a solution is to store the computed navmesh for later use.
var recast = new Recast();
recast.OBJLoader('nav_test.obj', function(){
recast.buildTiled(); // only tiled mesh is supported right now
recast.saveTileMesh('./navmesh.bin', recast.cb(function (error, blob) {
var buffer = new Buffer(blob.length);
for (var i = 0; i < blob.length; i++) {
buffer.writeUInt8(blob[i], i);
}
fs.writeFile('./navmesh.bin', buffer, function (err) {
// done
});
}));
});
var recast = new Recast();
recast.OBJLoader('nav_test.obj', function(){
recast.loadTileMesh('./navmesh.bin', recast.cb(function(){
// recast is now ready to use
}));
});
new Recast()
Create a new instance
- recast.settings - Apply new navigation mesh settings
- recast.OBJLoader - Load an
.obj
file - recast.OBJDataLoader - Load an
.obj
content - recast.buildSolo - Build a single-object navigation mesh
- recast.buildTiled - Build a tiled navigation mesh
- recast.saveTileMesh - Store a tiled navigation mesh
- recast.loadTileMesh - Load a tiled navigation mesh
- recast.saveTileCache - Store a tiled navigation tilecache
- recast.loadTileCache - Load a tiled navigation tilecache
- recast.getRandomPoint - Get a random navigable position
- recast.findNearestPoint - Find the nearest navigable point
- recast.findNearestPoly - Find the nearest navigable polygon
- recast.queryPolygons - Find nearest polygons
- recast.setPolyFlags - Set flags on nearest polygons
- recast.setPolyFlagsByRef - Set flags on a single polygon
- recast.findPath - Find a path between two points
- recast.addAgent - Add an agent
- recast.updateCrowdAgentParameters - Add an agent settings
- recast.removeCrowdAgent - Remove an agent
- recast.initCrowd - Initialize the crowd system
- recast.crowdRequestMoveTarget - Set an agent's destination
- recast.crowdUpdate - Update the crowd system
- recast.crowdGetActiveAgents - Retrieve all crowd agents
- recast.requestMoveVelocity - Set an agent's velocity
- recast.addTempObstacle - Add a temporary obstacle
- recast.removeTempObstacle - Remove a temporary obstacle
- recast.getAllTempObstacles - Retrieve all temporary obstacles
- recast.clearAllTempObstacles - Remove all temporary obstacles
- recast.addOffMeshConnection - Add an off-mesh connection
- recast.Zone - Create a new zone
- zone.isWalkable - Test if a zone is walkable
- zone.is - Test a specific flag
- zone.setFlags - Set specified flags
- zone.clearFlags - Clear specified flags
- zone.toggleFlags - Toggle specified flags
Some tips to help you getting rid of well-known traps
- test your geometry in Recast - it is easily runnable on Windows, MacOS and Linux
- adjust settings wisely. Test them in Recast if you are not sure.