Bounding box & intersectsPoint() when mesh is rotated

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?

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)) { ... }

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.

1 Like

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

Looks like it works in playground: Babylon.js Playground
For some reason computeWorldMatrix does not have any effect in my own project though.

Would be great to have a repro if you can :slight_smile:

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:

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

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

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):

1 Like

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

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;
1 Like

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:

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 :+1: