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

Published December 21, 2020
Advertisement

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:

<script src="js/bundle.js"></script>

<!-- <script src="js/bundle.min.js"></script> -->

1 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement