- Home /
how much area colliding
hi everyone
my question: i have 2 objects and this two shape starting collide, this 2 object not have rigidbody. i want calculate how much nested this two object, like attached image:
Are you only dealing with cubes? if so, that is a straightforward geometry exercise.
$$anonymous$$ore complicated meshes will make things a bit different. What are you trying to do with this information?
Like TreyH said it highly depends on your specifications:
Do you always just need to check two objects?
Are they always cubes?
Do they always have the same size?
Are they always axis-aligned?
If you answer any of those questions with "no" it gets much more complex / complicated. If you can say "yes" to all it's fairly easy.
yes, always check 2 object
no, not always cube, different shapes and meshes
no, not have always same size
no, not always axis-aligned
attached some other examples thinks,
red shapes are base shapes, how much area colliding in red shape
(image have 2 example):
(thanks for answers)
Answer by Bunny83 · Nov 29, 2016 at 02:06 AM
Sorry for the late response ^^. I currently have about 150 UnityAnswers tabs open.
Well as i said having arbitrary meshes makes it way more complicated. Especially since the right "red" object is not convex. If you only deal with convex objects it's easier. So if you really need the general case there is one initial requirement: the meshes need to be closed otherwise the concept of "inside" and "outside" makes no sense. So if you have two arbitrary closed meshes you have to do;
split each mesh into one or multiple convex meshes. Every "closed" mesh can be broken down into convex meshes but it might not be that simple to do so. The result of the first step is that the initial Mesh A and Mesh B has been broken down into A1, A2, ... and B1, B2, ... where each one is a convex mesh.
In the second step you would convert each face of lets say all A meshes into mathematical planes that can be used to intersect the meshes with each other.
Now you have to cut down all B meshes by all planes of each A mesh. The resulting convex mesh is the intersection mesh between the two convex meshes. So when you intersect n x A meshes with m x B meshes you will end up with (n * m) x C meshes. However some of these meshes will be "empty" and can be discarded.
In the final step you have a collection of all intersection volumes as convex meshes. Now you just need to calculate the volume of those meshes. Since they are all convex you can simply split it logically into tetrahedron. From there it's easy to calculate the volume of each part. Finally just add up the volume of each part.
If you want a "percentage" in relation to one of the two initial meshes you would need to calculate the volume of that reference mesh as well. Since you already have the mesh as convex parts that's fairly easy.
If you have problems with the implementation, here's my VisPortal demo. You can use the Polygon class inside PolyArea.cs to represent a single "face" / triangle of your mesh. It has a clipping method to clip it against some clipping planes. The great thing about convex meshes is that when you intersect all border planes with each other you get the exact faces of the mesh.
As i said this is not going to be easy. This is actually the same what is needed for calculating CSG operations on meshes.
Your answer
Follow this Question
Related Questions
spheres not colliding with world 1 Answer
nested if statements doubt 2 Answers
Error with nested coroutines 0 Answers