How to perform collision detection on a 2D rotating rectangle, you can use a detection algorithm called OBB (oriented bounding box) to enclose the box in the direction. The algorithm is based on the SAT (separating axis theorem) separation axis law. The OBB is not just a collision detection of a rectangle, but an algorithm model. Simply explain the concept, bounding box and separation axis law.
Bounding box: the size and direction of the box is determined by the set shape of the object, so that the most compact box can be selected to represent the object. See figure below
Black is the bounding box, can be a convex polygon, the most close to the detection of objects can be.
Separation axis law: Two convex polygon objects, if we can find an axis, so that two objects on the axis of the projection does not overlap, then there is no collision between the two objects, the axis is separating axis
Then use the general to detect those axes, perpendicular to the polygon each side of the axis. The following figure:
Therefore, the separation axis law becomes, the projection of two polygons on all axes overlap, it is judged as collision; otherwise, no collisions occur.
Below, I only consider the case of the rectangle, how to detect the split axis.
Obviously, the rectangle has 4 edges, there are 4 inspection axes, then 2 rectangles have 8. But the rectangle has 2 axes which are duplicated, so only 2 axes are detected, which is the axis of the two perpendicular sides of the rectangle.
As above, judging the collision, we need to determine whether the projection of the 2 rectangles on the 4 axes overlaps. There are 2 possible ways to do this. First, the 4 vertices of each rectangle are projected onto an axis, so that the maximum line distance of 4 vertices is calculated, and the second rectangle is treated later, and the 2 rectangular projection distances are determined to overlap.
In the second way, the radius distance of 2 rectangles is projected onto the axis, and then the center line of the 2 rectangles is projected onto the axis, then the center line projection of 2 rectangles and the sum of the radius projections of 2 rectangles are judged. This article uses this approach.
Here is a mathematical knowledge of some vectors. The following figure:
The P point is the projection point of the rectangle on the x-axis, and the projection point of the rectangle on the vertical axis is the origin point. As can be seen here, point P is located on the rectangular axis, the projection length on the X-axis is op, if the rectangle counterclockwise detour point O Rotation, op on the x-axis of the projection length is smaller, until the 0,op perpendicular to the x-axis. That is, the maximum and minimum value of the projection length of the OP on the x-axis. This also explains why we choose to detect axes that are perpendicular to the polygon edges, because we can get the extrema on these axes, and the middle ones don't have to be tested.
How to represent the axes, we need to use vectors, correct using the unit vector, can simplify the model, reduce the difficulty of thinking. The following figure:
Assuming that the coordinates of P point are (px, py), then the vector p is (px, py), point P on the x-axis projection point Q coordinate is (QX, qy), then the vector q is (QX, qy). We assume that the unit vectors on the x-axis are (1, 0). Then the vector p and x axes are multiplied by the unit vector points:
Vector p * x-axis unit vector = | p| * | X-Axis Unit vector | * COSPQ = px * 1 + py * 0 = px
And because the length of the unit vector is equal to 1, the PX is the length of the vector Q. This is very meaningful, we get a rule, is to put a vector point by a unit vector, we get the vector on this unit vector projection length. Expressed in code as: [CPP] view plain Copy/** * dot-multiply */private float dot (float[] axisa, float[] axisb) { Return Math.Abs (axisa[0] * axisb[0] + axisa[1] * axisb[1]); }
Here float[] stores the x, y coordinates of a point. The AXISB is the unit vector, and the result is the length of the projection on the Axisa vector, the unit vector AXISB.
Let's take a look at how the unit vectors are represented:
The unit vector is described by a unit circle. Assuming that the radius of the circle is 1, then any vector on the circle to the origin can be regarded as a unit vector, and the length is 1. So, the obvious point P is a unit vector. The point P moves on the unit circle, then the unit vector is rotated, and the vector p establishes a relationship with the angle A. It is obvious that CosA is the x-coordinate of the vector p, and the SinA is the y-coordinate of the vector p.
So we can conclude that the unit vector p is (Cosa,sina). The meaning of this model is that the unit vector P can be regarded as the edge of a rectangle. The following figure:
So how is the unit vector s of the other side of the rectangle represented, and the vector s and the vector p are perpendicular, we can conclude that s (-sina, CosA), vector s dot multiply vector p = 0
At this point, we can get a unit vector of 2 detection axes of a rectangle by a rotation angle. The code is as follows: [CPP] view Plain copy//unit vector of X axis private float[] AxisX; Unit vector of Y axis private float[] Axisy; 0-360 private float rotation; /** * Set axis x and y by rotation * * @param rotation float 0-360