Resolved: Bouding box is one move late

I am trying to get an immediate real-time intersection. So, I want “intersection” to be printed in the console when the window bounding box intersects with the wall bounding box. However, I noticed that the window box is one move late. So, the first time I put the window in the wall, not instersection is captured. I need to move the window for a second time in the wall so I get the intersection printed in the console.

Does someone know what is the problem?

I have been trying to update the boxes, but the issue persists.

Here is the code:

private wallWindowIntersection(window, walls) {

 let intersection = false;

    // Compute the bounding sphere for the window
    const meshesWindow = window.meshes[0];
    meshesWindow.updateMatrixWorld(true);

 const windowBoundingBox = new THREE.Box3();
    const min = new THREE.Vector3(
      adjustedCenter.x - width / 2,
      adjustedCenter.y - height / 2,
      adjustedCenter.z - depth / 2
    );
    const max = new THREE.Vector3(
      adjustedCenter.x + width / 2,
      adjustedCenter.y + height / 2,
      adjustedCenter.z + depth / 2
    );
    windowBoundingBox.set(min, max);

    windowBoundingBox.applyMatrix4(meshesWindow.matrixWorld);

    const boxHelper = new THREE.Box3Helper(windowBoundingBox, 0x00ff00);

scene.add(boxHelper);

      const meshesWall = wall.meshes[0];
      meshesWall.updateMatrixWorld(true);
 const boundingBoxWall = new THREE.Box3().setFromObject(meshesWall);

      const wallHelper = new THREE.BoxHelper(meshesWall, 0x00ff00);
      this.world.scene.three.add(wallHelper);

if (windowBoundingBox.intersectsBox(boundingBoxWall)) {
        intersection = true;
        console.log("INTERSECTION", intersection);
 break;
      }
    }
    return intersection;
  }

I am learning three.js, so I am a beginner. I apologize if the question is idiot.

:thinking: This peice of code looks suspect, there’s also one too many closing brackets it looks like from what you’ve posted, can you give this a try instead…

if (windowBoundingBox.intersectsBox(boundingBoxWall)) {
        intersection = true;
        console.log("INTERSECTION", intersection);
 }
 else{
       intersection = false;
        console.log("INTERSECTION", intersection);
 } 
    return intersection;
}

I tried your code, but nothing happens. I think it is that the bounding box is late somehow. Because the box helper is displayed always late.

how about using BVH for collision detection. I have used that in one my codes and its working like a charm.

Three-mesh-bvh

The problem was related to another part of the code and not with the bounding box! I fixed it, it was related with the drag controls.

Thank you guys

1 Like