# solved Bounding Sphere Radius and Point Intersection

## Recommended Posts

Just got a quick question demonstrated in the following example: http://www.babylonjs-playground.com/#IK3V1A#6

Q1. You can see a sphere mesh being constructed with a diameter of 4, yet the bounding sphere of said mesh has a radius of '3.4641016151377544'.  Shouldn't it be ~2?

Q2. I may be misunderstanding BoundingSphere.intersectsPoint, but after picking a mesh from the scene, an additional check is done to see if said point exists within the bounding sphere.  This returns true even when dragging across the plane,  far away from the sphere.  Why is this happening?

Snippet of BoundingSphere.intersectsPoint

``````// pick from scene, ignore ray helper, don't use fast pick
const pick = scene.pick(scene.pointerX, scene.pointerY, m => m.name !== 'ray', false)

// if we have a hit and the point exists within bounding sphere
if (pick.hit && bs.intersectsPoint(pick.pickedPoint)) {
...
}``````

Thank you!

##### Share on other sites

http://www.babylonjs-playground.com/#IK3V1A#8

He is right, there is some error in this calculation.

##### Share on other sites

http://www.babylonjs-playground.com/#IK3V1A#9

what is happening is we are relying on a distance calculation that for some reason is not doing it

here is a work around.

``````var min = Number.POSITIVE_INFINITY;
var max = Number.NEGATIVE_INFINITY;
var va = ['x','y','z'];

for(var i=0; i<3; i++){
if(bs.minimum[va[i]]<min){
min=bs.minimum[va[i]];
}
if(bs.maximum[va[i]]>max){
max=bs.maximum[va[i]];
}
}

var distance = max-min;

##### Share on other sites

Could also do this:

if its not truly spherical and want the radius per axis.

##### Share on other sites

Thanks for the quick response and details Pryme8!

Doesn't this mean that each operation performance against the Bounding Sphere is potentially incorrect? EG intersectsPoint, isInFrustum.  Assuming they depend on the internal radius, not 'realRadius'.

##### Share on other sites

ehhhhh, maybe honestly...  I mean it kinda would seem that way.

The distance.length() calc seems to be the problem.
but Im not sure if everything else is working correctly, thats why I left the old stuff and just did a secondary calc.

When I get back from lunch Ill dig more into this math.

##### Share on other sites

Q1 : I guess there's a misunderstanding about what is the bounding sphere of a mesh here, of any mesh (a sphere or a box, or anything). The bounding sphere is simply the sphere embedding the mesh bounding box !

In the particular case where the mesh itself is a sphere, well, it is contained in a bounding box greater than it, this bounding box being contained in its bounding sphere greater than the bounding box.

That's why the bounding sphere radius is always greater than the contained spherical mesh radius.

If you want a better explanation, please have a read at the part  starting by "As you may know, a mesh -so a solid particle- is inside its bounding ..." here : http://doc.babylonjs.com/how_to/solid_particle_system#particle-intersections

In the case of spherical mesh the ratio between the bounding sphere radius and the contained sphere radius is sqrt(3)

##### Share on other sites

soooo:

var r = BABYLON.Vector3.Distance(bs.maximum, bs.minimum);
r /= Math.sqrt(3);
r*=0.5;

I had to divide it in half as well, cause ratio of sqrt 3 just gave me the diameter.

##### Share on other sites

Thanks for the explanation jerome!

##### Share on other sites

If you use the boundingSphere, it always returns true for intersects points, even if you use a very small bounding sphere.

I've implemented a difference version of intersectsPoint, which gives the results I was expecting.

##### Share on other sites

this one works right?

Perhaps I don't get what the problem is

##### Share on other sites
``````const min = new BABYLON.Vector3(-0.5, -0.5, -0.5)
const max = new BABYLON.Vector3(0.5, 0.5, 0.5)
const pnt = new BABYLON.Vector3(50, 50, 50)
const boundingSphere = new BABYLON.BoundingSphere(min, max)

// result is true
const result = boundingSphere.intersectsPoint(pnt)``````

The bounding sphere check above always returns true, although the point doesn't exist within the boundingSphere.

##### Share on other sites

This is a yacky bug I'll fix that now

next commit will contain the fix

Thank you for the heads up