Example #1
0
	MTV PolygonvsPolygon(Shape* pa, Shape* pb)
	{
		Polygon* a = dynamic_cast<Polygon*>(pa);
		Polygon* b = dynamic_cast<Polygon*>(pb);

		Vec2 smallest;
		float overlap = FLT_MAX;
		for (unsigned i = 0; i < a->getAxesNum(); i++) {
			Vec2 axis = a->getAxes(i);
			Projection p1 = a->project(axis);
			Projection p2 = b->project(axis);
			if (!p1.overlap(p2)) {
				return MTV(false);
			}
			else {
				// get the overlap
				float o = p1.getOverlap(p2);
				o = fabs(o);
				// check for minimum
				if (o < overlap) {
					// then set this one as smallest
					overlap = o;
					smallest = axis;

					Vec2 d;
					d = a->Center() - b->Center();
					if (Dot(d, smallest) < 0)
						smallest.Negate();
				}
			}
		}

		for (unsigned i = 0; i < b->getAxesNum(); i++) {
			Vec2 axis = b->getAxes(i);
			Projection p1 = a->project(axis);
			Projection p2 = b->project(axis);
			if (!p1.overlap(p2)) {
				return MTV(false);
			}
			else {
				// get the overlap
				float o = p1.getOverlap(p2);
				o = fabs(o);
				// check for minimum
				if (o < overlap) {
					// then set this one as smallest
					overlap = o;
					smallest = axis;

					Vec2 d;
					d = a->Center() - b->Center();
					if (Dot(d, smallest) < 0)
						smallest.Negate();
				}
			}
		}

		return MTV(smallest, overlap, true);
	}