RigidBody with imported obj in THREE.js and Ammo.js Collision

I have imported 3d obj and glb files in my THREE.js and Ammo.js project .
For example lets take say i have a tiger obj and a lion obj or glb file.

I need to apply Rigidbody constraint to both of them so when they move towards each other they should collide.

But in my case as I couldn’t predict the shape , How can I achieve this …

How can I apply the Rigidbody function to my loaded obj model.


 var objLoader2 = new THREE.OBJLoader();
 objLoader2.load('../../objfiles/Object.obj', function(object) {
     scene.add(object);
 });

Do someone have the code for that.
Or if someone could help me solve this problem it would be of great help .

2 Likes

noooooooo the link is broken! do you still have a solution?

I’ve deleted my previous post since @Coder’s crosspost at stackoverflow did not receive any answers and thus was deleted.

2 Likes

Hey,Has it been solved,I met too。

If the mesh of the model has parameters attribute like SphereBufferGeometry , BoxBufferGeometry,.then I can traverse to get the length, width and height of the Geometry in the model and create the corresponding ammojs physical model. :sweat_smile: :sweat_smile:

Hey, im using this function ATM. I need to scale up the geometry to get the right shape, so if anyone knows how to improve, let me know. This is for static rigid bodies / kinematic bodies only!

function createTriangleShapeByBufferGeometry(geometry, scalingFactor) {
        var mesh = new Ammo.btTriangleMesh(true, true);
        var vertexPositionArray = geometry.attributes.position.array;
        for (var i = 0; i < geometry.attributes.position.count/3; i++) {
                mesh.addTriangle(
                    new Ammo.btVector3(vertexPositionArray[i*9+0]*scalingFactor, vertexPositionArray[i*9+1]*scalingFactor, vertexPositionArray[i*9+2]*scalingFactor ),
                    new Ammo.btVector3(vertexPositionArray[i*9+3]*scalingFactor, vertexPositionArray[i*9+4]*scalingFactor, vertexPositionArray[i*9+5]*scalingFactor),
                    new Ammo.btVector3(vertexPositionArray[i*9+6]*scalingFactor, vertexPositionArray[i*9+7]*scalingFactor, vertexPositionArray[i*9+8]*scalingFactor),
                    false
                );
        }
        var shape = new Ammo.btBvhTriangleMeshShape(mesh, true, true);
        return shape;
    }