• Stars
    star
    4,005
  • Rank 10,359 (Top 0.3 %)
  • Language
    C++
  • License
    Other
  • Created almost 13 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Direct port of the Bullet physics engine to JavaScript using Emscripten

ammo.js

Demos

Overview

Example code to give you an idea of the API:

ammo.js is a direct port of the Bullet physics engine to JavaScript, using Emscripten. The source code is translated directly to JavaScript, without human rewriting, so functionality should be identical to the original Bullet.

'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)

Instructions

builds/ammo.js contains a prebuilt version of ammo.js. This is probably what you want.

You can also build ammo.js yourself.

Usage

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

https://emscripten.org/docs/compiling/Building-Projects.html

or you can use Bullet directly from emscripten-ports, with -s USE_BULLET=1. In both cases, you don't need ammo.js, just plain Bullet.

If, on the other hand, you want to write code in JavaScript, you can use the autogenerated binding code in ammo.js. A complete example appears in

examples/hello_world.js

That is HelloWorld.cpp from Bullet, translated to JavaScript. Other examples in that directory might be useful as well. In particular see the WebGL demo code in

examples/webgl_demo/ammo.html

Bindings API

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 |get_| or |set_|. For example,

    rayCallback.get_m_rayToWorld()

    will get m_rayToWorld from say a ClosestRayResultCallback. Native JavaScript getters and setters could give a slightly nicer API here, however their performance is potentially problematic.

  • Functions returning or getting float& or btScalar& are converted to float. The reason is that float& is basically float* with nicer syntax in C++, but from JavaScript you would need to write to the heap every 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
    = op_set
    + op_add
    - op_sub
    * op_mul
    / op_div
    [] op_get
    == op_eq

Building

In order to build ammo.js yourself, you will need Emscripten and cmake.

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 -DEMSCRIPTEN_ROOT.

Building using Docker

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 docker-compose.yml file.

Reducing Build Size

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 btIDebugDraw and 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 DebugDrawer.

Testing

You can run the automatic tests with npm test, which in turn will run ava against both the javascript and WebAssembly builds:

$ 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

When no AMMO_PATH is defined, builds/ammo.js is tested by default.

Running the Examples

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 builds directory:

$ npx http-server -p 3000 .

Troubleshooting

  • 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

    This is an annoying aspect of JavaScript, sadly.

Reporting Issues

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.

Release Process

Pushing a new build in builds/ammo.js should be done only after the following steps:

  • Configure with closure enabled: cmake -B builds -DCLOSURE=1

  • Build both the asm.js and wasm libraries: cmake --build builds

  • Make sure they pass all automatic tests: npm test

  • Run the WebGL demo in examples/webgl_demo and make sure it looks ok, using something like firefox examples/webgl_demo/ammo.html (chrome will need a webserver as it doesn't like file:// urls)

Upstream Version

Bullet 2.82 patched with raycast fix from 2.83

More Repositories

1

BananaBread

BananaBread is a C++ 3D game engine that runs on the web using JavaScript+WebGL+HTML
C++
1,353
star
2

box2d.js

Port of Box2D to JavaScript using Emscripten
C
1,299
star
3

speak.js

Text-to-Speech in JavaScript using eSpeak
C++
1,273
star
4

llvm.js

LLVM compiled to JavaScript using Emscripten
JavaScript
453
star
5

webgl-worker

JavaScript
230
star
6

xml.js

Port of libxml to JavaScript using Emscripten
JavaScript
175
star
7

zee.js

zlib compiled to JavaScript using Emscripten
C
100
star
8

j2k.js

Port of OpenJPEG, an open-source JPEG2000 codec, to JavaScript using Emscripten
C
82
star
9

intensityengine

[Not active!] An open source platform for 3D games and virtual worlds
C++
57
star
10

lzma.js

An LZMA implementation in JavaScript, compiled from lzip
C++
56
star
11

meandmyshadow.web

C
51
star
12

Massive

The asm.js benchmark
JavaScript
46
star
13

gmp.js

A port of the GNU Multiple-Precision Library (GMP), a library for arbitrary precision arithmetic, to JavaScript using Emscripten
C
43
star
14

mloc_emscripten_talk

JavaScript
41
star
15

clangor

clang ported to js
C++
40
star
16

worker-ui

Discussions and experiments with splitting UI rendering off the main thread on the web, using workers
34
star
17

boon

Freedoom + PrBoom: Open source Doom in HTML5
C++
33
star
18

Relooper

this repo is deprecated, see the binaryen repo
C++
21
star
19

build-suite

Pre-built WASM builds
JavaScript
19
star
20

bullet

C++
18
star
21

cashew

asm.js parser in C++
C++
18
star
22

syntensity

Experimental port of Sauerbraten/Syntensity to the web, using Emscripten
C++
17
star
23

llvm-js

C++
16
star
24

talks

JavaScript
16
star
25

clang-js

C++
14
star
26

cxx_demangle

C++ name demangling
JavaScript
14
star
27

misc-js-benchmarks

Miscellaneous JavaScript Benchmarks, of Dubious Value
JavaScript
13
star
28

emscripten-site

Emscripten website
HTML
12
star
29

demo-bananabread

BananaBread demo
JavaScript
10
star
30

musl-emscripten

C
10
star
31

binaryen-webpack-plugin

Binaryen plugin for webpack: shrinks wasm files
Python
8
star
32

webgl-2d-screen

JavaScript
7
star
33

embenchen

Emscripten benchmark suite
JavaScript
6
star
34

xz-lzma.js

C
3
star
35

simulations

Python
2
star
36

almond

C
2
star
37

tps_report_cloud_archiver

JavaScript
2
star
38

blog

SCSS
2
star
39

minir

C++
1
star
40

embooken

1
star
41

llvm-js-2

Trying to coax the Cpu0 backend to output JS
C++
1
star