It was my mistake. I forgot to send rbInfo
to Ammo.btRigidBody()
like this:
const rbInfo = new Ammo.btRigidBodyConstructionInfo(mass, motionState, colShape, localInertia);
const body = new Ammo.btRigidBody(rbInfo);
Playground: Plunker - Ammo.js as ES6-module. Falling Box
Thank you very much for your solution which makes it possible to use Ammo.js with Rollup.
This is my example ammo-es-module-rollup-js.zip (3.9 KB) (ammo.js: ammo-es.zip (434.0 KB)) which outputs gravity to the console using Rollup as the bundler. These packages must be installed globally: npm i -g rollup uglify-js
Use these commands:
npm run dev
is for development. Rollup will watch for file changes. You can set breakpoints in the browser
npm run release
is for release. Rollup will create a bundle. Uglify will compress it.
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- Since import maps are not yet supported by all browsers, it is
necessary to add the polyfill es-module-shims.js
Source: https://threejs.org/docs/index.html#manual/en/introduction/Installation
-->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"ammo-es": "https://dl.dropboxusercontent.com/s/1os9vvoo1pa3ajk/ammo-es.js"
}
}
</script>
<script type="module" src="./js/bundle.js"></script>
</body>
</html>
src/main.js
import AmmoLib from "ammo-es";
let Ammo, physicsWorld;
function setupPhysicsWorld() {
const collisionConfiguration = new Ammo.btDefaultCollisionConfiguration();
const dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration);
const overlappingPairCache = new Ammo.btDbvtBroadphase();
const solver = new Ammo.btSequentialImpulseConstraintSolver();
physicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
physicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));
}
function animationLoop() {
const gravity = new Ammo.btVector3(0, -10, 0);
console.log(`Gravity: (${gravity.x()}, ${gravity.y()}, ${gravity.z()})`);
// requestAnimationFrame(animationLoop);
}
function init() {
AmmoLib().then((re) => {
Ammo = re;
setupPhysicsWorld();
animationLoop();
});
}
init();
rollup.config.js
export default {
input: "./src/main.js",
output: {
file: "public/js/bundle.js"
}
}
package.json
{
"name": "ammo-es-module-rollup-js",
"version": "1.0.0",
"description": "",
"main": "rollup.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "rollup -cwm",
"del-bundle-map": "del /f /q /s .\\public\\js\\bundle.js.map",
"bundle": "rollup -cm",
"minify": "uglifyjs public/js/bundle.js -o public/js/bundle.js",
"release": "npm run bundle && npm run minify && npm run del-bundle-map"
},
"keywords": [],
"author": "",
"license": "ISC"
}