Navigation Mesh and crowd Agents

This is great, thank @Cedric. I’m now using it this way:

import { RecastJSPlugin } from '@babylonjs/core/Navigation/Plugins/recastJSPlugin'
import Recast from 'recast-detour'
const navigationPlugin = new RecastJSPlugin(Recast)

Just 2 small improvements maybe:

  1. You could add the repository field to your package.json, that will let npm automatically link the module to the repo in their interface.
  2. After I import Recast like this, I’m getting a new warning. Everything works though.
    In Chrome: recast.js:2356 - Invalid asm.js: Undefined global variable
    In Firefox: recast.js:2356 - TypeError: asm.js type error: 'undefined' not found in local or asm.js module scope

Let me know if I can be of any help on that warning, not sure where to start tbh.

Congrats on your first npm package :wink:.

1 Like

The repo only contains the source and not the compilation result recast.js. Is it a problem if I add it anyway to the package?

If I’m not wrong it’s just a piece of information with no behavior attached so that’s fine. Double checked at npm-package.json | npm Documentation and it seems it’s just that yes. Note that you can put in the directory as well since you’re in a monorepo setup (last part of the link above).

1 Like

Sorry for replay after few days, but I was off. There is PG with my problem. Blue boxes are enemies and red is player. They are fighting. For some reason new enemy called enemy4 want to attack player, but it cannot attack from the front, because others enemies (1,2,3) are there. On the right side is one enemy (5), so last called enemy4 should go to left side and attack player. I try to tuning parameters to achieve constant speed for player and enemies. Also I want to avoid slowly decreasing speed where box is near target point.

Hi @FurcaTomasz

In your playground, the enemy 4 goes on the left to attack player. The path is shorter to the player. Do you get another behavior?

The slow decrease in speed when the enemy is near the player might be caused by the collision response between enemy 4 and the player. You ask the enemy to go where the player is. So he’ll try to take his position. Try to compute a destination position around the player within a radius corresponding to the sum of enemy radius and player radius. So the enemy will stop before reaching him. Then, increase the maxAcceleration value so the acceleration and deceleration curves will be steeper.

I just reproduct on PG my backend validation of movement. It looks very good now.

Is crowd have method to stop goTo action? I want to fire it, when enemy intersect with player mesh.

No, there is nothing to stop an agent. But you can call goto with the current agent position.

1 Like

Yeah! This is that! Maybe someone need similar code:

Thanks a lot!

1 Like

Yo @Cedric

Did you ever add support to use an already baked navmesh.

I’m looking to update the unity exporter to use recastJsPlugin with the nav mesh baked in unity and exported as a GLTF mesh

Can you add support for existing meshes… pretty please :pleading_face:

Not yet. I have some time next week. I’ll try to do something.

1 Like

Thanks bro :slight_smile:

Yo @Cedric … Can you please add rotation support so that the agent is rotated towards the direction he is heading. When using animated characters as the transform node (model parent) it looks funny when the character is not rotated towards the direction of travel.

Can you please look at that as well :pleading_face:

recast doesn’t handle orientation. But it’s possible to handle it on your side. The orientation vector is the velocity when its length is > 0. otherwise, it’s unchanged.

Could show me a quick sample to set the orientation/rotation from the agent velocity…, please

check lines [32 … 44]
agent direction is interpolated to reach the velocity vector. That direction vector is then used to compute an orientation angle (lines 46 - 48)


Thanks again @Cedric … rotation code works great :+1:

1 Like


It’s not possible to use an arbitrary mesh with recast. There are constraints that basically force to have a navmesh built as a top down projection. But I’m adding the ability to load a binary representation of the navmesh. No construction needed then.

What’s your pipeline for building a navmesh? Can you export a binary file from recast in Unity? I guess it’s the same binary representation and I’d like to test it if you can send me an export.

What do mean binary representation of a navmesh… what file format… ???

Right now I simply bake the unity navmesh to GLTF or GLB file… and I was using the Babylon Navigation-Mesh extension and passing the native babylon mesh to it

There are no other type of export?

Nope just GLTF export.
What binary format are you thinking of ???