size_t collide(CircleShape &circle, BoxShape &box, float dt, Contact *pxContacts, size_t numMaxContacts) { dt, pxContacts, numMaxContacts; const Point2f &p1 = circle.body.getPosition(); const Point2f &p2 = box.body.getPosition(); // work in the box's coordinate system const Point2f kDiff = p1 - p2; // compute squared distance and closest point on box float fSqrDistance = 0.0f, fDelta; Point2f kClosest(kDiff * box.GetDir(0), kDiff * box.GetDir(1)); const Point2f &extents = box.GetExtents(); if (kClosest.x < -extents.x) { fDelta = kClosest.x + extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = -extents.x; } else if (kClosest.x > extents.x) { fDelta = kClosest.x - extents.x; fSqrDistance += fDelta*fDelta; kClosest.x = extents.x; } if (kClosest.y < -extents.y) { fDelta = kClosest.y + extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = -extents.y; } else if (kClosest.y > extents.y) { fDelta = kClosest.y - extents.y; fSqrDistance += fDelta*fDelta; kClosest.y = extents.y; } if (fSqrDistance > circle.GetRadius() * circle.GetRadius()) { return 0; } Point2f d = p2 + kClosest - p1; d.Normalize(); pxContacts[0] = Contact(p1 + d * circle.GetRadius(), p2 + kClosest, &circle.body, &box.body); return 1; }
size_t collide(BoxShape &box1, BoxShape &box2, float dt, Contact *pxContacts, size_t numMaxContacts) { dt; Point2f xAxis [4]; float fDepth[4]; xAxis[0] = box1.GetDir(0); if (!IntervalIntersect(xAxis[0], box1, box2, fDepth[0])) return 0; xAxis[1] = box1.GetDir(1); if (!IntervalIntersect(xAxis[1], box1, box2, fDepth[1])) return 0; xAxis[2] = box2.GetDir(0); if (!IntervalIntersect(xAxis[2], box1, box2, fDepth[2])) return 0; xAxis[3] = box2.GetDir(1); if (!IntervalIntersect(xAxis[3], box1, box2, fDepth[3])) return 0; float dcoll; Point2f Ncoll; if (!FindCollisionPlane(xAxis, fDepth, Ncoll, dcoll)) return 0; Point2f D = box1.body.getPosition() - box2.body.getPosition(); if (D * Ncoll < 0.0f) Ncoll *= -1.0f; Point2f xPoints0[4]; Point2f xPoints1[4]; size_t numPoints0; size_t numPoints1; numPoints0 = box1.FindSupportPoints( Ncoll, xPoints0); numPoints1 = box2.FindSupportPoints(-Ncoll, xPoints1); if (!ConvertSupportPointsToContacts(box1, box2, Ncoll, xPoints0, numPoints0, xPoints1, numPoints1, pxContacts, numMaxContacts)) return 0; return numMaxContacts; }