Reference Source Address http://xxxxxfsadf.iteye.com/blog/540669
In the flying shooter game, most of our planes are triangular, and we can use triangles as approximate bounding boxes. Now we assume that the plane is a positive triangle (or other triangles, I think if the plane is designed to the right and left asymmetrical monsters, then his aesthetic must have a problem), my plane is a positive, upward fly triangle, the enemy plane is inverted, fly down the triangle, And the plane doesn't spin (this is the case in most games). We can define airplanes like this: Center point O (xo,yo), three vertices P0 (x0,y0), P1 (x1,y1), P2 (X2,y2). The center point is the center point of the positive triangle, that is, the distance between the center point and the three vertices is equal. The next question is how to make sure that the two triangles interfere with each other? Well, now we're in touch with the substance of the problem. If you have studied plane analytic geometry, I believe you can come up with many ways to solve this problem. Judging whether each vertex of a triangle is inside another triangle looks like a good way to do this, but I find a small problem: the vertex of a triangle is not in the other triangle, but it may collide, because the vertex of the other triangle is inside the triangle, So to judge two times, this is very troublesome. Is there a way to judge? We put the triangle in the polar plane, the center point is the origin point, the horizontal line is the x-axis zero angle. We found that the triangle formed this way: at each angle we could find a distance to describe the edges of the triangle. Now that we have found the distance from the edge to the center point, we can use this distance to detect collisions. The coordinates of the center point of the two triangles are (xo,yo) and (XO1,YO1) respectively, the coordinates of the two points are calculated from the two point distance and the two-point line and the angle θ of the x-axis, then the distance between the center point line and the triangle edge is calculated by θ, and compared with the distance from the two center point. Thus judging whether the two or three angle is colliding. Because the triangles are symmetrical around each other, θ takes-90~90度 intervals. Ha, now the problem is much more interesting,-90~90度 interval is the definition of the tangent function field, to find the corresponding edge to the center of the distance is much easier, using geometric knowledge, two, the triangle is divided into three parts, that is, figure 2 red and green blue three parts, according to θ in that part and respectively treated. The distance from the edge to the center point is calculated by the sine theorem, which is the length of the light green segment in Figure 2. However, the efficiency is still low if the aircraft is judged this way every time it moves. We can combine the radius method to solve, first use the RADIUS method to determine whether a collision, if possible collision, and then use the above method to determine exactly whether the collision is true, so basic can be. What if the plane spins, for example, three of the planes rotate an angle α, looking closely at Figure III, you can find the distance from the edge to the center point by using (θ-α), then you should pay attention to the boundary situation, that is (θ-α) may be greater than 90 degrees or less than 90 degrees。 So wordy said so much, I don't know if you understand. I have written a simple routine to illustrate my intentions. In the example, assume that all planes are the same size and do not rotate.
Collision algorithms in flight games-bounding box collision detection