Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Set Up Ammo.js with Browserify (Ammo.js is a port of Bullet Physics Engine to JS)

Set Up Ammo.js with Browserify (Ammo.js is a port of Bullet Physics Engine to JS)

8Observer8
8Observer8
My Instructions for beginners · · 1 min read
6,642 0

I found a solution how to use Ammo.js with Browserify:

npm install kripken/ammo.js

Install Browserify and UglifyJS globally:

npm i browserify uglify-js -g

Make this project structure:

public/index.html
public/js
src/client
src/server

Copy this example to src/client/main.js

const Ammo = require("ammo.js");

let physicsWorld = null;

function main()
{
    Ammo().then(start);

    function start(Ammo)
    {
        console.log("start");
        setupPhysicsWorld(Ammo);
        console.log("gravity =", physicsWorld.getGravity().y());
    }

    function setupPhysicsWorld(Ammo)
    {
        let collisionConfiguration = new Ammo.btDefaultCollisionConfiguration(),
            dispatcher = new Ammo.btCollisionDispatcher(collisionConfiguration),
            overlappingPairCache = new Ammo.btDbvtBroadphase(),
            solver = new Ammo.btSequentialImpulseConstraintSolver();

        physicsWorld = new Ammo.btDiscreteDynamicsWorld(dispatcher, overlappingPairCache, solver, collisionConfiguration);
        physicsWorld.setGravity(new Ammo.btVector3(0, -10, 0));
    }
}

window.onload = main();

Add this commands in package.json for release and debug modes:

  "scripts": {
    "clear": "del /f /q /s .\\public\\js\\*.*",
    "del-bundle": "del /f /q /s .\\src\\bundle.js",
    "bundle-debug": "browserify --debug src/client/main.js -o public/js/bundle.js",
    "bundle-release": "browserify src/client/main.js -o src/client/bundle.js",
    "uglify": "uglifyjs src/client/bundle.js -o public/js/bundle.min.js",
    "debug": "npm run bundle-debug",
    "release": "npm run clear && npm run bundle-release && npm run uglify && npm run del-bundle"
  },

You can build debug and release:

npm run debug
npm run release

bundle.js and bundle.min.js will be generated. Comment/Uncomment recently lines in index.html for debug and release:


Discussion

Loading comments...