3

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.

Ben
  • 2,314
  • 1
  • 19
  • 36
Łukasz Kidziński
  • 1,613
  • 11
  • 20
  • 1
    Note that you probably don't want to use a mesh that detailed (it gets computationally very heavy very quickly) and a low poly count proxy model is usually used for such calculations instead. Visually it won't make too much of a difference. You'd need 3D modelling software to make that low poly count mesh though, doing it programmatically is difficult, and when creating it by hand you can fine tune the result as well. – Leeft Sep 12 '16 at 06:57
  • @Leeft thanks I expect some computational issues with the direct approach but still I want to start with anything working, since the correct physics are the key of the project. I will try the low-poly approximation in the next step (especially that there are regions I care less about). Thanks! – Łukasz Kidziński Sep 13 '16 at 17:48

2 Answers2

5

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.

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
0

There are lots of threads around the web, that Physijs Concave mesh does not work with collission. It seems, that btBvhTriangleMeshShape is not intended to work with collission in ammo.js, as I found out searching for that topic in bullet related forums.

What worked for me, is btConvexHullShape:

var triangle, triangle_mesh = new Ammo.btTriangleMesh;
var btConvexHullShape = new Ammo.btConvexHullShape();
var _vec3_1 = new Ammo.btVector3(0,0,0);
var _vec3_2 = new Ammo.btVector3(0,0,0);
var _vec3_3 = new Ammo.btVector3(0,0,0);
for ( i = 0; i < triangles.length; i++ ) {
    triangle = triangles[i];
    _vec3_1.setX(triangle[0].x);
    _vec3_1.setY(triangle[0].y);
    _vec3_1.setZ(triangle[0].z);
    btConvexHullShape.addPoint(_vec3_1,true);
    _vec3_2.setX(triangle[1].x);
    _vec3_2.setY(triangle[1].y);
    _vec3_2.setZ(triangle[1].z);
    btConvexHullShape.addPoint(_vec3_2,true);
    _vec3_3.setX(triangle[2].x);
    _vec3_3.setY(triangle[2].y);
    _vec3_3.setZ(triangle[2].z);
    btConvexHullShape.addPoint(_vec3_3,true);
    triangle_mesh.addTriangle(
        _vec3_1,
        _vec3_2,
        _vec3_3,
        true
    );
}
return btConvexHullShape;

In the process of learning physic based 3d with threejs, I also want to mention the following best practice: when using complex models, create a low poly model that you can push to that converter function instead of the original model, or you will encounter a stack overflow.