In the game, collision detection is often required. For example, to determine whether there are obstacles in the front and whether a bullet hits the plane, it is to detect whether two objects collide, then, different processing is performed based on the detection results.
Collision detection objects may have some shapes and complexities. These require combined collision detection, which means to combine complex objects into a combination of basic shapes and then perform different detection.
The following describes how to deal with the collision of the two most basic shapes.
1. collision between Rectangles and rectangles
Generally, an object collision rule can be processed as a rectangular collision. The principle is to check whether two rectangles overlap. Let's assume that the parameters of rectangle 1 are: the coordinates in the upper left corner are (x1, y1), the width is w1, and the height is h1. The parameters of rectangle 2 are: the coordinates in the upper left corner are (x2, y2), the width is w2, and the height is h2.
In terms of mathematics, the relationship between the distance and the width of the coordinates of the center point on the x and y directions can be processed. That is, the absolute value of the distance between the two rectangular centers in the x direction is less than or equal to 1/2 of the width and 1/2 of the rectangle, and the absolute value of the distance in the y direction is less than or equal to of the rectangle height and. The following is a mathematical expression:
X direction: | (x1 + w1/2)-(x2 + w2/2) | <| (w1 + w2)/2 |
Y direction: | (y1 + h1/2)-(y2 + h2/2) | <| (h1 + h2)/2 |
In the program, you only need to convert the above conditions into code.
But rectangular collision is just a rough collision detection method, because many actual objects may not be a regular rectangle.
Next we will introduce round collision.
2. Round and circular collision
The collision between a circle and a circle is the simplest collision, because in mathematics, there is a formula for calculating the distance between the two circles if they overlap. Then the condition is: calculate whether the distance between the two centers is smaller than the radius and of the two circles.
Assume that the coordinates in the upper left corner of circular 1 are (x1, y1), the radius is r1, the coordinates in the upper left corner of Circular 2 are (x2, y2), and the radius is r2.
Because there is no floating point number in MIDP1.0, and the floating point number operation is slow, we make a simple transformation of the condition: Square on both sides of the condition, in this way, the open operation step is removed.
The following is a mathematical expression:
(X1-x2) 2 + (y1-y2) 2 <(r1 + r2) 2
In the program, you only need to convert the above conditions into code.
The above is only the implementation of the most basic collision detection algorithm, and the collision detection problems encountered in the actual programming process are much more complicated than these, and other forms of detection are required, more in-depth learning is required.