public Vector2 checkCollision(ConvexPolygonInterface polygonA, ConvexPolygonInterface polygonB, float radDistance, Vector2 velocity) { int edgesCountPolygonA = polygonA.getNumberOfPoints(); int edgesCountPolygonB = polygonB.getNumberOfPoints(); Vector2 centerA = polygonA.getCenter(); Vector2 centerB = polygonB.getCenter(); if (polygonA is CircularConvexPolygon && polygonB is CircularConvexPolygon) { Vector2 tempRet = centerA - centerB; tempRet.Normalize(); return tempRet * radDistance; } else if (polygonA is CircularConvexPolygon) { edgesCountPolygonA = edgesCountPolygonB; } else if (polygonB is CircularConvexPolygon) { edgesCountPolygonB = edgesCountPolygonA; } float minDistance = float.PositiveInfinity; Vector2 minAxis = Vector2.Zero; Vector2 currentEdgeNormal; for (int i = 0; i < edgesCountPolygonA + edgesCountPolygonB; i++) { if (i < edgesCountPolygonA) { if (polygonA is CircularConvexPolygon) { currentEdgeNormal = polygonB.getPoint(i) - centerA; } else { currentEdgeNormal = polygonA.getEdge(i); } } else { if (polygonB is CircularConvexPolygon) { currentEdgeNormal = polygonA.getPoint(i - edgesCountPolygonA) - centerB; } else { currentEdgeNormal = polygonB.getEdge(i - edgesCountPolygonA); } } currentEdgeNormal.Normalize(); float minA = 0, minB = 0, maxA = 0, maxB = 0; polygonA.projectPolygonOnAxis(currentEdgeNormal, new Height(), ref minA, ref maxA); polygonB.projectPolygonOnAxis(currentEdgeNormal, new Height(), ref minB, ref maxB); float velocityProjection = ConvexPolygon.dotProduct(velocity, currentEdgeNormal); if (velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } float distance = distanceBetweenProjections(minA, maxA, minB, maxB); if (distance > 0) { return Vector2.Zero; } distance = Math.Abs(distance); if (distance < minDistance) { minDistance = distance; if (ConvexPolygon.dotProduct(centerA - centerB, currentEdgeNormal) < 0) { minAxis = -currentEdgeNormal; } else { minAxis = currentEdgeNormal; } } } return minAxis * minDistance; }