PickingRay does not work on a boundingbox of a moved mesh

Hi there !

The playground below shows that a ray (from screen point x,y) is not working on a moved mesh.

Using those functions changes nothing.

box.computeWorldMatrix(true);
box.refreshBoundingInfo(true, true);

The function :

ray.intersectsBox(box.getBoundingInfo().boundingBox);

continues to work with the initial mesh position.

Is that a bug or am I missing something ?

Thank you

It’s not a bug :slight_smile:
box.getBoundingInfo().boundingBox is local (not global world coords)

You may want to use result = ray.intersectsMesh(box); and result.hit;

Edit : you could also work on a global bounding box but keep in mind it may be problematic with ray picking, since a bounding box can be bigger than the mesh itself (for example if you rotate your cube)

2 Likes

I just notice it was in the documentation, but I usually directly read source code comments in my IDE and it is not written it was LOCAL space :slight_smile:

I was going with this working workaround :

public getWorldSpaceBoundingBox(): BoundingBox {
    const boundingBox: BoundingBox = this.getMesh().getBoundingInfo().boundingBox;
    const minTransformed: Vector3 = boundingBox.minimumWorld;
    const maxTransformed: Vector3 = boundingBox.maximumWorld;
    const center: Vector3 = Vector3
      .Center(minTransformed, maxTransformed);
    const scaleFactor: number = 0.8;
    const scaledMin: Vector3 = Vector3.Lerp(center, minTransformed, scaleFactor);
    const scaledMax: Vector3 = Vector3.Lerp(center, maxTransformed, scaleFactor);
    return new BoundingBox(scaledMin, scaledMax);
}

UPDATE: I want to intersect with the boundingBox of the mesh, not the mesh itself

a bounding box can be bigger than the mesh itself (for example if you rotate your cube)

That is my second problem, my workaround above plays on a scale factor but it is not clean…

No sorry, your solution is NOT what I am looking for : I want to check collision with the boundingBox not the mesh !

In this case just create a box with the same size as BoundingBox and check collisions with it.

I thought about that but why, in the given playgrounds, when we say “show bounding box” the bounding appears in the world space ? Babylon already has this box and manages to display it in the world space, I would like to reuse it instead of doing custom stuff.

Like so? https://playground.babylonjs.com/#JJMFBC#8

The rayhelper is off though :frowning:

1 Like