I’m having issue with cannonjs physics, some dynamic bodies which are created from bounding box of a glb model do not collide with static bodies created using threejs Mesh will collide.
here is the code snippet,
createBody(object, bodyType, shape) {
console.log(bodyTypesData[bodyType.toLowerCase()]);
let box = new THREE.Box3().setFromObject(object);
let size = box.getSize(new THREE.Vector3());
let pos = box.getCenter(new THREE.Vector3());
let body = new CANNON.Body({
mass: bodyType === 'Static' ? 0 : 1,
type: bodyTypesData[bodyType.toLowerCase()],
shape: new CANNON.Box(
new CANNON.Vec3(size.x / 2, size.y / 2, size.z / 2)
),
});
console.log(body.type);
object.userData.size = size;
body.position.copy(pos);
body.quaternion.copy(object.quaternion);
body.threeMesh = object;
object.cannonBody = body;
this.world.addBody(body);
this.threejsBodies.set(object, body);
this.cannonjsBodies.set(body, object);
console.log(this.threejsBodies, this.cannonjsBodies);
}
and I’m also changing specific body to kinematic while using custom transform controls and changing it back to dynamic after the interaction
findPhysicsBodyByObject(object, position) {
console.log('finding body');
let foundBody = null;
for (let i = 0; i < this.world.bodies.length; i++) {
let body = this.world.bodies[i];
if (body.threeMesh === object) {
foundBody = body;
break;
}
}
foundBody.type = CANNON.Body.KINEMATIC;
foundBody.type;
this.updateKinematicBodyPosition(foundBody, position);
}
updateKinematicBodyPosition(body, newPosition) {
if (body.type === CANNON.Body.KINEMATIC) {
body.position.copy(newPosition);
}
}
kinematicToDynamic() {
console.log('kinematic to dynamic');
// Get all kinematic bodies in the world
const kinematicBodies = this.world.bodies.filter(
(body) => body.type === CANNON.Body.KINEMATIC
);
console.log(kinematicBodies);
// Change the body type to Dynamic for each kinematic body
kinematicBodies.forEach((body) => {
body.type = CANNON.Body.DYNAMIC;
});
}
Thanks for the help