.intesectsBox return false

I used this method to copy gltfmodel box to outside of gltfloader function
live example

const gltfBox2 = new THREE.Box3();

gltfLoader.load('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Fox/glTF/Fox.gltf',(gltf) => {
  gltf.scene.traverse((child) => {
    gltf.scene.updateMatrixWorld(true);
    if (child.isMesh) {
.
.
.
const gltfBox = new THREE.Box3().setFromObject(m);
gltfBox2.copy(gltfBox);

.
.
.
    }
     });
  });



function updateBoxes() {
  dummies.forEach((d, idx) => {
.
.
.
let box = new THREE.Box3().setFromObject(cube);
const intersectBox = gltfBox2.intersectsBox(box);
console.log(intersectBox);

});
}

Why intersectBox return false?

Does anyone have any ideas?

I have updated the example,
As you can see in the example, .intersectBox returns true when the blue cube and other cubes intersect. The problem is with the gltf model, which returns false when it intersects with cubes
How can I solve it?

hey teymoor, I havne’t had a chance to look in any detail, but is it possible that you run your intersectBox function before the fox is loaded? you might have to change your logic so that you run intersectBox as a callback

2 Likes

intersectBox It currently runs before loading the model.
I think I need to run “intersectBox” in the “updateBoxes” function to check the intersection of all cubes with the model. As I did now. Currently, this happens before the model is loaded.

function updateBoxes(gltfBox) {
  dummies.forEach((d, idx) => {

let box = new THREE.Box3().setFromObject(cube);
const intersectBox = box.intersectsBox(gltfBox);
console.log(intersectBox);

});
}
gltfLoader.load('https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Models/master/2.0/Fox/glTF/Fox.gltf',(gltf) => {
  gltf.scene.traverse((child) => {
    gltf.scene.updateMatrixWorld(true);
    if (child.isMesh) {

const gltfBox = new THREE.Box3().setFromObject(m);

updateBoxes(gltfBox);

    }
     });
  });

I have update the code with your point, now “console.log(Intersect Box);” Returns true when cubes collide with the model, but I get this error :

err

it looks like you still call updateBoxes() outside of the scope? what happens if you remove that

 gsap.to(x, {
    onUpdate: updateBoxes,
  });

I have this code in the model click function
If I remove “updateBoxes()” outside the scope, animation does not work

You call the updateBoxes function outside the callback scope

In any case, I think we’ve solved your original problem

updateBoxes(); //try removing this function call
/**
 * Renderer
 */
const renderer = new THREE.WebGLRenderer({
    canvas: canvas
})
1 Like