[SOLVED]Telling if a mesh is inside a box?

I am working on a simple mesh sculpting thing, and have the sculpting working fine. The mesh is inclosed in a “Bounding Box”. I need to find out if the mesh is overlapping the bounding box, so that I can have the mesh scale down to keep it’s self inside the bounding box. Also, I need a way to do this without using collisions.

Any ideas?

Presumably, you are already using the Mesh API to implement your sculpting. If your bounding box is axis-aligned then it is quite easy to loop over the vertices array to find the ones that are outside the box (X, Y or Z coordinates will be outside the desired range). You can then just scale the whole model so that the vertex farthest from the centre lies within the box.

If your box is not axis-aligned, then things are much more difficult. I’m looking into producing some example code for non-AA boxes, but I can’t promise when this will be ready.

Ah! Thanks! Yes, the mesh is centered inside the bounding box. Thanks for your help! I’ll try it out, and see what happens.

``````if (verts[vs].x < -1 * msh.localScale.x * obtargt.localScale.x || verts[vs].x > 1 * msh.localScale.x * obtargt.localScale.x)
iscontained = false;
``````

msh is the transform of the mesh. obtargt is the transform of obtarg. obtarg is the bounidng box. verts are the vertices inside the mesh.

For what ever reason, this snippet of code is always cranking out false!

Any help?

I’m working on figuring out what’s going on, probably something obvious, knowing me…

I’m not sure if the JS operator precedences are actually documented anywhere, but this looks like it might be a precedence issue. I recommend you put the sub-expressions inside brackets just to be sure:-

``````if (verts[vs].x < (-1 * msh.localScale.x * obtargt.localScale.x) || verts[vs].x > (1 * msh.localScale.x * obtargt.localScale.x) )
iscontained = false;
``````

Thanks! I’ll try that.

Sadly, that didn’t work, however, this is working perfectly!

``````if (verts[vs].x < -1.1 / msh.localScale.x / obtargt.localScale.x /2 || verts[vs].x > 1.1 / msh.localScale.x / obtargt.localScale.x / 2)
``````