three.jsphysicsbulletphysicsammo.js

Rigid body (shape) in bullet/ammo.js from a mesh in three.js


I am using bullet/ammo.js with three.js. I have a 3d mesh and I want to use the exact shape for collision detection with a soft body. Is there a way I can create a 3d rigid body (in bullet) from a mesh (in three.js)?

Here is an example: http://kidzinski.com/miamisura/lazy3d/ (please wait a second for the 3d model to download). I have a cloth falling on a 3d body and I need to simulate collision of this cloth with the body.

I am new to these frameworks sorry if I fundamentally misunderstood something.


Solution

  • It looks like you can do some work to turn an arbitrary Three.js mesh into a Bullet concave mesh. This is supported by Physi.js, which is a plug and play solution to link Three.js directly to ammo.js. I personally wouldn't recommend using the project (Physi.js) but you can look at the source code to see how they implement concave meshes.

    First they loop over the geometry to create a custom list of "triangle" data objects on these lines of physi.js

    for ( i = 0; i < geometry.faces.length; i++ ) {
        face = geometry.faces[i];
        if ( face instanceof THREE.Face3) {
            triangles.push([
                ...
    

    Then these triangles are passed off to Ammo.js to make a new Ammo.btBvhTriangleMeshShape on these lines:

    for ( i = 0; i < description.triangles.length; i++ ) {
        ...
        triangle_mesh.addTriangle( _vec3_1, _vec3_2, _vec3_3, true );
    }
    
    ...
    
    shape = new Ammo.btBvhTriangleMeshShape( triangle_mesh, true, true );
    

    This should be a good starting point for building your own Ammo.js custom mesh.