- Home /
How would I cut geometry in real-time?
Ok lets say I had a piece of rectangular wood. What if I wanted to use an axe to cut the one piece into two parts at any place in the geometry into two semi-equal parts? Is there an easy way to accomplish this?
Answer by richyrich · Dec 11, 2014 at 09:25 PM
Edit: Re-read the question ;)
The way I have done something similar was to define all my cuboids by a start point and an end point both at the centre of each end. To split, all that is required is to identify the hit point on the collider, then find the projection of this point to an imaginary line that runs between the two points - this gives you the breaking point.
You can then centre the original wood piece (transform.position) on the midpoint between the start and the break point. You then retrieve from a pool of pre-created objects, another GameObject and centre that halfway between the breaking point and the end.
You then recreate your meshes with procedural edit code. Let use know if you need more info.
Quick answer: Yes it can be done
Answer by Habitablaba · Dec 11, 2014 at 09:40 PM
I'm doing something similar in my game. My idea was to find the split point and create a plane at that location with a normal along the mesh object's forward vector (you could use whichever one splits your mesh the way you want it.
Then, I loop through all of the vertices in sets of 3 (for triangles) and determine which side of the plane they are on. Triangles that split the plane are arbitrarily assigned to a side in this case. I didn't want to deal with splitting triangles and all that.
In the below code, I'm just getting the list of triangles for each mesh. Later, two new arrays of vertices are created from the lists of triangles and the original vertex array.
After that, I create the needed game objects and add all the components they need. Then assign the new meshes to the proper object.
Hope this helps!
var plane =
new Plane(this.transform.forward, pivotPoint);
for(int i = 0; i < mesh.triangles.Length; i+=3)
{
index1 = mesh.triangles[i];
index2 = mesh.triangles[i+1];
index3 = mesh.triangles[i+2];
vertA = (mesh.vertices[index1]);
vertB = (mesh.vertices[index2]);
vertC = (mesh.vertices[index3]);
if(!plane.GetSide(vertA) &&
!plane.GetSide(vertB) &&
!plane.GetSide(vertC))
{
triangles1.Add(index1);
triangles1.Add(index2);
triangles1.Add(index3);
}
else
{
triangles2.Add(index1);
triangles2.Add(index2);
triangles2.Add(index3);
}
}