Physics on multiple objects with ammo.js + multiplayer


https://didisoftwares.ddns.net/12/index.html

Testing with the ammo.js physics library, I decided to apply physics to an entire city, including buildings, streets and houses.
And it works very well.

There is another way to apply physics to large or complex objects, which is BVH (Bounding Volume Hierarchy) :
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/physics.html
however, ammo.js is much more complete.

The biggest problem when working with ammo.js is that you must always destroy all variables that are no longer used. Either reuse them, or the memory will burst.
This is the final physics class: https://didisoftwares.ddns.net/12/js/physics.js
With the function I created, you can generate physics for any object with geometry with one line:

import IPHYSICS from "./physics.js"
const physics = new IPHYSICS();
const clock = new THREE.Clock();

await physics.create().then(() => {//wait physics world create  
    const geometry = new THREE.BoxGeometry(1, 1, 1);
    const mesh = new THREE.Mesh(geometry, new THREE.MeshStandardMaterial());
    await physics.createObj(mesh, 'geometry', 'wall', null, 0, null, { margin: 1.0 });
//options: 'wall' - static | 'tile' - ground | 'obj' - dinamic
});

and in update:

const delta = clock.getDelta();
physics.update(delta);

I just simplified the meshes and fixed some broken faces of the model

2 Likes

Yup. I try to explain this to people who are rolling their own physics solutions.

Bullet and by extension, Ammo.js, has been in active development for > 20 years, and has been widely used and battle tested.

Bullet (software) - Wikipedia

I think the other engines that can approach the level of completeness and functionality of ammo/bullet, are Havok, PhysX, and maybe Jolt.

On the flip side there is a lot of stuff in Bullet that isn’t really accesible via ammo , like GPU simulation, and certain features that don’t make as much sense or aren’t as generally flexible, like CCD.

Bullet is a truly general purpose physics engine. Engines like PhysX and Havok I think are a bit more focused on games, and can sacrifice some accuracy for speed, but their web implementations are still not super mature, though in some respects they may have less legacy baggage than bullet/ammo. Similar situation with Jolt physics.

There are a few web engines that are great for smaller scale simulations, and there is definitely a lot of art in how you integrate them. I think as more technologies like webGPU and wasm gc start to arrive, the situation might change, and there may emerge a more dominant, web focused physics API that dispenses with some of the clunkyness of using these wasm’ed APIs. but for now, yea… there are only really a few good choices for doing larger simulations at scale.

Also I don’t think mesh-bvh’s “physics” demos are a valid comparison, since it’s very much focused on collision, which just happens to be useful enough to do game like stuff. It doesn’t try to address things like mass/friction/rotation etc, and that’s ok. It’s targeting a more general domain of geometry manipulation.

For small “maps” and simulations, I will often just use the threejs fps octtree based collision, or mesh-bvh, (easy to use, fast, scales well) or if I need rigid bodies, something like cannon (easy to use, fully js, and fast enough for small sims)…

But for anything larger like you’re doing…
For anything larger, then definitely… ammo/havok/physx/jolt (and possibly rapier, tho I haven’t used it enough to have a strong opinion)

2 Likes

In addition, ammo.js, as mentioned, is based on bullet, which is made in c++. old because the calculations for physics haven’t changed. It is possible to create your own version of ammo.js using ‘emscripten’.
The version I use has a component that I didn’t find in the others, which is ‘btGImpactMeshShape’

1 Like