1

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

  • 1
    Are the edges of one cube in the same direction as those of the other, as per your image? That's a very easy special case. Or if you need the general case, would Monte Carlo be fast enough for your purposes? – J.G. Oct 10 '21 at 15:45
  • @J.G. All the cubes are aligned to the axis, so YES the edges of one cube are in the same direction as those of the other. Having established this:
    1. what is "Monte Carlo"? 2. Is it valid also in my axis aligned case?
    – Enrico Gliaschera Oct 10 '21 at 15:51
  • 1
    In that case all you need is this. Monte Carlo would address this case, as well as others, but it's slow. Basically, you assume the proportion of cube 1 inside cube 2 is the proportion of random points from cube 1 that are in cube 2. – J.G. Oct 10 '21 at 15:58

1 Answers1

2

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.

Asinomás
  • 105,651