findMaxSeparation method

void findMaxSeparation(
  1. _EdgeResults results,
  2. PolygonShape poly1,
  3. Transform xf1,
  4. PolygonShape poly2,
  5. Transform xf2
)

Find the max separation between poly1 and poly2 using edge normals from poly1.

Implementation

void findMaxSeparation(_EdgeResults results, final PolygonShape poly1,
    final Transform xf1, final PolygonShape poly2, final Transform xf2) {
  int count1 = poly1.count;
  int count2 = poly2.count;
  List<Vector2> n1s = poly1.normals;
  List<Vector2> v1s = poly1.vertices;
  List<Vector2> v2s = poly2.vertices;

  Transform.mulTransToOutUnsafe(xf2, xf1, _xf);
  final Rot xfq = _xf.q;

  int bestIndex = 0;
  double maxSeparation = -double.maxFinite;
  for (int i = 0; i < count1; i++) {
    // Get poly1 normal in frame2.
    Rot.mulToOutUnsafe(xfq, n1s[i], _n);
    Transform.mulToOutUnsafeVec2(_xf, v1s[i], _v1);

    // Find deepest point for normal i.
    double si = double.maxFinite;
    for (int j = 0; j < count2; ++j) {
      Vector2 v2sj = v2s[j];
      double sij = _n.x * (v2sj.x - _v1.x) + _n.y * (v2sj.y - _v1.y);
      if (sij < si) {
        si = sij;
      }
    }

    if (si > maxSeparation) {
      maxSeparation = si;
      bestIndex = i;
    }
  }

  results.edgeIndex = bestIndex;
  results.separation = maxSeparation;
}