ammo.js
ammo.js copied to clipboard
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.
I found this Rigidbody code in ammo.js example which applies Rigidbody collision to a plane or a block basically something in cube or Box dimension.
var threeObject = new THREE.Mesh(new THREE.BoxGeometry(sx, sy, sz, 1, 1, 1), material);
var shape = new Ammo.btBoxShape(new Ammo.btVector3(sx * 0.5, sy * 0.5, sz * 0.5));
shape.setMargin(margin);
createRigidBody(threeObject, shape, mass, pos, quat);
But in my case as I couldn't predict the shape , How can I achieve this ..
I saw this post that was similar to my problem . But I couldn't understand how to implement that. How can I use btBvhTriangleMeshShape with my imported obj or gltf . Link to that Post
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 .
I also have the same doubt. Any help would be very much appreciated.
https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=437
Sorry, it isn't a ammo.js example and they don't have example code which is the most annoying part. But they do mention two options you can use. I would look for C++ code of Bullet that uses convex hull or compounds. I would go for convex hull but if your model has a concave spot in it that has to be detected then you will have to use compound.
It's not difficult to apply mesh collision shape to imported model, but it seems like two mesh collision shape can't collide with each other
https://github.com/kripken/ammo.js/issues/339
Hello,
you can do this using following code:
`
static createPhysicsMeshFromGeometry(geometry) {
const triangleMesh = new Ammo.btTriangleMesh();
const vectA = new Ammo.btVector3(0, 0, 0);
const vectB = new Ammo.btVector3(0, 0, 0);
const vectC = new Ammo.btVector3(0, 0, 0);
const verticesPos = geometry.getAttribute('position').array;
const triangles = [];
for (let i = 0; i < verticesPos.length; i += 3) {
triangles.push({
x: verticesPos[i],
y: verticesPos[i + 1],
z: verticesPos[i + 2]
})
}
for (let i = 0; i < triangles.length - 3; i += 3) {
vectA.setX(triangles[i].x);
vectA.setY(triangles[i].y);
vectA.setZ(triangles[i].z);
vectB.setX(triangles[i + 1].x);
vectB.setY(triangles[i + 1].y);
vectB.setZ(triangles[i + 1].z);
vectC.setX(triangles[i + 2].x);
vectC.setY(triangles[i + 2].y);
vectC.setZ(triangles[i + 2].z);
triangleMesh.addTriangle(vectA, vectB, vectC, true);
}
let shape = new Ammo.btBvhTriangleMeshShape(triangleMesh, true);
geometry.verticesNeedUpdate = true
shape.setMargin(0.05);
return shape;
}
//then later call it...
{
this.objLoader = new OBJLoader();
this.objLoader.load(mapModel, (root) => {
let model = root.children[0]
this.geometry = model.geometry
let physicsBody = PhysicsManager.createPhysicsBodyFromGeometry(this.geometry)
PhysicsManager.physicsWorld.addRigidBody(
physicsBody
)
};
`