# Bounding box & intersectsPoint() when mesh is rotated

**URL:** <https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308>\
**Category:** Questions\
**Created:** [January 12, 2023, 5:48am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308 "2023-01-12T05:48:45Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thompsoni](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thompsoni](https://forum.babylonjs.com/u/thompsoni)\
**Post date:** [January 12, 2023, 5:48am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/1 "2023-01-12T05:48:45Z")

</div>

I’m trying to check if a point is inside the bounding box of a mesh. Everything seems to work fine as long as I don’t rotate the mesh.

For some reason intersectsPoint keeps checking for the non-rotated dimensions of the bounding box although I can see that the bounding box is drawn correctly. Am I supposed to somehow update the bounding box after rotation?

```auto
const testBox = BABYLON.MeshBuilder.CreateBox('TESTBOX', { size: 0.3 }, scene);
testBox.position.set(1, 0.15, -1);
testBox.rotation.set(0, 1, 0);

testBox.getBoundingInfo().intersectsPoint(this.predictionPos)) { ... }

```

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 12, 2023, 7:55am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/2 "2023-01-12T07:55:28Z")

</div>

Welcome aboard!

You should probably add a `testBox.computeWorldMatrix(true);` call after you update the mesh position and rotation to make sure the world matrix of the mesh is up to date before calling `intersectsPoint`.

---

<div class="post-metadata">

**Author:** ![thompsoni](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thompsoni](https://forum.babylonjs.com/u/thompsoni)\
**Post date:** [January 12, 2023, 4:16pm UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/3 "2023-01-12T16:16:54Z")

</div>

This does not have any effect. Also tried testBox.refreshBoundingInfo(true, true) but nothing changes

---

<div class="post-metadata">

**Author:** ![thompsoni](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thompsoni](https://forum.babylonjs.com/u/thompsoni)\
**Post date:** [January 12, 2023, 4:47pm UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/4 "2023-01-12T16:47:21Z")

</div>

Looks like it works in playground: [Babylon.js Playground](https://playground.babylonjs.com/#ZMJ0VP)  
For some reason computeWorldMatrix does not have any effect in my own project though.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [January 12, 2023, 9:31pm UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/5 "2023-01-12T21:31:45Z")

</div>

Would be great to have a repro if you can 🙂

---

<div class="post-metadata">

**Author:** ![thompsoni](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thompsoni](https://forum.babylonjs.com/u/thompsoni)\
**Post date:** [January 16, 2023, 1:00am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/6 "2023-01-16T01:00:32Z")

</div>

Getting back to this, so the issue is that the drawn bounding box dimensions are not same as the dimensions where intersectsPoint is checking it. you can see the problem here: [https://playground.babylonjs.com/#ZMJ0VP#1](https://playground.babylonjs.com/#ZMJ0VP#1)

the actual bounding box area is apparently like in this image:

 ![collision2](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/2/32f8902b789ba502f4c7d4e2038d588e3a2e6668.png)

Is this intentional feature? I need to check if a point is actually inside the mesh boundaries itself. @Evgeni_Popov @sebavan

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 16, 2023, 10:24am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/7 "2023-01-16T10:24:22Z")

</div>

Yes, the bounding box is always an AABB (Axis Aligned Bounding Box). You would need to test with an OBB (Oriented Bounding Box).

As I can see it, there’s no way to test that a point intersects an OBB, but you can test two OBBs by using `mesh.intersects(otherMesh, true)`: passing `true` as the second parameter uses OBBs instead of AABBs in the test. So, you can represent your point as a small mesh (cube, for eg):

> **[Babylon.js Playground](https://playground.babylonjs.com/#ZMJ0VP%233)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

---

<div class="post-metadata">

**Author:** ![thompsoni](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@thompsoni](https://forum.babylonjs.com/u/thompsoni)\
**Post date:** [January 16, 2023, 11:26pm UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/8 "2023-01-16T23:26:43Z")

</div>

If someone having the same issue, here is a function to check if a point is inside mesh boundaries:

```auto
export function isPointInsideMesh(point: BABYLON.Vector3, mesh: BABYLON.Mesh | BABYLON.AbstractMesh) {
    // Get the world matrix of the mesh
    const worldMatrix = mesh.getWorldMatrix();

    // Invert the world matrix to get the local matrix
    const localMatrix = BABYLON.Matrix.Invert(worldMatrix);

    // Transform the point from world space to local space
    const localPoint = BABYLON.Vector3.TransformCoordinates(point, localMatrix);

    // Get the mesh's bounding box in local space
    const boundingBox = mesh.getBoundingInfo().boundingBox;

    // Check if the point is inside the bounding box, rounding might not be necessary
    //localPoint.x = Math.round(localPoint.x * 1000) / 1000;
    localPoint.y = Math.round(localPoint.y * 1000) / 1000;
    //localPoint.z = Math.round(localPoint.z * 1000) / 1000;

    if (localPoint.x < boundingBox.minimum.x || localPoint.x > boundingBox.maximum.x) return false;
    if (localPoint.y < boundingBox.minimum.y || localPoint.y > boundingBox.maximum.y) return false;
    if (localPoint.z < boundingBox.minimum.z || localPoint.z > boundingBox.maximum.z) return false;

    return true;
}

```

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [January 17, 2023, 12:46am UTC](https://forum.babylonjs.com/t/bounding-box-intersectspoint-when-mesh-is-rotated/37308/9 "2023-01-17T00:46:20Z")

</div>

This test is still a AABB test. In your PG, it happens that the box is aligned with the axis in its local coordinates system, but it may not be the case in general for any mesh.

For eg, if in your PG we bake the current transformation so that the vertices are modified by the transformation, the box is not aligned with the axis anymore and the check will fail:

> **[Babylon.js Playground](https://playground.babylonjs.com/#ZMJ0VP%234)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

However, if you know that your meshes are aligned with the axis in its local coordinate system, this is a smart way to do it 👍
