void RenderHelper::DrawPolygon( const Polygon2 & polygon, bool closed) { int ptCount = polygon.pointCount; if (ptCount >= 2) { vertexStream->Set(TYPE_FLOAT, 2, 0, polygon.GetPoints()); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetRenderData(renderDataObject); RenderManager::Instance()->DrawArrays(PRIMITIVETYPE_LINESTRIP, 0, ptCount); if (closed) { Vector2 line[2] = {Vector2(polygon.GetPoints()[0]), Vector2(polygon.GetPoints()[ptCount-1])}; vertexStream->Set(TYPE_FLOAT, 2, 0, line); RenderManager::Instance()->DrawArrays(PRIMITIVETYPE_LINESTRIP, 0, 2); } } }
void RenderHelper::FillPolygon(const Polygon2 & polygon) { int ptCount = polygon.pointCount; if (ptCount >= 3) { vertexStream->Set(TYPE_FLOAT, 2, 0, polygon.GetPoints()); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetRenderData(renderDataObject); RenderManager::Instance()->DrawArrays(PRIMITIVETYPE_TRIANGLEFAN, 0, ptCount); } }
void Collisions::ProjectPolygon(const Vector2 & axis, Polygon2 & poly, float32 & min, float32 & max) { Vector2 * points = poly.GetPoints(); float32 proj = DotProduct(points[0], axis); min = proj; max = proj; for (int32 pi = 1; pi < poly.GetPointCount(); ++pi) { proj = DotProduct(points[pi], axis); if (proj < min) min = proj; if (proj > max) max = proj; } }
void RenderHelper::DrawPolygonPoints(const Polygon2 & polygon) { int ptCount = polygon.pointCount; if (ptCount >= 1) { #if defined (__DAVAENGINE_OPENGL__) glPointSize(3.0f); #endif vertexStream->Set(TYPE_FLOAT, 2, 0, polygon.GetPoints()); RenderManager::Instance()->SetRenderEffect(RenderManager::FLAT_COLOR); RenderManager::Instance()->SetRenderData(renderDataObject); RenderManager::Instance()->DrawArrays(PRIMITIVETYPE_POINTLIST, 0, ptCount); #if defined (__DAVAENGINE_OPENGL__) glPointSize(1.0f); #endif } }
bool Collisions::IsPolygonIntersectsPolygon(Polygon2 & poly1, Polygon2 & poly2) { //#define DEBUG_DRAW_INTERSECTIONS Vector2 * points1 = poly1.GetPoints(); Vector2 * points2 = poly2.GetPoints(); separationAxes.clear(); for (int32 index1 = 0; index1 < poly1.pointCount; ++index1) { int32 index2 = (index1 + 1 != poly1.pointCount) ? (index1 + 1) : (0); Vector2 line = points1[index2] - points1[index1]; Vector2 normal = Vector2(line.y, -line.x); normal.Normalize(); AddSeparationAxis(normal); #if defined(DEBUG_DRAW_INTERSECTIONS) RenderManager::Instance()->SetColor(0.0f, 0.0f, 1.0f, 1.0f); RenderHelper::DrawLine(points1[index1] + (line / 2), points1[index1] + (line / 2) + normal * 10); #endif } for (int32 index1 = 0; index1 < poly2.pointCount; ++index1) { int32 index2 = (index1 + 1 != poly2.pointCount) ? (index1 + 1) : (0); Vector2 line = points2[index2] - points2[index1]; Vector2 normal = Vector2(line.y, -line.x); normal.Normalize(); AddSeparationAxis(normal); #if defined(DEBUG_DRAW_INTERSECTIONS) RenderManager::Instance()->SetColor(0.0f, 1.0f, 0.0f, 1.0f); RenderHelper::DrawLine(points2[index1] + (line / 3), points2[index1] + (line / 3) + normal * 10); #endif } size_t size = separationAxes.size(); #if defined(DEBUG_DRAW_INTERSECTIONS) for (size_t index = 0; index < size; ++index) { Vector2 axis = separationAxes[index]; RenderManager::Instance()->SetColor(1.0f, 0.0f, 0.0f, 1.0f); RenderHelper::DrawLine(Vector2(50.0f, 50.0f), Vector2(50.0f, 50.0f) + axis * 1000); } #endif for (size_t index = 0; index < size; ++index) { Vector2 axis = separationAxes[index]; float32 p1Min, p1Max; ProjectPolygon(axis, poly1, p1Min, p1Max); float32 p2Min, p2Max; ProjectPolygon(axis, poly2, p2Min, p2Max); #if defined(DEBUG_DRAW_INTERSECTIONS) RenderManager::Instance()->SetColor(0.0f, 1.0f, 1.0f, 1.0f); Vector2 norm = Vector2(axis.y, -axis.x); RenderHelper::DrawLine(Vector2(50.0f, 50.0f) + axis * p1Min + norm * 2.0f, Vector2(50.0f, 50.0f) + axis * p1Max + norm * 2.0f); RenderManager::Instance()->SetColor(1.0f, 1.0f, 0.0f, 1.0f); RenderHelper::DrawLine(Vector2(50.0f, 50.0f) + axis * p2Min - norm * 2.0f, Vector2(50.0f, 50.0f) + axis * p2Max - norm * 2.0f); #endif if (IntervalDistance(p1Min, p1Max, p2Min, p2Max) > 0) return false; } return true; }