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

Source: Internet
Author: User
3. Further expansion of Fast Moving ObjectsThe above method will achieve very good results when processing slow moving objects. However, when an object moves very fast, the collision system will lose accuracy, collision, or allow the objects to cross each other. This is not what we expected. Here we still use the separation coordinate axis method, and further expand it, and use this algorithm to detect collision and overlap at a certain time point in the future. The principle is still the same. You can use the following picture to explain it:

Now we need to use projection mathematics. If the projection interval does not overlap, the velocity is projected onto the separation axis and the collision time between the two intervals is calculated. Compared with the static Separation Axis algorithm, we need to test an extended axis. Obviously, this is the speed vector axis. Now we have three options: 1. Interval overlap 2. The intervals do not overlap, but a collision will occur at a certain time point in the future. 3. The separation is not intersecting and will not collide in the future. The third possibility means that the object will not collide at the frame, and the Separation Axis truly isolates the object. Therefore, no collision occurs between objects. The axisseparatepolygons () function will reflect this phenomenon and return the overlap or collision time. To distinguish the two, a negative value is returned when overlap is detected. If a collision in the future is detected, a positive value is returned. The function looks as follows:

Bool axisseparatepolygons (vector axis, polygon A, polygon B, vector offset, vector Vel, float & T, float Tmax );

Here, offset is the relative distance between polygon A and polygon B, and Vel is the relative speed of polygon A to Polygon B. The algorithm for solving the collision plane is very similar to MTD. Only collision will be better than overlap. If a collision in the future is detected, the latest one will be selected. If no collision is detected and only overlap is detected, select the axis with the smallest overlap as before. The collision detection function returns the normal direction of the collision, as well as the collision depth (negative value) and collision time (positive value. The final pseudo code is as follows...

Bool Collide (const vector * a, int anum,
Const vector * B, int bnum,
Const vector & xoffset, const vector & xvel,
Vector & N, float & T)
{
If (! A |! B) Return false;

// All the separation Axes
// Note: a maximum of 32 vertices per Poly is supported
Vector xaxis [64];
Float taxis [64];
Int inumaxes = 0;

Xaxis [inumaxes] = vector (-xvel. Y, xvel. X );
Float fvel2 = xvel * xvel;
If (fvel2> 0.20.1f)
{
If (! Intervalintersect (A, anum, B, bnum, xaxis [inumaxes], xoffset, xvel, taxis [inumaxes], t ))
Return false;
Inumaxes ++;
}

// Test separation axes of
For (Int J = Anum-1, I = 0; I <anum; j = I, I ++)
{
Vector e0 = A [J];
Vector e1 = A [I];
Vector E = e1-E0;
Xaxis [inumaxes] = vector (-E. Y, E. X );

If (! Intervalintersect (A, anum, B, bnum, xaxis [inumaxes], xoffset, xvel, taxis [inumaxes], t ))
Return false;

Inumaxes ++;
}

// Test separation axes of B
For (Int J = Bnum-1, I = 0; I <bnum; j = I, I ++)
{
Vector e0 = B [J];
Vector e1 = B [I];
Vector E = e1-E0;
Xaxis [inumaxes] = vector (-E. Y, E. X );

If (! Intervalintersect (A, anum, B, bnum, xaxis [inumaxes], xoffset, xvel, taxis [inumaxes], t ))
Return false;
Inumaxes ++;
}

If (! Findmtd (xaxis, taxis, inumaxes, N, t ))
Return false;

// Make sure the polygons gets pushed away from each other.
If (N * xoffset <0.0f)
N =-N;

Return true;
}

Bool axisseparatepolygons (vector N, polygon A, polygon B, vector offset, vector Vel, float & T, float Tmax)
{
Float min0, max0;
Float min1, max1;

Calculateinterval (n, A, min0, max0 );
Calculateinterval (n, B, min1, max1 );

Float H = offset dot n;
Min0 + = h;
Max0 + = h;

Float D0 = min0-max1; // If overlapped, DO <0
Float d1 = min1-max0; // If overlapped, D1> 0

// Separated, test dynamic intervals
If (D0> 0.0f | D1> 0.0f)
{
Float v = vel dot n;

// Small velocity, so only the overlap test will be relevant.
If (FABS (v) <0.0000001f)
Return false;

Float T0 =-D0/V; // time of impact to D0 reaches 0
Float T1 = D1/V; // time of impact to D0 reaches 1
// Sort the times.
If (T0> T1)
{
Float temp = T0;
T0 = T1;
T1 = temp;
}
// Take the minimum positive
Taxis = (T0> 0.0f )? T0: T1;

// Intersection time too late or back in time, no collision
If (Taxis <0.0f | taxis> Tmax)
Return true;

Return false;
}
Else
{
// Overlap. Get the interval, as a smallest of | D0 | and | d1 |
// Return negative number to mark it as an overlap
Taxis = (D0> d1 )? D0: d1;
Return false;
}
}


Bool findcollisionplane (vector * axis, float * taxis, int inumaxes, vector & ncoll, float & tcoll)
{
// Find collision first
Int mini =-1;
Tcoll = 0.0f;
For (INT I = 0; I <inumaxes; I ++)
{
If (Taxis [I]> 0.0f)
{
If (Taxis [I]> tcoll)
{
Mini = I;
Tcoll = taxis [I];
Ncoll = Axis [I];
Ncoll. normalise (); // normalise axis
}
}
}

// Found a collision
If (Mini! =-1)
Return true;

// Nope, find overlaps
Mini =-1;
For (INT I = 0; I <inumaxes; I ++)
{
Float n = Axis [I]. normalise (); // axis length

Taxis [I]/= N; // normalise interval overlap too

// Remember, those numbers are negative, so take the closest to 0
If (mini =-1 | taxis [I]> tcoll)
{
Mini = I;
Tcoll = taxis [I];
Ncoll = Axis [I];
}
}

Return (Mini! =-1 );
}

Now, you have a detection system that can detect future collisions, or return the collision plane and collision depth/time when overlapping.

 

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.