Skip to content

Latest commit

 

History

History
132 lines (96 loc) · 5.3 KB

GETTING_STARTED.md

File metadata and controls

132 lines (96 loc) · 5.3 KB

What is this all about

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, ...)

Getting started

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 );
    })

  })
})

Speed considerations

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.

Saving

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
    });
  }));
});

Loading

var recast = new Recast();
recast.OBJLoader('nav_test.obj', function(){

    recast.loadTileMesh('./navmesh.bin', recast.cb(function(){
      // recast is now ready to use
    }));
});

API

new Recast() Create a new instance

General

Basic navmesh operations

Basic agents operations

Crowd

Temporary obstacles

Off-mesh connections

Zones

Troubleshooting

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.