Bug with cannonJSPlugin.js in node_modules

Hello all I am using BabylonJS with typescript. I am trying to implement physics in my scene by using CannonJS. I got the plugin working and I have my dependencies installed with npm install --save cannon and npm install --save-dev @types/cannon. The physics seems to be working if i test with a sphere and ground. However, if i try to use a heightmap i get the below error. I have:

import { CannonJSPlugin } from '@babylonjs/core/Physics/Plugins/CannonJSPlugin';
import { PhysicsImpostor } from '@babylonjs/core/Physics';
import * as CANNON from 'cannon'; 

at the top of my typescript file and:

    let gravityVector = new Vector3(0,-9.82, 0);
    let physicsPlugin = new CannonJSPlugin(undefined, undefined, CANNON);
    this.scene.enablePhysics(gravityVector, physicsPlugin);

    let sphere = Mesh.CreateSphere("sphere1", 16, 10, this.scene);
    sphere.position = new Vector3(0, 100, 100);
    sphere.physicsImpostor = new PhysicsImpostor(sphere, PhysicsImpostor.SphereImpostor, { mass: 1, restitution: 0.7 }, this.scene);

    let terrainMaterial = new StandardMaterial("terrain", this.scene);
    terrainMaterial.diffuseTexture = new Texture("assets/babylon/textures/grass.jpg", this.scene);
    let terrain = Mesh.CreateGroundFromHeightMap("terrain", "assets/babylon/heightmap.jpg", 5000, 5000, 50, 0, 200, this.scene, false);
    terrain.position = new Vector3(0, -100, 0);
    terrain.material = terrainMaterial; 
    terrain.physicsImpostor = new PhysicsImpostor(terrain, PhysicsImpostor.HeightmapImpostor, { mass: 0, restitution: 0.9 }, this.scene);

but I am getting this error in the console:

core.js:5980 ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of null
    TypeError: Cannot read property 'length' of null
        at CannonJSPlugin.push.VxYV.CannonJSPlugin._createHeightmap (CannonJSPlugin.js:356)
        at CannonJSPlugin.push.VxYV.CannonJSPlugin._createShape (CannonJSPlugin.js:335)
        at CannonJSPlugin.push.VxYV.CannonJSPlugin.generatePhysicsBody (CannonJSPlugin.js:94)
        at PhysicsEngine.push.3kgD.PhysicsEngine.addImpostor (physicsEngine.js:102)
        at PhysicsImpostor.push.LVN+.PhysicsImpostor._init (physicsImpostor.js:393)
        at new PhysicsImpostor (physicsImpostor.js:190)
        at TestBabylonComponent.physics (test-babylon.component.ts:119)
        at TestBabylonComponent.<anonymous> (test-babylon.component.ts:71)
        at Generator.next (<anonymous>)
        at fulfilled (tslib.es6.js:73)
        at resolvePromise (zone-evergreen.js:798)
        at zone-evergreen.js:705
        at fulfilled (tslib.es6.js:73)
        at ZoneDelegate.invoke (zone-evergreen.js:364)
        at Object.onInvoke (core.js:28301)
        at ZoneDelegate.invoke (zone-evergreen.js:363)
        at Zone.run (zone-evergreen.js:123)
        at zone-evergreen.js:857
        at ZoneDelegate.invokeTask (zone-evergreen.js:399)
        at Object.onInvokeTask (core.js:28289)

being caused by this function in cannonJSPlugin.js in the node_modules:

CannonJSPlugin.prototype._createHeightmap = function (object, pointDepth) {
        var pos = object.getVerticesData(VertexBuffer.PositionKind);
        var transform = object.computeWorldMatrix(true);

        // convert rawVerts to object space
        var temp = new Array();
        var index;
        for (index = 0; index < pos.length; index += 3) {
            Vector3.TransformCoordinates(Vector3.FromArray(pos, index), transform).toArray(temp, index)    ;
            }
...

Adding @Cedric or @RaananW

Hi,

this seems to be related to the mesh you are using. What mesh are you using as the base of the impostor? Will you be able to reproduce that in the playground?

Here you go. Its also happening on the Playground: Babylon.js Playground

Ground is created asynchronous. Which means you will need to wait for the ground mesh to be ready before inflicting an imperious on it. The create grind mesh function has an onSuccess call back. This would be the right place to construct the new impostor

I’ll check that out tomorrow. Thanks :smile: Totally forgot about async even tho I’m accounting for it with other mesh imports lol

1 Like