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.00001f) { if (!IntervalIntersect( A, Anum, B, Bnum, xAxis[iNumAxes], xOffset, xVel, taxis[iNumAxes], t)) return false; iNumAxes++; } // test separation axes of A 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 the 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); } |