I’m looking for a compact, processor-light solution for the following scenario:
Given a group of (up to 32) cubes with random dimensions, locations, and orientations, what is the smallest bounding cube that can contain them all? The containing cube must be oriented 0,0,0 in world space. In case it impacts a possible solution, the cubes are all nested under one parent object.
An obscure request, but I’m hoping some genius math-head/C#-head here might have a relatively easy calculation to go by. The calculation needs to very quick, as it’s called multiple times per FixedUpdate().
For each cube, get the vertices and convert to world space.
Store all vertices in one collection
Loop through vertices and keep track of the min and max x,y,z values for each one
Use min and max values to compute the bounding cube
Hopefully this will be fast enough. Time will tell once all future logic is in place.
Instead of going through all the vertices, you can actually get access to the bounding box of each individual object with GetComponent().bounds.
Mesh.bounds has already had to go through the vertices to figure out it’s own values, so there’s no point in you doubling up efforts.
So, instead, loop through all of the objects, check their bounds.center + or - bounds.extents for each axis to quickly figure out which objects have the furthest bounds in each direction and there, you have the +x, -x, +y, -y, +z, -z to define your overall bounding box.