Dimensions of boundingSphere of sphere mesh don't match

I must be missing something but I can’t get the boundingSphere to return anything close to the dimensions of the sphere. Check the console for this PG:

// Our built-in 'sphere' shape.
var sphere = BABYLON.MeshBuilder.CreateSphere("sphere", {diameter: 2, segments: 32}, scene);

// Get radius of bounding sphere - should be 1.0 right?!
console.log(sphere.getBoundingInfo().boundingSphere.radius);

Shouldn’t the boundingSphere radius match the sphere radius?

Due to this issue or misunderstanding on my part, I’ve been having trouble getting correct results from boundingSphere.intersectsPoint()

What am I doing wrong?

Possibly answering my own question, but if I apply a scale of 0.333333 to the boundingSphere I can get it to conform, like so …

https://playground.babylonjs.com/#KU7GBX#1

So I guess the initial boundingSphere radius is the distance from the center to the boundingBox corner i.e. the bounding box fits inside the bounding sphere?

And … if I apply a position to my sphere, subsequent calls to boundingSphere.intersectsPoint() don’t seem to factor in the new position, like the sphere moves but its bounding box and bounding sphere remain stationary. See console returning false …

https://playground.babylonjs.com/#KU7GBX#2

Yep, it’s pretty clear I’m misunderstanding something fundamental :slight_smile:

Also just discovered and tried Mesh.intersectsPoint() and that’s not working the way I expected it might either.

https://playground.babylonjs.com/#KU7GBX#3

If anyone can shed some light on this I’d appreciate it.

The engine does not know that your mesh is a sphere or any other shape: it’s only a triangle soup.

The bounding sphere is computed thanks to the bounding box: the center of the sphere is the center of the bounding box and the radius is the half of the diagonal. With a unit sphere centered on the origin, the min and max of the bounding box are (-1,-1,-1) and (+1,+1,+1), the diagonal is Math.sqrt(2²+2²+2²)=3.461 and half the diagonal = radius of the bounding sphere = 1.732.

You can do better than the engine if you know the shape of your mesh and provide a more fitting bounding sphere (as you did with the 0.33333333 scale).

If you want to use intersectsPoint yourself, you must make sure the world center/radius are up to date given the world coordinates of the underlying mesh: you need to call _update() with the world matrix of the mesh:

https://playground.babylonjs.com/#KU7GBX#4

Your last example does not work because the world matrix of the sphere is not up to date: call sphere.computeWorldMatrix(true); before calling intersectsPoint.

1 Like

Thanks for the detailed reply @Evgeni_Popov. I knew I must have be missing something crucial.