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 ); } |