Introduction
This article describes how to perform precise and efficient Collision Detection in 2D action games. Here, the collision is based on Polygon rather than on the genie. The two are designed differently. The collision detection based on the genie is completed by overlapping pixels between the elves. The polygon uses vector mathematics to accurately calculate the intersection, time, and collision direction. Although polygon is only an approximation of the genie, it is more advanced than the genie system.
- It can accurately simulate realistic simple physics, such as rebound, friction, and slope sliding.
- Collision detection can be used more accurately in High-Speed genie systems. In an genie-based system, if an object moves too fast, it will skip another object.
- Vector-based mathematics can be extended to 3D, but the sprite collision system is strictly limited to 2D.
Features
The algorithm used in this article is only applicable to convex polygon, such as triangles, quadrilateral, hexagonal, and circular shapes. For non-convex polygon, you can break it into multiple convex polygon, such as a triangle. The algorithm can be used to move a polygon quickly or slowly. Collision will not be lost no matter how fast the object moves. It can also deal with overlapping issues and promote the separation of overlapping objects. The demonstration also supports splitting polygon crossover. This can be used for bullet modeling. It also provides a simple object system, elasticity, basic friction and static friction. Used to ensure that the object does not slide from the slope.
There is an example of a rigid body system that uses the chrsi Hecker physical tutorial.
Restrictions
Ordered collision. That is to say, it is not an orderly collision. This may cause problems for fast moving objects. Once a collision is detected, it is processed directly. Ideally, you may need to find a collision point and handle it, and then look for more collisions. But for 2D action games, this is usually unnecessary.
I. Axis Separation MethodThis method is the core of collision detection. Its rules are very simple and easy to implement. This method is also very fast and reliable, because Division operations are not used in computing. The following is a simple Collision Detection example based on two boxes.
The algorithm tries to find a proper plane between two objects. If the plane exists, the objects do not have to be intersecting. To test whether an object is separated, a simple method is to project the object to the normal line of the plane, and compare the distance between the two to see whether the two overlap.
Obviously there are countless planes that can be used to separate two objects. However, it has been proved that you only need to use a part of the plane for testing. For the box, we can see that the normal of the plane is the long axis of Box B.
For the box, the split plane to be tested is the axial plane whose normal is equal to the two boxes. Therefore, for two boxes, you only need to test four split planes. In these four planes, once a split plane is found to split the box, you can conclude that the two boxes are not intersecting. If the four planes cannot separate the boxes, the two boxes must be at the same intersection, that is, a collision occurs. This algorithm can be extended to a common polygon. The algorithm is the same and the number of planes to be tested is changed. The split plane has a normal line in the vertical direction of each polygon edge. In, you can see two split planes used for testing. On the red plane, you can see that the two intervals overlap. However, the intervals on the blue plane do not overlap. Therefore, the blue plane splits the plane and therefore the objects do not overlap.
Now, we have an algorithm to check whether two polygon are intersecting. The code can be divided into three parts:
A) Generate the Separation Axis to be tested
B) Calculate the projection of each polygon on the Separation Axis method.
C) Checks whether these projections are intersecting.
Bool intersect (polygon A, polygon B) { For (I = 0; I <A. num_edges; I ++) { Vector n = vector (-A. edgedir [I]. Y, A. edgedir [I]. X ); If (axisseparatepolygons (n, a, B )) Return false; } For (I = 0; I <B. num_edges; I ++) { Vector n = vector (-B. edgedir [I]. Y, B. edgedir [I]. X ); If (axisseparatepolygons (n, a, B )) Return false; } Return true; } |
Void calculateinterval (vector axis, polygon P, float & min, float & MAX) { Float d = Axis dot P. vertex [0]; // compute the vector from the coordinate origin Min = max = D; For (I = 0; I <p. num_vertices; I ++) { Float d = P. vertex [I] dot axis; If (d <min) Min = D; Else If (D> MAX) Max = D; } } |
The algorithm detects collision between 2D polygon. This algorithm is extremely fast and applicable. The edge direction does not require unitization. Therefore, you can avoid storing the edge direction and directly obtain the edge direction through the vertices of the polygon.
For (j = A. num_vertices-1, I = 0; I <A. num_vertices; j = I, I ++) { Vector E = A. vertex [I]-A. vertex [J]; Vector n = vector (-E. Y, E. X ); If (axisseparatepolygons (n, a, B )) Return false; } |