Hello.
Here’s a difficult programming problem, at least for me.
I have a set of 3D objects/meshes that form a map.
To detect when the player gets out of this map, I created a box trigger collider all around the map.
I would like this process of creating the box to be automatic, the script should create the smallest possible box around the objects. The box can be obviously rotated as needed, so it’s not just “take the smallest and the biggest x,y and z values”.
Does anyone have an algorhythm to do this? Thank you.
Look at the Bounds class. It sounds like what you need. Grab all the renderers of the objects, create a bounds object and call Encapsulate on all of their bounding boxes.
It is.
For static mesh (given desired box transformation matrix):
Transform vertices into world space using mesh object’s localToWorldMatrix, then transform them into box space using box space using worldToLocal matrix, then proceed as usual by picking minimum/maximum. You can create box collider using values you’ll get from that process and matrix for the box.
If you want OPTIMALLY aligned box (optimal box-shaped bound rotated in such way so the amount of empty space is minimized), then you’re talking about OBB s (object oriented bounding box) and will need to dig through couple of siggraf papers. I think there should be something helpful on the subject in some papers that discuss OBB trees (maybe in papers discussed OPCODE library), but I don’t remember anything specific at the moment.
As stated from the documentation, if I understand well, karl’s and neginfinity’s solutions are similar, and allow me to create a box that is aligned to world matrix.
I’m dealing with a track in a race, so imagine it like a snake, very long. What if it goes in diagonal or makes long curves and highs and lows? A long diagonal snake would be encapsulated in a gigantic cube, while a diagonal box would be much closer to the actual shape of the track.
A “optimally” aligned box sounds like a very interesting but hard mathematical problem. If there is no intermediate solution, that approximates the optimal solution, I could use the Bounds/Encapsulate system, iterating it one dozen of times on different angles until I find the smallest box. Not perfect but enough for me.
At this point a new question arises: how do I retrive the bounds of the objects and create a boundary, relative to an arbitrary matrix?
Thanks again