These are ripped from my polygon class and simplified.
template <class T>struct Vertex2d{ T x; T y;};//:://///////////////////////////////////////////////////////////////////////////:: Function Name://:: Precondition://:: Postcondition://:: ///////////////////////////////////////////////////////////////////////////template <class T>bool IsPolygonPolygonCollision(Vertex2d<T>* polygon1, int size1, Vertex2d<T>* polygon2, int size2){ bool bIsCollision = false; int size; size = size1; while(size--) { if(IsVertexPolygonCollision(polygon2, size2, polygon1[size])) { bIsCollision = true; break; } } if(!bIsCollision) { size = size2; while(size2--) { if(IsVertexPolygonCollision(polygon1, size1, polygon2[size])) { bIsCollision = true; break; } } } return bIsCollision;}//:://///////////////////////////////////////////////////////////////////////////:: Function Name://:: Precondition://:: Postcondition://::///////////////////////////////////////////////////////////////////////////template <class T>bool IsVertexPolygonCollision(Vertex2d<T>* polygon, int size, Vertex2d<T>& vertex){ bool bIsInside = false; bool bIsYTop; bool bLastY; T prevX; T prevY; // Get the last vertex in polygon prevX = polygon[size - 1].x; prevY = polygon[size - 1].y; if(prevY >= vertex.y) { bLastY = true; } else { bLastY = false; } for(int i = 0; i < size; i++) { if(polygon[i].y >= vertex.y) { bIsYTop = true; } else { bIsYTop = false; } if(bLastY != bIsYTop) { if(((polygon[i].y - vertex.y) * (prevX - polygon[i].x) >= (polygon[i].x - vertex.x) * (prevY - polygon[i].y)) == bLastY ) { bIsInside = !bIsInside; } } prevX = polygon[i].x; prevY = polygon[i].y; bLastY = bIsYTop; } return bIsInside;}
BTW... This polygon test is so effecient I can render a polygon by creating a bounding box around ANY polygon no matter how complex, and test each pixel if it is inside the polygon. It is pixel perfect, and the rendering isn't that slow considering I'm checking every pixel. Obviously I just render it using the collision detection to check that it works well.
[edited by - Zefrieg on August 8, 2003 5:00:09 AM]