Navigation and multiple obstacles lead to lost references

Hi,

Many thanks for the great JS port of recast navigation. I’ve been exploring the examples and attempted to add multiple obstacles to the docs example “Adding a door to a navigation mesh”:

https://playground.babylonjs.com/#WCSDE1

Adding multiple obstacles works wonders, but you aren’t able to remove a specific obstacle later. The reason seems to be that creating multiple obstacles returns the same pointer.

It’s easy to reproduce this bug with the following code:

var obstacle = null;
var obstacle2 = null;
var obstacleMesh;
var toggleObstacle = function(scene, navigationPlugin) {
    if (!obstacle)
    {
        obstacle = navigationPlugin.addCylinderObstacle(new BABYLON.Vector3(0,-1,0), 1, 2);
        obstacle2 = navigationPlugin.addCylinderObstacle(new BABYLON.Vector3(2,-1,0), 1, 2);
        obstacleMesh = BABYLON.MeshBuilder.CreateBox("cube", { size: 1 }, scene);
        obstacleMesh.scaling.x = 0.5;
        obstacleMesh.scaling.z = 2;
        var matObstacle = new BABYLON.StandardMaterial('matObstacle', scene);
        matObstacle.diffuseColor = new BABYLON.Color3(0.7,0.3,0.3);
        obstacleMesh.material = matObstacle;
        obstacleMesh.position = new BABYLON.Vector3(0, 0.5, 0);
    } else {
        console.log(obstacle, obstacle2);
        navigationPlugin.removeObstacle(obstacle);
        obstacleMesh.dispose();
        obstacle = null;
    }

In the console both obstacle and obstacle2 hold the same pointer:

dtObstacleRef {ptr: 23572} dtObstacleRef {ptr: 23572}

Attempting to remove the first obstacle will result in removing the second one. The pointer for the first obstacle has been lost somehow. I’ve been studying the recastjs plugin on how to resolve this, without success so far.

1 Like

Welcome aboard!

Adding @Cedric who is the creator of the navmesh plugin.

1 Like

Let me take a look …

1 Like

I’ve found the issue and a PR with updated recast JS is here : update recast JS with proper pointer from obstacle creation by CedricGuillemet · Pull Request #10090 · BabylonJS/Babylon.js · GitHub
npm update to follow

3 Likes

Many thanks @Cedric for the prompt reply. I’ve tested your PR in my custom recast build and the issue has been resolved, all obstacles get their correct pointer reference.

1 Like

By the way, on a side note the walkableSlopeAngle parameter doesn’t seem to be implemented in the recastjs.cpp solution. You can easily test this in any of the Babylon.js PGs, changing the setting doesn’t affect the resulting navmesh.

I think what’s missing is the following recast method, taken from the original cpp repo:

Ho! Thanks! I’m taking a look.

1 Like