GJK algorithm, collision detection (self-study notes, infringement deleted)

Learn 哔哩哔哩 "Seemingly simple complex problems, strange and elegant solutions (GJK algorithm) |

 

1. The basic concepts of convex and concave shapes

All graphics can be divided into two types: convex and concave, as shown in Figure 1.1.

Figure 1.1 Convex and concave shapes

 The properties of a convex shape are:

  1. The line connecting any two points on the shape must be inside the shape, which is easier to handle than a concave shape.
  2. For every point on its shape, there must be a direction such that the TV is the farthest point in this direction. That is, traversing all possible directions on the shape and finding the farthest point in the direction will inevitably get every point on the shape.

The concave shape does not obey the above characteristics, so when dealing with the concave shape, it can be divided into multiple convex shapes to simplify the calculation, so all the intersection judgments between shapes can be transformed into the intersection problem of convex shapes.

Figure 1.2 The concave shape is transformed into multiple convex shapes

2. The relevant characteristics of the intersection of two shapes

The reason why the concept of "origin" appears in the judgment of the intersection of two shapes is shown in Figure 2.1. When two convex shapes intersect, there must be two points (vectors) whose difference is the origin, and this also proves that there is an intersection between the two.

Figure 2.1 The relationship between the intersection of two shapes and the origin

The above-mentioned "origin" solution method further proposes the concept of Minkowski sums/differences (Minkowski Sums/Differences).

2.1 Minkowski sum/difference

Minkowski sum: Adds all points inside one shape to all points inside another shape. And each point on each shape is treated as a vector starting from the origin.

Minkowski difference: subtracts all points inside one shape from all points inside another shape.

Figure 2.2 Minkowski and

Figure 2.3 Minkowski difference

The Minkowski difference also has the following two properties:

  1. The shape obtained by the Minkowski difference of two convex shapes is also convex; according to the nature of the convex shape, this means that the line of any two points must exist in the Minkowski difference;
  2. When two shapes intersect, their Minkowski difference must contain the origin, that is, at least the two shapes have at least one point in common (the same principle as in Figure 3);

 Therefore, the problem of judging the intersection of two shapes becomes a problem of judging whether the Minkowski difference of two shapes includes the origin.

Select the difference of three points in two convex shapes A and B to form a triangle (these three sides must be in the Minkowski difference); when the origin is included in the triangle, it means that the origin must also exist in the Minkowski difference, and then It can be judged that shapes A and B intersect.

Figure 2.4 The origin of the triangle contains the intersection relationship with the convex shapes A and B

Therefore, the problem of judging the intersection of two shapes can be further simplified as whether three points can be found in the Minkowski difference of convex shapes A and B to form a triangle near the origin (that is, a triangle containing the origin), as shown in Figure 2.5 . This triangle is called a simplex.

Figure 2.5 Simplification of intersection and triangle problems

2.2 Single row (simplex)

In different dimensions, the shape of a single row is not the same. In 2D, a single row is a triangle; in 3D, a single row is a tetrahedron.

Figure 2.6 Introduction of a single row

2.3 Support Function (support function)

According to the characteristic 2 of the convex shape, the direction can be mapped to the point on the convex shape, and the function that maps the direction vector to the farthest point on the shape is called the support function, and the corresponding point is called the support point.

The very interesting point of this support function is that the addition of two convex support functions can get the support function and support point of its Minkowski sum.

Figure 2.7 The relationship between convex support function, support point and Minkowski sum support function, support point

 When facing the Minkowski difference, specify the vector direction of the first convex shape A to find its support point; then use the opposite direction of this direction as the direction on the second convex shape B to find the support point on the convex shape B . The subtraction of these two points is the support point on the Minkowski difference boundary. This is useful for finding triangles containing the origin in the Minkowski difference.

Figure 2.8 The relationship between the convex support point and the support support point of the Minkowski difference

 Interestingly, by observing Figure 2.7-2.8, it can be found that when finding the supporting points of the Minkowski sum, the directions selected on the convex shapes A and B are the same, and when finding the supporting points of the Minkowski difference, the convex shape A , The direction selected on B is horizontally opposite. The source of this difference has to do with the definition of Minkowski and \poor itself.

Calculation of Support Function: 

