Example code to give you an idea of the API:
Note: ammo.js has just been updated to a new porting approach. If you find some part of the Bullet API that is not supported that you need, please see https://github.com/kripken/ammo.js/issues/60
‘ammo’ stands for “Avoided Making My Own js physics engine by compiling bullet from C++” ;)
ammo.js is zlib licensed, just like Bullet.
Discussion takes place on IRC at #emscripten on Mozilla’s server (irc.mozilla.org)
builds/ammo.js contains a prebuilt version of ammo.js. This is probably what you want.
You can also build ammo.js yourself.
The most straightforward thing is if you want to write your code in C++, and run that on the web. If so, then you can build your C++ code with emscripten normally and either build and link Bullet using
or you can use Bullet directly from emscripten-ports, with
In both cases, you don’t need ammo.js, just plain Bullet.
ammo.js autogenerates its API from the Bullet source code, so it should be basically identical. There are however some differences and things to be aware of:
See https://github.com/kripken/emscripten/wiki/WebIDL-Binder for a description of the bindings tool we use here, which includes instructions for how to use the wrapped objects.
All ammo.js elements should be accessed through
Ammo.*. For example,
Ammo.btVector3, etc., as you can see in the example code.
Member variables of structs and classes can be accessed through
setter and getter functions, that are prefixed with
m_rayToWorld from say a
however their performance is potentially problematic.
Functions returning or getting
btScalar& are converted to
float. The reason is that
float& is basically
float* with nicer syntax
time you call such a function, making usage very ugly. With this change,
you can do
|new btVector3(5, 6, 7)| and it will work as expected. If
you find a case where you need the float& method, please file an issue.
Not all classes are exposed, as only what is described in ammo.idl is wrapped. Please submit pull requests with extra stuff that you need and add.
There is experimental support for binding operator functions. The following might work:
|Operator||Name in JS|
For more information about setting up Emscripten, see the getting started guide.
To configure and build ammo into the
builds directory, run the following:
$ cmake -B builds $ cmake --build builds
There are also some key options that can be specified during cmake configuration, for example:
$ cmake -B builds -DCLOSURE=1 # compile with closure $ cmake -B builds -DTOTAL_MEMORY=268435456 # allocate a 256MB heap $ cmake -B builds -DALLOW_MEMORY_GROWTH=1 # enable a resizable heap
On windows, you can build using cmake’s mingw generator:
> cmake -B builds -G 'MinGW Makefiles' > cmake --build builds
Note that if you have not installed emscripten via the emsdk, you can configure
its location with
ammo.js can also be built with Docker. This offers many advantages (keeping its native environment clean, portability, etc.). To do this, you just have to install Docker and run:
$ docker-compose build # to create the Docker image $ docker-compose up # to create the Docker container and build ammo.js $ docker-compose run builder # to build again the ammojs targets after any modification
If you want to add arguments to cmake, you have to edit the
The size of the ammo.js builds can be reduced in several ways:
Removing uneeded interfaces from ammo.idl. Some good examples of this are
DebugDrawer, which are both only needed if visual debug rendering is desired.
Removing methods from the
-s EXPORTED_RUNTIME_METHODS= argument in make.py. For example,
UTF8ToString is only needed if printable error messages are desired from
You can run the automatic tests with
$ npm run test-js # --> AMMO_PATH=builds/ammo.js ava $ npm run test-wasm # --> AMMO_PATH=builds/ammo.wasm.js ava
It’s also possible to run ava directly for more options:
$ npx ava --verbose $ npx ava --node-arguments inspect
AMMO_PATH is defined,
builds/ammo.js is tested by default.
http-server is included as a dev
dependency as an easy way to run the examples. Make sure to serve everything
from the repo root so that the examples can find ammo in the
$ npx http-server -p 3000 .
It’s easy to forget to write |new| when creating an object, for example
var vec = Ammo.btVector3(1,2,3); // This is wrong! Need 'new'!
This can lead to error messages like the following:
Cannot read property 'a' of undefined
Cannot read property 'ptr' of undefined
If you find a bug in ammo.js and file an issue, please include a script that reproduces the problem. That way it is easier to debug, and we can then include that script in our automatic tests.
Pushing a new build in
builds/ammo.js should be done only after the
Configure with closure
cmake -B builds -DCLOSURE=1
Build both the asm.js and wasm libraries:
cmake --build builds
Make sure they pass all automatic tests:
Run the WebGL demo in examples/webgl_demo and make sure it looks
ok, using something like
(chrome will need a webserver as it doesn’t like file:// urls)
Bullet 2.82 patched with raycast fix from 2.83