/********************************************************** C# code from http://www.codeproject.com/Articles/15573/2D-Polygon-Collision-Detection */ bool Polyhedron::DoesCollide(Vector2f* Velocity, Polyhedron* CheckWith) { bool Intersect = true; bool WillIntersect = true; int edgeCountA = Edges->count; int edgeCountB = CheckWith->Edges->count; // float minIntervalDistance = 99999999; // Vector2f* translationAxis = new Vector2f( 0, 0 ); Vector2f* edge; // Loop through all the edges of both polygons for( int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++ ) { if( edgeIndex < edgeCountA ) edge = Edges->ItemAt<Vector2f*>(edgeIndex); else edge = CheckWith->Edges->ItemAt<Vector2f*>(edgeIndex - edgeCountA); // ===== 1. Find if the polygons are currently intersecting ===== // Find the axis perpendicular to the current edge Vector2f* axis = new Vector2f(-edge->y, edge->x); axis->Normalise(); // Find the projection of the polygon on the current axis float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; Project(axis, &minA, &maxA); CheckWith->Project(axis, &minB, &maxB); // Check if the polygon projections are currentlty intersecting if( IntervalDistance(minA, maxA, minB, maxB) > 0 ) Intersect = false; // ===== 2. Now find if the polygons *will* intersect ===== // Project the velocity on the current axis float velocityProjection = axis->DotProduct( Velocity ); // Get the projection of polygon A during the movement if( velocityProjection < 0 ) minA += velocityProjection; else maxA += velocityProjection; // Do the same test as above for the new projection float intervalDistance = IntervalDistance( minA, maxA, minB, maxB ); if( intervalDistance > 0 ) WillIntersect = false; delete axis; // If the polygons are not intersecting and won't intersect, exit the loop if( !Intersect && !WillIntersect) break; /* // Check if the current interval distance is the minimum one. If so store // the interval distance and the current distance. // This will be used to calculate the minimum translation vector intervalDistance = Math.Abs(intervalDistance); if (intervalDistance < minIntervalDistance) { minIntervalDistance = intervalDistance; translationAxis = axis; Vector d = polygonA.Center - polygonB.Center; if (d.DotProduct(translationAxis) < 0) translationAxis = -translationAxis; } */ int xcv = 1; } // The minimum translation vector can be used to push the polygons appart. // First moves the polygons by their velocity // then move polygonA by MinimumTranslationVector. // if (result.WillIntersect) result.MinimumTranslationVector = translationAxis * minIntervalDistance; return Intersect | WillIntersect; }