So I am trying to get Ammo.js to work with BABYLON inside NodeJS using import statements, I have already installed babylonjs and ammo.js using npm, although I don’t know how to use import/require statements to get Babylon to register that Ammojs is there, here is the small example I have right now:
import * as BABYLON from 'babylonjs';
var canvas = document.getElementById("renderCanvas");
var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
var scene = new BABYLON.Scene(engine);
scene.enablePhysics(new BABYLON.Vector3(20,0,0),new BABYLON.AmmoJSPlugin())
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
camera.attachControl(canvas, true);
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.7;
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
new BABYLON.PhysicsImpostor(sphere,BABYLON.PhysicsImpostor.MeshImpostor,{ mass: 1, friction: 1.0, restitution: 0 },scene)
sphere.position.y = 1;
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 6, height: 6}, scene);
engine.runRenderLoop(function () {
if (scene) {
scene.render();
}
});
But when I build it using webpack and then run the index.html I get the error “Uncaught ReferenceError: Ammo is not defined”
Good news everyone, I have figured out the answer, even though I’m pretty sure no one will ever need this information the problem was that the ammo.js npm module is missing like half of the api for some reason (fantastic!) but after instead doing npm install kripken/ammo.js it installed the complete version and then after modifying the code because I realized i need to pass the import to the plugin in worked great.
import * as BABYLON from 'babylonjs';
import * as Ammo from 'ammo.js';
var canvas = document.getElementById("renderCanvas");
var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
var scene = new BABYLON.Scene(engine);
scene.enablePhysics(new BABYLON.Vector3(20,0,0),new BABYLON.AmmoJSPlugin(true,Ammo))
var camera = new BABYLON.FreeCamera("camera1", new BABYLON.Vector3(0, 5, -10), scene);
camera.setTarget(BABYLON.Vector3.Zero());
camera.attachControl(canvas, true);
var light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), scene);
light.intensity = 0.7;
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);
new BABYLON.PhysicsImpostor(sphere,BABYLON.PhysicsImpostor.MeshImpostor,{ mass: 1, friction: 1.0, restitution: 0 },scene)
sphere.position.y = 1;
var ground = BABYLON.MeshBuilder.CreateGround("ground", {width: 6, height: 6}, scene);
engine.runRenderLoop(function () {
if (scene) {
scene.render();
}
});
Weird… it was working a couple days ago, today I fired it up with no luck, now my browser console says ‘apply’ could not be found from ammo.js. I’ve setup a file to test JUST ammo.js:
and now it gives me this on line 7 where I assign the Ammo plugin:
So confused. I swear I’ve changed nothing since I got this running only a few days ago. Any idea what is causing this?
Thanks! That confirms my suspicion that it was a change made to Kripken’s ammo lib on 11th Apr. I was seriously tearing my hair out on this one blaming my implementation. I’ve opened an issue on the git repo, Kripken has replied ^^.