gkjohnson / three-mesh-bvh

A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.
https://gkjohnson.github.io/three-mesh-bvh/example/bundle/raycast.html
MIT License
2.38k stars 247 forks source link

getBoundingBox box very slightly bigger than threejs bounding box #680

Closed ToyboxZach closed 6 days ago

ToyboxZach commented 1 week ago

Describe the bug

Calling:

On a BoxGeometry geometry creates a bounding box that is very slightly bigger than the geometry itself

To Reproduce

const boxGeo = new BoxGeometry(width, depth, height);
const bvh =  new MeshBVH(boxGeo);
const box = new Box3();
bvh.getBoundingBox(box);
vs
boxGeo.computeBoundingBox()

Expect that to be nice round numbers of 0.5 -> -0.5 instead it adds just a little bit to the edge of each edge. image

Maybe I misunderstand the purpose of this bounding box, but this ended up causing us problems because we wanted to get the center of the geometry and it was just barely off for some less symetrical cases

gkjohnson commented 6 days ago

The bounds of the BVH are stored as half-widths and expanded by a small epsilon (specifically 2^-24) to account for floating point error and ensure that the triangles are correctly contained (see #169, #170).

What specific issue is this causing for you?

ToyboxZach commented 6 days ago

Ah ok, we are using this bounding box incorrectly then. We were just depending on it to get the exact center of an object to be able to adjust a geometry’s position.

Maybe it’s an issue that it’s not symmetrical but doesn’t seem like this is the purpose of this bounding box

gkjohnson commented 5 days ago

If you'd like to adjust the documentation to help make the behavior more clear I'd be open to a PR. Also note that by default that bounding box will be assigned to the geometry when generating the BVH if it hasn't already been generated. This behavior can be disabled with setting the setBoundingBox option to false, though.