The Support Function returns the furthest point v in the direction d specified by the convex edge. As shown in the formula in Figure 2.9, the dot product can measure the directional similarity between two vectors. The more similar the directions of the two vectors are, the higher the value of the dot product will be; and the farther point in one direction will have Higher dot product. This property makes it possible to solve the support points.

Figure 2.9 Calculation of Support Function

3. GJK algorithm and its implementation details

3.1 GJK algorithm

1. First, select a random direction and find the support point in this direction, which is the first point of the simplex.

2. Take the current support point as the starting point of the vector, find the vector pointing to the origin as the new direction d, and find the second support point.

3. Determine whether the dot product result of the second support point and the current direction d is less than 0. Through the nature of the dot product, we can know that the dot product of the two required vectors is not less than 0, and the angle between the two vectors is [0°, 90°]. If the two shapes are to intersect, the angle between the vector formed by the origin and the second support point and the current direction d is [0°, 90° ], and the reasonable range of the position of the second support point is shown in the blue area in Figure 3.0.

        1) If the dot product of the second support point and the direction d is less than 0, it means that there is no support point that can cross the origin in the current iteration direction, that is, the second support point does not fall in the blue area of ​​Figure 3.0 , the two shapes do not intersect, exit the GJK algorithm.

        2) On the contrary, add the current second support point to the simplex.

4. If the second support point is reasonable, then the two support points are connected into a straight line, and the vector perpendicular to the line is the new direction d, and the new support point is calculated to form a triangle.

5. Check if the current triangle contains the origin. If so, the two shapes intersect; otherwise update the orientation, adding a new support point. Select the vertical vector of the triangle side whose distance is closest to the origin and keep two points on this side, delete the remaining one point, and use this vertical vector as the new direction d to find the third support point again, do the next time Iteration and judgment.

Figure 3.0 The reasonable area of ​​the second support point that can cross the origin (blue part)

 

3.2 Implementation Details 

question:

  1. How do you know if a point on the Minkowski difference crosses the origin?
  2. How do we choose a new direction when we have two points?
  3. How can we detect if the currently formed triangle contains the origin?
  4. How do we choose the next direction if the current triangle does not contain the origin

Question 1: How to know whether a point on the Minkowski difference crosses the origin?

Check whether a point A passes through the origin in the manner shown in Figure 3.1. From the origin point A is regarded as a vector, judged by the dot product between the vector and the direction d, when the dot product is negative, the current point does not pass the origin; otherwise, vice versa.

Figure 3.1 Criteria for judging that the current point passes the origin

 Question 2: How do we choose a new direction when we have two points?

First of all, when we have two points, point A is often the latest added point, and point B is the first added point. We first construct vector AO(OA) and vector AB(BA), and solve the vector perpendicular to these two vectors and vertical upward by cross-multiplying the two vectors; secondly, solve a vertical vector and vector AB Vector (triple product). This is the new direction we mentioned d.

Figure 3.2 Method for selecting a new direction based on two points
Figure 3.3 Calculation method of new direction d

Question 3 and Question 4: How to detect whether the currently formed triangle contains the origin? How do we choose the next direction if the current triangle does not contain the origin?

Assume that we have three points A, B, and C (where A is often the latest added support point) forming a triangle, and we make a vertical line on each side of the triangle to define the area of ​​the space, forming the Voronoi area; assuming B Points 1 and C are fixed, given all possible positions of point A, the corresponding area will also change. In order to determine which areas the origin is likely to end up in, we need to analyze it.

Figure 3.4 Voronoi Region

3.1. Regions that cannot contain the origin and their analysis:

Rc: Since the support point C is the first support point, when we want to select the next support point, we must take point C as the starting point and point to the origin as the next new direction d to find the support point, which means that we is to search toward the origin in the opposite direction to Rc, so the origin cannot be in Rc.

Rb: When the origin is in Rb, it means that point B cannot exceed the origin, because the current simplex is invalid, and the currently selected point B is invalid.

Ra: When the origin is in Ra, it means that point A cannot exceed the origin, because the current simplex is invalid, and the currently selected point A is invalid.

Figure 3.5 Area detection and analysis

Rbc: The vertical line in the direction of BC is used for us to find point A, which is completely opposite to the direction of Rbc, because Rbc cannot possibly contain the origin. 

3.2. It is necessary to check whether the region containing the origin and its analysis:

