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

Source: Internet
Author: User
IV, Basic arc Collision Response

What we need to do below is to separate two objects with a given amount, and add a little friction and some static friction, so that the object can rest on the inclined surface.

This part uses a simple speed to influence the algorithm. Similarly, in order to make the Collision Response more realistic, the object is given quality (better is the reciprocal of quality ).

The reciprocal of mass is commonly used. The zero value means that the object has an infinite quality and therefore cannot be moved. At the same time, the reciprocal of quality used in speed response has better physical accuracy.

Now we know that Polygon A has a velocity VA at the position Pa, and it collides with polygon B at the position Pb velocity VB. Ncoll and tcoll define the collision plane. If there are overlapping objects before the collision, the two objects are first separated as follows:

If (tcoll <0)
{
If (A. invmass = 0)
Pb + = ncoll * tcoll;
Else if (B. invmass = 0)
Pa-= ncoll * tcoll;
Else
{
Pa-= ncoll * (tcoll * 0.5f );
Pb + = ncoll * (tcoll * 0.5f );
}
}

Then we can call the Collision Response Code. to simplify the code, we can consider a particle hitting a plane.

Here, "V" indicates the particle's entry speed. "v" indicates the particle's collision speed. "N" indicates the normal direction of the plane.

V' = V-(2 * (V. N) * n

Ideally, the energy of particles before and after a collision is the same. However, we can add an elastic coefficient to the collision of particles.

V '= V-(1 + elasticity) * (V. N) * n

The range of elasticity is [0, 1]. If it is 0, it means that the particle will slide along the plane. If it is 1, the particle will be unlocked without energy loss.

We can also add some friction. If we break down the velocity along the collision normal and the collision plane, we can calculate both the elasticity and friction.

Here, the velocity is decomposed along the normal direction and plane of the plane. The elastic coefficient will affect the response (VN) along the normal direction of the plane, and the friction will affect the tangent (VT) of the velocity ).

The range of the friction coefficient is [0, 1]. 0, which means no friction, and 1 means that the particle will suddenly stop.

Vn = (V. N) * N;
Vt = V-VN;
V' = Vt * (1-friction) + VN *-(elasticity );

For static friction, simply set VT to (0, 0) when the velocity VT is less than the given value, or set the friction coefficient slightly greater than 1 ( 1.001f ).

Now, calculate the Collision Response between two objects. The principle is the same. However, the calculation is based on the relative velocity of the object, and the object will be affected as above. The result is added to each object.

 

Now we need to modify the coefficient, because now we use the relative concept.

Vector v = va-Vb; // relative velocity
Vn = (V. N) * N;
Vt = V-VN;

If (vt. Length () < 0.01f ) Friction = 1.01f ;

// Response
V' = Vt *-(friction) + VN *-(1 + elasticity );

Va + = V '* 0.5f ;
VB-= V '* 0.5f ;

In this example, object A and object B have the same response results.

To make the results more interesting, A and B may have different quality. Obviously, lighter objects will be greatly affected, and heavier objects will be less affected by collision. Therefore, we can use quality to determine the Collision Response of two objects. A large object has a small reciprocal of Mass. If the mass is an infinite mass, the reciprocal is zero.

Va + = V' * (invmassa)/(invmassa + invmassb );
VB-= V' * (invmassb)/(invmassa + invmassb );

 

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.