primers
Dynamic CollisionDetection was introduced in CollisionDetection: Transformation, so much of the CollisionDetection project is covered up to this point. If you look up data, you may see other methods of detection. Now let’s look at another method: Separating Axis Theorem.
- Origin
- My GitHub
Related knowledge points
Vectors and scalars
To put it simply:
- A vector, “vector,” also called a vector, is a quantity that has magnitude and direction, such as acceleration and force.
- A scalar has only a quantity of magnitude, such as time or temperature.
In geometry, vectors are represented by directed line segments as follows:
Vector V calculation method:
- V = C2 – C1
- V = (7-3, 7, 2)
- V = (4, 5)
Normal vector: The perpendicular vector of a vector, swapping the x and y components, and then inverting the x component of the coordinate. The normal vector to V up here is minus 5,4.
Dot product and projection
The dot product
Two vectors can be multiplied by Dot Product, and the result is a scalar quantity. The expression is A · B.
The dot product can be calculated in two ways:
Methods a
A dot B is equal to Ax times Bx plus Ay times ByCopy the code
Way 2
A. B = | | | | * B * cos (theta)Copy the code
- | | is A vector quantity
- | | B is A vector quantity
- Theta is the Angle between vectors A and B
The other thing you need to know is the concept of a unit vector, and the way you compute a unit vector is by dividing a vector by itself.
A / |A|
Copy the code
See here for more information.
projection
For Projection, see the figure below:
Imagine a light source shining parallel rays on an object, creating shadows on a surface. This shadow is a two-dimensional projection of a THREE-DIMENSIONAL object.
Similarly, a projection of a two-dimensional object is a one-dimensional “shadow”.
The dot product and the projection
The dot product gives the projection of one vector onto another. And that’s just a simple derivation.
As shown in the figure above, the projection scalar of V on W is denoted as Pw(V), and it can be known that:
Pw (V) = | V | * cos (theta)Copy the code
According to the dot product calculation method:
V. W = | | | | V * W * cos (theta) V * (W/W | | = | V | * cos (theta)Copy the code
Therefore, it can be concluded that:
Pw (V) = | V | * cos (theta) = V * (W/W | |)Copy the code
polygon
Convex polygon
When a straight line passes through a Polygon, the Polygon is to Convex Polygon if it intersects the Polygon no more than twice.
Concave polygon
When a straight line passes through a Polygon, if the line intersects the Polygon more than twice, the Polygon is Concave Polygon.
Separating Axis Theorem
Developed by Hermann Minkowski, Separating Axis Theorem is used to solve the problem of convex polygon collisions. It states:
If there is an axis on which the projections of two convex objects do not overlap, then the two convex objects do not overlap.
This axis is called a split axis. Let’s talk more about that. The axial theory is referred to as SAT below.
There is no overlap
In the figure above, you can see that the projections do not overlap. According to SAT, the two shapes do not overlap.
When SAT is being tested, it may need to test many axes, but if the projection on one axis does not overlap, the test can be stopped. Because of this, the SAT is ideal for applications (games, simulations, and so on) where there are many objects but few collisions.
overlap
If the projections of shapes overlap on all of the sub-axes, then we can be sure that the shapes overlap. The following is an example:
Algorithm implementation
With the above principle, the following problems need to be considered when converting into an algorithm:
- How do I get all the potential sub-axes?
- What is the basis of projection overlap?
Question 1
The answer to the first question, based on research, is that in 2D, all the potential splines are normals to each edge of the shape.
A normal is simply a normal vector with no direction. This is described in the previous knowledge points. Here is an approximate logical implementation:
const vertices = [] // Set of coordinates for vertices, assuming values already exist
const axes = [] // Store the splitter
const verticesLen = vertices.length;
for (let i = 0; i < verticesLen; i++) {
const p1 = vertices[i];
const p2 = vertices[i + 1 == vertices.length ? 0 : i + 1];
// Find a vector algebraic representation of each edge. Step 27 Subtract p1 from P2
const edge = subtract(p1,p2);
(x, y) => (-y, x) or (y, -x)
const normal = normalAxes(edge);
axes.push(normal);
}
Copy the code
Question 2
In the above introduction to SAT, it can be clearly observed in the diagram that in the algorithm implementation, all vertices and sub-axes of the shape need to be traversed to perform the dot product, and the minimum and maximum values can be obtained by comparison. Then mark the minimum and maximum values roughly on an axis to see if there is any overlap.
Here is an approximate logical implementation:
Suppose we have polygons A and B.
const verticesA = []; // Set of all vertex coordinates of A shape
const verticesB = []; // Set of coordinates for all vertices of the B shape
const axes = [] // Store all the subaxes obtained
const axesLen = axes.length;
for (let i = 0; i < axesLen; i++) {
const separateAxes = axes[i];
// The getProject method gets the maximum and minimum values of the projection
const projectA = getProject(separateAxes,verticesA);
const aMin = projectA.min;
const aMax = projectA.max;
const projectB = getProject(separateAxes,verticesB);
const bMin = projectB.min;
const bMax = projectB.max;
// The projection overlaps.
if ( (aMin <= bMax && aMin >= bMin) || (bMin <= aMax && bMin >= aMin) ) {
continue;
} else {
return false; }}Copy the code
validation
According to the above idea, take the upper left corner of the web page as the origin of coordinates, the horizontal left as the X-axis, and the vertical down as the Y-axis. Describe coordinate points in terms of CSS units.
This is the test page, the mobile terminal is as follows:
In the above test page, unoverlapped projection data is taken as an example, and the detected data is projected onto an axis:
You can see there’s no overlap.
The resources
- Separating Axis Theorem
- SAT (Separating Axis Theorem)
- Collision Detection and Response
- Collision Detection Using the Separating Axis Theorem
- Dot product and vector projections
One cannot fully understand another.