Rab: If point A becomes a valid point, it will cross the origin in a given direction (that is, include the origin), then the area where point A is located is shown in Figure 3.6. In this range, Rab has the potential to contain the origin.

Figure 3.6 The area where the effective point A is located

 In order to verify the above conjecture, a vector perpendicular to AB is defined through the triple product theorem. When the dot product of the current vertical vector and vector A0 is greater than 0, it means that the origin is in Rab, so the current simplex is not suitable. We need to reselect and update this simplex, we remove point C from the simplex, the vector perpendicular to AB is used as the new direction d to find the third support point, so Rab has to check.

Figure 3.7 Rab's checking logic

 Rac: The check logic of this area is similar to that of Rab. When the origin is in Rac, we remove the current point B and reselect the point.

Figure 3.8 Check logic of Rac

One of the most important area checks:

Only two areas, Rab and Rac, are checked. When neither the Rab nor the Rac region contains the origin, that is, when the dot product of the vector perpendicular to AB and AC and the vector AO is less than 0, it means that the current origin is in the remaining Rabc.

Figure 3.9 The method of judging whether the origin is in Rabc

3.3 GJK Algorithm Details 

def GJK(s1,s2)
#两个形状s1,s2相交则返回True。所有的向量/点都是三维的,例如([x,y,0])
#第一步:选择一个初始方向,这个初始方向可以是随机选择的,但通常来说是两个形状中心之间的向量,即:
    d= normalize(s2.center-s1.center)
#第二步:找到支撑点,即第一个支撑点
    simplex=[support(s1,s2,d)]
#第三步:找到第一个支撑点后,以第一个支撑点为起点指向原点O的方向为新方向d
     d=ORIGIN-simplex[0]
#第四步:开始循环,找下一个支撑点
    while True
        A=[support(s1,s2,d)]
#当新的支撑点A没有经过原点,那我们就返回False,即两个形状没有相交
        if dot(A,d) <0:
            return False
#否则,我们就将该点A加入到simplex中
        simplex.append(A)
#handleSimplex负责主要逻辑部分。主要负责处理寻找新方向和更新simplex的逻辑内容,当当前simplex包含原点,则返回Ture
        if handleSimplex(simplex,d):
            return Ture

def handleSimplex(simplex,d)
#如果当前的simplex为直线情况,则进入lineCase(simplex,d)函数,寻找下一个方向d,并返回False,即直线情况下的simplex不包含原点
    if len(simplex==2):
        return lineCase(simplex,d)
#如果当前的simplex为三角情况,则进入triangleCase(simplex,d,
    return triangleCase(simplex,d)

def  lineCase(simplex,d)
#构建向量AB与AO,并使用三重积得到下一个方向
    B,A = simplex
    AB,AO=B-A,ORIGIN-A
    ABprep= tripleProd(AB,AO,AB)
    d.set(ABprep)
#由于一条直线的情况下,原点不能包含在simplex中,所以返回False
    return False

def triangleCase(simplex,d)
#构建向量AB,AC与AO,并来检测原点在空间的哪个区域。
    C,B,A = simplex
    AB,AC,AO=B-A,C-A,ORIGIN-A
#通过三重积分别得到垂直于AB、AC的向量,检测区域Rab、Rac中是否包含原点。
    ABprep= tripleProd(AC,AB,AB)
    ACprep= tripleProd(AB,AC,AC)
#如果原点在AB区域中,我们移除点C以寻找更加完美的simplex,新的方向就是垂直于AB的向量
    if dot(ABprep,AO)>0:
       simplex.remove(C);d.set(ABprep) 
       return False
#如果原点在AC区域中,我们移除点B以寻找更加完美的simplex,新的方向就是垂直于AC的向量
    elif dot(ACprep,AO)>0:
       simplex.remove(Ba);d.set(ACprep) 
       return False
#如果这两种情况都不符合,那就说明当前的三角形中包含原点,两个形状相交
    return Ture

def support(s1,s2,d)
#取第一个形状上方向d上最远点并减去第二个形状上相反反向(-d)上最远的点
    return s1.furthestPoint(d)-s2.furthestPoint(-d)

Just to strengthen my understanding and memory, if it involves infringement issues, please contact me and I will delete it~

Guess you like

Origin blog.csdn.net/qwerpoiu66/article/details/131370634