CannonJs, collision issue with dynamic bodies and static bodies

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

Don’t know if your problem is related, but if you create objects in an interpenetrating state, they sometimes behave like this… you can verify it by trying to drop the object from higher up initially and if it then does fall and collide, then it’s probably an initial interpenetration problem.

When I drop the objects on the floor which is static, it passes through through the, this happens only for the colliders which are created from gltf objects.

Can you try calling glb.scene.updateMatrixWorld(true) before trying to get the bounding box

Okay, i have worked with react three fiber with rapier physics and in that, if the colliding body is a plane then it passes through it, it must have some z value.

for your case, inside your model if there are some meshes which is plane, try changing it to boxgeometry and check if it works.

I need to use the plane walls as i need to make them One Sided to view the objects inside the room. But, I made the collider bodies boxes, as the plane bodies were not rotating properly according to model. It collides after I increased the thickness but after few milli seconds it sinks into the floor and falls. The model I’m using has the origin to its base, and offsetting it by half the size in y-direction while updating the physics.