How can I find the volume of the intersection of two cubes aligned to the axis? The cubes can have different sizes and positions. (I add a picture to show a simple example case of two cubes)
After deep research, I am quite sure that does not exist a specific function in Unity to use for this purpose, the only way to work out this problem is mathematical logic. For example, my first idea was:
1)To find the 8 vertexes of the cube"intersection" (B in image).
2)Try to build a new cube with this vertex.
3)Find the size and volume of cube "intersection".
Unity permits to find:
A) the centre of each primary cube (A in the image) with "Bounds.centre";
B) the extents of each primary cube (A in the image) from the centre of the cube (half of the size of the Bounds) with "Bounds.extents".
documentation avaible here:https://docs.unity3d.com/ScriptReference/Bounds.html
However, I can't find a way to have the vertex of each cube. And, secondly, a logical function that can find what 8 of 16 vertexes found are the right vertex to use to build the cube"Intersection" from their coordinates.
Have u got any help or suggest?
Image: enter image description here
Every prism $a$ paralel to the axis is characterized by three intervals, one in each coordinate, let them be $a_x,a_y$ and $a_z$. The interval $a_x$ corresponds to the values of $x$ that the points in the prism can take.
If you intersect $a$ with another prism $b$ that corresponds to the intervals $b_x,b_y,b_z$ then you get a prism, the prism that corresponds to the intervals $a_x\cap b_x, a_y\cap b_y, a_z\cap b_z$.
So you just need to calculate the lengths of those intervals and multiply them together.