Collision detection and response based on 2D polygon (6)

Source: Internet
Author: User
6. Computing contactsTo dynamically move a rigid body, we need to accurately calculate the contacts between two collision polygon. This is not complicated for 2D, but it will become very complicated in 3D scenarios. In 2D, you can consider the intersection of vertices and edges or the intersection of edges. This process requires almost no tutorials, but it is very suitable for a visual demonstration.

Here, I only consider overlap. This principle is also applicable to collision. Now we provide a method for collision. How can we obtain the contact point when the vertex and edge are in contact? For contact point A, it is directly forward. We need to call a support ing function, which will return the lowest point of the polygon in the specified direction, very similar to the calculateinterval () function in the first example.

Int findsupportpoints (const vector & N, float t,
Const vector * a, int anum,
Const vector & PA, const vector & va,
Const Matrix & OA, vector * s)
{
Vector norm = n ^ OA;
Float d [32];
Float dmin;
Dmin = d [0] = A [0] * norm;

For (INT I = 1; I <anum; I ++)
{
D [I] = A [I] * norm;

If (d [I] <Dmin)
{
Dmin = d [I];
}
}

Int snum = 0;

Const float Threshold = 1.0e-3f;

For (INT I = 0; I <anum; I ++)
{
If (d [I] <dmin + threshold)
{
S [snum ++] = transform (A [I], Pa, VA, OA, t );

If (snum = 2)
Return snum;
}
}
Return snum;
}

Here, the first part of the function finds the minimum polygon value. The second part simply finds all vertices close to the minimum value. Therefore, if the collision normal is perpendicular to an edge, two vertices are returned. These two points are stored in the world coordinates. The transformation function will ensure that the contact of the polygon is converted to the world coordinates. If it is a future collision, this point will be converted to the collision moment.

Vector transform (const vector & vertex, const vector & P, const vector & V, const Matrix & xorient, float T)
{
// Convert point into World Space
Vector T = P + (vertex * xorient );

// Collision forwatd in time, need to translate to moment of collision
If (T> 0.0f)
T + = V * t;
Return T;
}

For vertices on B, you only need to find a Support Point in the opposite direction. In future processing, we need a pair of support points on two objects to simulate the thrust and rotate the object, you can find a pair of contacts in different collision scenarios. Now, the findsupportpoint () function is called to return one or two contacts on each object. In the case of one-to-one contact, there is no need to do anything. Currently, one-to-one contact is not supported, but it can be easily extended to the Separation Axis algorithm. In the case of one-to-two contact, it is a simple point-to-edge collision. For example, the first graph in the case of two-to-one contact can also use the above situation, in addition to the fact that an object is exchanged with two sides, it is an edge collision. You need to find the overlapping areas of the two sides. First, for point-and-edge collision, in this case, a pair of collision points can be simply projected to the side of B by the collision point on, or the point closest to the contact on the side of B.

Vector findclosestpoint (const vector & V, const vector & A, const vector & B, float * PT)
{
Vector AV = V-;
Vector AB = B-;

Float t = (av * AB)/(AB * AB );

If (T <0.0f)
T = 0.0f;
Else if (T> 1.0f)
T = 1.0f;

If (PT) * PT = T;

Vector p = A + T * AB;
Return P;
}

In the case of side-to-side collision, the processing process is very similar. You just need to sort the point along the vertical direction of the collision normal and get two intermediate points. Then, they are projected to another side to get a pair of contacts.

With these contacts, you can write a Basic Rigid Body System, and objects will collide with each other more realistically and respond to the collision.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.