Detecting intersection for adding objects

Hi ! I’m trying to detect if there’s an intersection between the text and the donut using .intersectsBox(), but I get a weird result, it seems the donuts geometry (not mesh) still are positionned at the center of the scene, anyone got any idea why that code don’t work ? (edited)

Check if this helps. Clone the donut bounding box and apply world matrix of mesh. Then do intersection test.

const box3 = donutGeometry.boundingSphere.clone();
box3.applyMatrix4(donut.matrixWorld);
// do intersection test using box3

Thanks for the answer, it raise an error that I don’t know how to fix :

The post by @anidivr mentions that you should do the intersection test using box3, so your if should begin with if (!box3.intersectsBox(... - without having to call .boundingBox on an entity that’s already a bounding box itself.

Ah yes that was a mistake from my part,
still, it produce the same anomaly :

By checking the values of the boudingSpheres, they all seems to be at the scene origin,
I don’t understand why, moving the mesh should have affected the geometry center too no ? Like in blender, moving the mesh in object mode should have moved the geometry center from edit mode
I don’t get it :thinking:

Does updating the donut’s world matrix before applying it to the box3, via donut.updateMatrixWorld(true); solve the problem?

Alternatively, you could just set the box3 from the object, via .setFromObject(), in order to account for all the transformations performed on the said object.

P.S. Blender does internally update various things when you do stuff, in Three.js your code should handle that appropriately…

1 Like

You’re a GENIUS :heart_eyes: :heart_eyes: :heart_eyes:
Thanks a lot !! The update matrix worked :

(Both of your solutions worked)

I’m glad it helped! :wink: By the way, this could have probably been done using something similar to:

donut.getWorldPosition(box3.position);
donut.getWorldQuaternion(box3.quaternion);
donut.getWorldScale(box3.scale);

as well, in order to set each donut transformation on the box3, without the need to update anything, but matrices usually need a bit of special handling in Three.js when it comes to updating them.

1 Like