Unity realizes 2D ground digging! Paint terrain (collision part, method 1)

The overall effect can be seen here: https://blog.csdn.net/ww1351646544/article/details/106410601
Insert picture description here

The Edge Collider 2D used
Insert picture description here
in this case is implemented. Under the EdgeCollider2D component, the Points vertices can be given. The EdgeCollider2D component will string together all the vertices in sequence with line segments. These line segments are the collision range.

When initializing, put this:
1. Give the position of the four boundary points.
Insert picture description here
2. Because of the calculation needs, each side needs to be divided into multiple line segments.
Insert picture description here
Third, the next step is more critical. When you click on the red position, 36 virtual points are created at a certain distance around the center of the circle, one every 10 degrees. (Don't create these red and yellow dots)
Insert picture description here
Fourth, find the first valid line segment 1 (not in the yellow circle, as shown in the blue dot). And use to make the first point of the circled point (as shown in the red dot) point to it, and all the circled points, the red and yellow points are also rotated.
Insert picture description here
4. Determine which of the yellow dots are within the range of the polygon.
Determine which of the green dots are within the yellow circle. (The green dots in the yellow circle and the yellow dots outside the green frame are gray because they are waste points)
Insert picture description here
5. Then find where the green line segment starts to have a waste point, insert the valid point in the yellow circle, and then connect the valid point behind .
Insert picture description here
6. Record each time the distance must be moved more than a certain distance, at least the distance of each point, and then do the next calculation, otherwise it is easy to cause problems.
7. As you keep digging, you need to dig through. The situation is as shown in Figure
Insert picture description here
8. At this time, you need to divide the situation. Below I will call the original green dot line segment and the yellow part circle. They are divided into 3 segments, as shown in the figure. Shown.
Below you can clearly see that the continuous line segment from the first point is cut into three segments, and the circle is cut into two segments.
Whether it is cut can be judged by whether the point index is continuous.
Insert picture description here
9. Use the red block as shown in the figure above: Line 1 ==> Circle 1 ==> Line 3
and then create an EdgeCollider2D to give a fixed-point blue block: Circle 2 ==> The
most image of line 2 gets the following effect
Insert picture description here
. Have you dug it through? Probably yes, but things are often not that simple. For example, the following situation.
A point of the circle is out, the circle is cut into two segments but the line segment is not cut.
Therefore, it is judged that the line is cut into 3 sections, and the circle cuts two sections.
Insert picture description here
11. The following situation is also the case of 3 segments and 2 segments of the line, but he does not need to cut two pieces.
Insert picture description here
12. We can judge by judging the distance between the last point of circle 1 and the first point of line 2. If the distance is very close, it is the case of Fig. 11, and if the distance is larger, the case of Fig. 8 needs to be cut. open.
13. The following explains how to sit in the middle and start digging. When the mouse clicks on the middle position, we record the point the mouse passes.
The red dot is shown in this way. Of course, the red dot must be kept at a certain distance.
Insert picture description here
14. When the circle has been dug to the edge, it is really dug through.
At this time, we need to have a sort , find which point is closest to the edge point of digging, and then simulating the mouse clicking on that point will execute the content of the previous twelve steps and dig it.
Insert picture description here
Fifteen. Recirculation centered on the first red dot, then sort all the intermediate points to find the nearest, and then perform the first twelve steps to dig.
This also solves the problem of center mining.

Is there a case where the thread is cut into 4 or more segments and the loop is cut into 3 or more segments? It does not exist when the size of the circle does not change.
The above method does have his lack of rigor, but it is basically sufficient in some of my own projects, and I share it with you, hoping to help.

However, because of higher project requirements later, the project was finally realized in a more precise and rigorous way. I will share it with you next time, thank you.

Guess you like

Origin blog.csdn.net/ww1351646544/article/details/106437063