//returns true if the polygons passed as parameters intersect int PolyInPoly(const POLY * t1, const POLY * t2) { int i, j; LINE l1, l2; if(!t1 || !t2) return 0; if(PointInPoly(&t1->p[0], t2) || PointInPoly(&t1->p[1], t2) || PointInPoly(&t1->p[2], t2)) return 1; //if any of the points of t1 are in t2 for(i = 0; i < 3; i++) { l1.a.x = t1->p[i].x; l1.a.y = t1->p[i].y; l1.b.x = t1->p[(i + 1) % 3].x; l1.b.y = t1->p[(i + 1) % 3].y; for(j = 0; j < 3; j++) { l2.a.x = t2->p[j].x; l2.a.y = t2->p[j].y; l2.b.x = t2->p[(j + 1) % 3].x; l2.b.y = t2->p[(j + 1) % 3].y; if(LinesIntersect(&l1, &l2)) //if the line intersects any line in t2 return 1; } } return 0; //no intersection }
//returns true if the point is contained within the polygon int PointInPoly(const POINT2 * p, const POLY * t) { int i, intersections = 0; LINE l1, l2; if(!p || !t) return 0; l1.a.x = -1.0f; l1.a.y = -1.0f; l1.b.x = p->x; l1.b.y = p->y; for(i = 0; i < 3; i++) { l2.a.x = t->p[i].x; l2.a.y = t->p[i].y; l2.b.x = t->p[(i + 1) % 3].x; l2.b.y = t->p[(i + 1) % 3].y; if(LinesIntersect(&l1, &l2)) intersections++; } return (intersections & 1); //returns 1 if odd, 0 if even # intersections }
bool Collider::CollidePolygonPolygon(PolygonCollider* a, PolygonCollider* b) { //If the polygons aren't even close to eachother, don't bother if (a->GetLeftmost() > b->GetRightmost()) return false; if (a->GetRightmost() < b->GetLeftmost()) return false; if (a->GetTopmost() > b->GetBottommost()) return false; if (a->GetBottommost() < b->GetTopmost()) return false; //Check if the first point of either polygon is inside the other //(accounts for the case where one poly is completely inside the other) if (a->IntersectsPoint(b->GetPoint(0)) || b->IntersectsPoint(a->GetPoint(0))) return true; //Compare lines, looking for an intersection int totalA = a->GetPointCount(); int totalB = b->GetPointCount(); for (int i = 0; i < totalA; ++i) { Vector2 startA = a->GetPoint(i); Vector2 endA = a->GetPoint((i + 1) % totalA); for (int j = 0; j < totalB; ++j) { Vector2 startB = b->GetPoint(j); Vector2 endB = b->GetPoint((j + 1) % totalB); if (LinesIntersect(startA, endA, startB, endB)) return true; } } return false; }
std::vector<IntResult> SimpleIntersector::Intersects(FastCurve* curve1, FastCurve* curve2) { std::vector<IntResult> ret; bool swap=false; FastLine* line1 = dynamic_cast<FastLine*>(curve1); if(line1) { FastLine* line2 = dynamic_cast<FastLine*>(curve2); if(line2) ret = LinesIntersect((FastLine*)curve1,(FastLine*)curve2); else { FastArc* arc2 = dynamic_cast<FastArc*>(curve2); if(arc2) { ret = LineArcIntersect(line1,arc2); } } } else { FastArc* arc1 = dynamic_cast<FastArc*>(curve1); if(arc1) { FastLine* line2 = dynamic_cast<FastLine*>(curve2); if(line2) { ret = LineArcIntersect(line2,arc1); swap = true; } else { FastArc* arc2 = dynamic_cast<FastArc*>(curve2); ret = ArcsIntersect(arc1,arc2); } } } if(swap) { for(unsigned int i=0; i < ret.size(); i++) { ret[i].Swap(); } } return ret; }
// ignores radius for now bool PathCollider::IntersectsLine(const Vector2& start, const Vector2& end, float radius, CollisionData *collisionData) { Node *node = startNode; while(node) { Node *next = node->GetNext(); if (next) { if (node->variant != -1) { if (LinesIntersect(node->GetWorldPosition(), next->GetWorldPosition(), start, end, collisionData)) { collisionData->collider = this; return true; } } } node = next; } return false; }
bool Collider::CollideRectPolygon(RectangleCollider* a, PolygonCollider* b, CollisionData *collisionData) { //TODO: store data in collisionData! //Check if it's even possible for them to collide if (a->GetLeft() > b->GetRightmost()) return false; if (a->GetRight() < b->GetLeftmost()) return false; if (a->GetTop() > b->GetBottommost()) return false; if (a->GetBottom() < b->GetTopmost()) return false; //Check if the first point of either is inside the other //(accounts for the case where one is completely inside the other) if (a->IntersectsPoint(b->GetPoint(0)) || b->IntersectsPoint(a->GetTopLeft())) return true; //Compare lines for intersections Vector2 rectPoints[4]; rectPoints[0] = a->GetTopLeft(); rectPoints[1] = a->GetTopRight(); rectPoints[2] = a->GetBottomRight(); rectPoints[3] = a->GetBottomLeft(); int totalB = b->GetPointCount(); for (int i = 0; i < totalB; ++i) { Vector2 startB = b->GetPoint(i); Vector2 endB = b->GetPoint((i + 1) % totalB); for (int j = 0; j < 4; ++j) { if (LinesIntersect(startB, endB, rectPoints[j], rectPoints[(j + 1) % 4])) return true; } } return false; }