Mesh.intersects with onlyBoundingInfo=true fails to account for world matrix when calculating distance

I get nonsensical distance numbers and unexpected picking results when picking with onlyBoundingInfo=true. It appears to be because the world matrix is not accounted-for when reporting the distance from this picking method.

compare Babylon.js/abstractMesh.ts at 68bf4bf9985fb56d7ed27d6630d600c0355b1932 · BabylonJS/Babylon.js · GitHub

with Babylon.js/abstractMesh.ts at 68bf4bf9985fb56d7ed27d6630d600c0355b1932 · BabylonJS/Babylon.js · GitHub

1 Like

Hi @FrameVRWill, could you please share a repro? Let’s ping @Evgeni_Popov to get his feedback.

2 Likes

This should probably be boundingInfo.boundingSphere.centerWorld instead of center:

Babylon.js Playground (babylonjs.com)

Can you try calculating yourself and see if the distance using the centerWorld is the right one?

@RaananW, fancy doing a PR for it ?

I tried just substituting centerWorld before reporting, but the numbers were still way off (getting like 10,000 for distance where picking the same object with onlyBoundingInfo=false would give me a distance of 3.

I believe that ray is not in world space here based on the conversions done later in the pickedPoint logic

1 Like

how do you create the ray? can you share a reproduction? the simplest one possible one possible so I can understand what doesn’t work exactly

I don’t create the ray, this it is the one provided by the scene picking loop. Please see linked code Babylon.js/abstractMesh.ts at 68bf4bf9985fb56d7ed27d6630d600c0355b1932 · BabylonJS/Babylon.js · GitHub

I will try to make time to create an example for you

1 Like

The scene.pick doesn’t allow you to set onlyBoundingInfo (AFAIK?), so i assumed you are using intersects directly. Are you referring to the scene.pick function? or a different picking function?

1 Like

I’m overriding mesh.intersects to force onlyBoundingInfo=true for certain, high poly meshes (just passing through all other args to intersects).

I found the fix. This gives the correct distance:

Vector3.TransformCoordinatesToRef(ray.origin, world, tempVector);
distance = Vector3.Distance(tempVector, mesh.getBoundingInfo().boundingSphere.centerWorld);

(ray & world are the arguments given to mesh.intersects)

So the ray is delivered by you?

No man, it’s the one provided by the scene picking loop

@FrameVRWill would you be willing to create a PR for it ?

I believe a reproduction will be best in this case to fully understand the scenario.
We are internally using onlyBoundingInfo=true in _internalPick ( Babylon.js/ray.ts at master · BabylonJS/Babylon.js (github.com)) so it is working for this usecase as expected. Though it does feel odd that we are using center and not centerWorld.

@FrameVRWill - is scene.pick working as expected after this change? If it does, a PR would be wonderful! :slight_smile:

1 Like