I'm trying to make a static rigid body collider from the vertex data of a gltf model and I cant get it to work. I only use this mesh collider for the floor, but all the other objects just glitch for a second and then fall trough. If I change the btBvhTriangleMeshShape to a btBoxShape it works just fine, but I would like to have the option to add non box elements to my game map. Is there maybe a better alternative to the btBvhTriangleMeshShape?
const indexOffset =
model.mesh.primitives[0].indices.bufferView.byteOffset / 2;
const indexLength =
model.mesh.primitives[0].indices.bufferView.byteLength / 2;
const index = new Uint16Array(
model.mesh.primitives[0].indices.bufferView.buffer
);
const position = new Float32Array(
model.mesh.primitives[0].attributes.POSITION.bufferView.buffer
);
const positionOffset =
model.mesh.primitives[0].attributes.POSITION.bufferView
.byteOffset / 4;
const positionLength =
model.mesh.primitives[0].attributes.POSITION.bufferView
.byteLength / 4;
this.trimesh = new Ammo.btTriangleMesh();
for (let i = 0; i < indexLength; i += 3) {
let j = 0;
const vertexes = [];
while (j < 3) {
let ind = index[indexOffset + i + j] * 3;
vertexes.push(
Ammo.btVector3(
position[positionOffset + ind],
position[positionOffset + ind + 1],
position[positionOffset + ind + 2]
)
);
j++;
}
this.trimesh.addTriangle(vertexes[0], vertexes[1], vertexes[2]);
}
this.geometry = new Ammo.btBvhTriangleMeshShape(
this.trimesh,
true
);
this.transform = new Ammo.btTransform();
this.transform.setIdentity();
this.transform.setOrigin(
new Ammo.btVector3(
this.model.translation[0],
this.model.translation[1],
this.model.translation[2]
)
);
this.transform.setRotation(
new Ammo.btQuaternion(
this.model.rotation[0],
this.model.rotation[1],
this.model.rotation[2],
this.model.rotation[3]
)
);
this.motionState = new Ammo.btDefaultMotionState(this.transform);
this.localInertia = new Ammo.btVector3(0, 0, 0);
this.geometry.calculateLocalInertia(this.mass, this.localInertia);
this.rbInfo = new Ammo.btRigidBodyConstructionInfo(
this.mass,
this.motionState,
this.geometry,
this.localInertia
);
this.body = new Ammo.btRigidBody(this.rbInfo);
this.body.setFriction(this.friction);
physicsWorld.addRigidBody(this.body);