Similar questions here and here but no answers or wrong language.
I've created a btBvhTriangleMeshShape
from a GLB object I created in Blender. But my objects just fall straight through it. I've nicked Enable3D's DebugDrawer and looking at the wireframe it doesn't look good, perhaps that's the issue? TBH I found the "Mesh to btBvhTriangleMeshShape" code online after much Googling, and don't fully understand it, so perhaps there's something wrong there? They are however using btConvexTriangleMeshShape
but as you can see here they have a commented line using btConvexTriangleMeshShape
so I assumed I could just switch them out?
createConcaveShapeFromObj (obj) {
// New ammo triangles
const triangleMesh = new Ammo.btTriangleMesh();
// Declare triangles position vectors
const vectA = new Ammo.btVector3(0, 0, 0);
const vectB = new Ammo.btVector3(0, 0, 0);
const vectC = new Ammo.btVector3(0, 0, 0);
// Retrieve vertices positions from object
const verticesPos = obj.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]
})
}
// Use triangles data to draw ammo shape
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);
}
var shape = new Ammo.btConvexTriangleMeshShape(triangleMesh, true); // Note that btConvexTriangleMeshShape works, but collision is done on the bounding box instead which doesn't work either.
shape.setMargin(0.05);
return shape;
}
You can view the full project here.