NullEngine + Server-side physics issue - am I doing it right?

Hello,

Babylon.js 4.1.0-beta-20
Node.js 12.4.1
NPM 6.13.6
Ubuntu 18.04

I’m using the NullEngine docs as the starting point.

I’ve modified that code for illustrative purposes and to be a little more friendly if you’re tinkering around inside it with the Node Inspector :slight_smile:

'use strict';

const BABYLON = require('babylonjs');
const LOADERS = require('babylonjs-loaders');
const {
  NullEngine,
  Scene,
  PointLight,
  Vector3,
  ArcRotateCamera,
  OimoJSPlugin,
  AmmoJSPlugin,
  CannonJSPlugin,
  SceneLoader: {
    ImportMeshAsync
  }
} = BABYLON;

global.XMLHttpRequest = require('xhr2').XMLHttpRequest;

(async () => {
  const engine = new NullEngine();
  const scene = new Scene(engine);
  const light = new PointLight("Omni", new Vector3(20, 20, 100), scene);
  const camera = new ArcRotateCamera("Camera", 0, 0.8, 100, BABYLON.Vector3.Zero(), scene);
  let result = null;
  let meshes = null;
  let physicsEngine = null;

  try { physicsEngine = new OimoJSPlugin() } catch (e) { console.log(e) }
  try { physicsEngine = new AmmoJSPlugin() } catch (e) { console.log(e) }
  try { physicsEngine = new CannonJSPlugin() } catch (e) { console.log(e) }

  if (physicsEngine) {
    scene.enablePhysics(undefined, physicsEngine);
  }

  try {
    result = await ImportMeshAsync("", "https://playground.babylonjs.com/scenes/", "skull.babylon", scene);
    meshes = result.meshes;
    camera.target = meshes[0];
    console.log("Meshes loaded from babylon file: " + meshes.length);
    for (const mesh of meshes) {
      console.log(mesh.toString());
    }
  } catch (e) {
    console.log(e);
  }

  try {
    result = await ImportMeshAsync("", "https://www.babylonjs.com/Assets/DamagedHelmet/glTF/", "DamagedHelmet.gltf", scene);
    meshes = result.meshes;
    console.log("Meshes loaded from gltf file: " + meshes.length);
    for (const mesh of meshes) {
      console.log(mesh.toString());
    }
  } catch (e) {
    console.log(e);
  }

  console.log("render started");
  engine.runRenderLoop(scene.render.bind(scene));
})();

What I’m noticing is that I get the following errors when I attempt to call the constructors for the physics engines:

$ node main.js

BJS - [15:05:11]: Babylon.js v4.1.0-beta.20 - Null engine
ReferenceError: OIMO is not defined
    at new e (/home/peter/Development/bjs-nullengine-demo/node_modules/babylonjs/babylon.js:16:1872650)
    at /home/peter/Development/bjs-nullengine-demo/main.js:30:25
    at Object.<anonymous> (/home/peter/Development/bjs-nullengine-demo/main.js:63:3)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
ReferenceError: Ammo is not defined
    at new e (/home/peter/Development/bjs-nullengine-demo/node_modules/babylonjs/babylon.js:16:1887238)
    at /home/peter/Development/bjs-nullengine-demo/main.js:31:25
    at Object.<anonymous> (/home/peter/Development/bjs-nullengine-demo/main.js:63:3)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
ReferenceError: CANNON is not defined
    at new e (/home/peter/Development/bjs-nullengine-demo/node_modules/babylonjs/babylon.js:16:1858332)
    at /home/peter/Development/bjs-nullengine-demo/main.js:32:25
    at Object.<anonymous> (/home/peter/Development/bjs-nullengine-demo/main.js:63:3)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
    at internal/main/run_main_module.js:17:11
Meshes loaded from babylon file: 1
Name: test, isInstance: YES, # of submeshes: 1, n vertices: 41864, parent: NONE
Meshes loaded from gltf file: 2
Name: __root__, isInstance: YES, # of submeshes: 0, n vertices: 0, parent: NONE
Name: node_damagedHelmet_-6514, isInstance: YES, # of submeshes: 1, n vertices: 14359, parent: __root__
render started

I haven’t been able to find much literature on the subject of using the physics engines server-side.

Am I doing it right? I expect to see one of the physics engines load, however none of the three I include in the code above seem to function correctly under a Node.js environment.

I threw together a project, which can be cloned from here.

I’m not sure about Oimo as I’ve never used it, but Cannon and Ammo require that you download their respective JS files (you can find the links for them here) and load them globally. On the client side before loading my engine I’ll do a either:

window.Ammo = require('./lib/ammo.js');
// Or
window.CANNON = require('./lib/cannon.js');

I think you can achieve the same thing with process.Ammo or similar with Node.

2 Likes

That was it, thanks!

I’ve updated the repo with a working example.

Seems Ammo puts itself into the global namespace already, but I matched its instantiation to the others’ instantiation method in my source example for the sake of consistency.

Thanks again, @DisownedWheat !