# Raycaster not working when boundingbox apply object matrix

**URL:** <https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143>\
**Category:** Questions\
**Tags:** raycaster\
**Created:** [February 17, 2023, 7:42am UTC](https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143 "2023-02-17T07:42:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discourse.threejs.org/u/rookie)\
**Post date:** [February 17, 2023, 7:42am UTC](https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143/1 "2023-02-17T07:42:38Z")

</div>

I find that when a mesh computing boundingbox and the box apply its mesh matrix, raycaster can’t catch the mesh. What causes this to happen?

```javascript
object.geometry.computeBoundingBox();
object.geometry.boundingBox.applyMatrix4(object.matrix.clone()); // the reason

```

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [February 17, 2023, 9:14am UTC](https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143/2 "2023-02-17T09:14:42Z")

</div>

This is because how intersection is optimized.

1. It first checks intersection with the bounding sphere. If there is no intersection, that’s all.
2. Then it checks intersection with the bounding box. If there is no intersection, that’s all.
3. Finally it checks intersection with the individual faces.

So, if you modify manually the bounding box, the raycasting reaches step 2 and exits.

Edit: The bounding box is `Box3` object and it is always AABB. This pages shows how to update it:  
[https://threejs.org/docs/?q=Box3#api/en/math/Box3](https://threejs.org/docs/?q=Box3#api/en/math/Box3)

---

<div class="post-metadata">

**Author:** ![rookie](https://avatars.discourse-cdn.com/v4/letter/r/e47c2d/32.png) [@rookie](https://discourse.threejs.org/u/rookie)\
**Post date:** [February 17, 2023, 9:29am UTC](https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143/3 "2023-02-17T09:29:49Z")

</div>

Does it mean that we don’t modify the properties “object.geometry.boundingBox” in order to make raycaster normal working?

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [February 17, 2023, 9:45am UTC](https://discourse.threejs.org/t/raycaster-not-working-when-boundingbox-apply-object-matrix/48143/4 "2023-02-17T09:45:59Z")

</div>

Yes, I expect that raycasting will still work if you move/scale/rotate the object.

You may need to recalculate the bounding box and bounding sphere only if you manually change the vertices in the geometry.
