- Home /
How to cut an object in any direction
Hello. I have a question about Unity2D. English is not my native language so please forgive my poor vocabulary.
On Unity2D, I can't find how to cut an object in any direction and divide it into two objects.
As shown above, start moving your finger or player character in any direction from the START point. ObjectA is not divided at this point. When a player finally reaches the END point, your finger or player character draws a trace like a dotted line as a result. As soon as the END point is reached, ObjectA is destroyed and ObjectB and ObjectC are created. (Note: The gap between B and C does not actually exist.)
How do I do this? That is the specific question content. I could find some ways to cut sprites in a straight line, but I can not think of how to apply them to cut them in a curve like this.
In addition, I want to add different operations to ObjectB and ObjectC after cutting, so it would be very helpful if you could give me a rough procedure for finding these two objects. Thank you.
Answer by Pakillottk · Aug 05, 2019 at 08:19 PM
Hi. Well I don't think there is an easy way to acheive that. First we have to make clear what kind of geometry you'll be cutting. If we are talking about 2D rectangles, like the one you are showing in the example above, or more complex objects (like a star or even a humanoid type figure).
In any case, as far as I know there's mainly two ways to do these kind of things:
Make some variations of cuts for the predefined models that you can cut and just instantiate the prebuilt broken pieces. If you really want to fine tune the cuts with an user-defined path this approach won't work, because it would be impossible to match those shapes in a believable way...
The hard way... You compute the new geometry "by hand". In your case, what I'm seeing that could work: I'm going to assume that the path that the user makes it's always an open polyline with no self-intersections. The approach will be: iterate over all the vertices of the object, find out which are on the left/right of that polyline, then you'll have two sets of vertices: left side + user-defined polyline and right side + user-defined polyline. (Now the tricky part...) Once you have the two sets, you have to generate a new set of faces, so you'll need to retriangulate the polygons yourself at runtime. Furthermore, you'll have polygons that won't be convex at all so the triangulation part will be even more tricky. Then with the vertices + faces you can just make two new Meshes and voilá... You'll have the perfect cut.
As challenging as it may sound, I think that it's doable (assuming that you'll have "light" geometry to cut, with not many vertices).
Hello Pakillottk. It turns very clear that what I have to do now. I will study how to distinguish the right and left side by the polyline first . Yes, you are right...It will be more difficult way because I’m a beginner of program$$anonymous$$g and Unity. I guess I will spend all my summer vacation. I’ll try hard. Thank you for your answer!
For getting the side, this will help you: https://www.geeksforgeeks.org/orientation-3-ordered-points/
Basically if one vertex of the object is in the left/right, all the triangles of the form: vertex, polyline-vertex-i, polyline-vertex-i+1 will have the same orientation.
These kind of things get tricky when you have points colinear to the polyline. So you have to remember to check first if the vertex of the object it's in the segment of the polyline.
Answer by nihonjindayo · Aug 07, 2019 at 07:15 AM
Thank you so much! That's interesting. This article will definitely help to solve my problem.
Your answer
Follow this Question
Related Questions
Can't get more than one object. 1 Answer
Object carry in game 1 Answer
move object when player reach trigger 0 Answers
I can see objects through other ones. 0 Answers
Unity2D Object edge with a darker color 0 Answers