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,
  PolygonShape poly1,
  Transform xf1,
  PolygonShape poly2,
  Transform xf2,
) {
  final count1 = poly1.vertices.length;
  final count2 = poly2.vertices.length;
  final n1s = poly1.normals;
  final v1s = poly1.vertices;
  final v2s = poly2.vertices;

  _xf.setFrom(Transform.mulTrans(xf2, xf1));
  final xfq = _xf.q;

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

    // Find deepest point for normal i.
    var si = double.maxFinite;
    for (var j = 0; j < count2; ++j) {
      final v2sj = v2s[j];
      final 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;
}