CollisionReport Seg2Seg(const Seg& a, const Seg& b) { CollisionReport report; glm::vec2 r = a.B - a.A; glm::vec2 s = b.B - b.A; glm::vec2 PQ = b.A - a.A; float rxs = cross2D(r, s); if (isEqual(rxs, 0.0f)) { // collinear or parallel float PQxr = cross2D (PQ, r); if (isEqual (PQxr, 0.0f, 1e-8f)) { // collinear float a0 = 0.0; float a1 = glm::dot (r,r); float b0 = glm::dot (PQ, r); float b1 = glm::dot (b.B - a.A, r); float am = std::min (a0, a1); float aM = std::max (a0, a1); float bm = std::min (b0, b1); float bM = std::max (b0, b1); if (bm >= aM || bM <= am) return report; report.collide = true; report.distance = 0.0f; } return report; } // if we get here then we need to compute t and u float t = cross2D(PQ, s) / rxs; float u = cross2D(PQ, r) / rxs; if (t >= 0.0f && t <=1.0f && u >= 0.0f && u <= 1.0f) { // intersection report.collide = true; report.distance = t * glm::length(r); } return report; }
bool Simplex::inside(Point * p) { return (cross2D(*_A, *_B, *p) < 0 && cross2D(*_B, *_C, *p) < 0 && cross2D(*_C, *_A, *p) < 0); }