findIncidentEdge method

void findIncidentEdge(
  1. List<ClipVertex> c,
  2. PolygonShape poly1,
  3. Transform xf1,
  4. int edge1,
  5. PolygonShape poly2,
  6. Transform xf2,
)

Implementation

void findIncidentEdge(
  List<ClipVertex> c,
  PolygonShape poly1,
  Transform xf1,
  int edge1,
  PolygonShape poly2,
  Transform xf2,
) {
  final count1 = poly1.vertices.length;
  final normals1 = poly1.normals;

  final count2 = poly2.vertices.length;
  final vertices2 = poly2.vertices;
  final normals2 = poly2.normals;

  assert(0 <= edge1 && edge1 < count1);

  final c0 = c[0];
  final c1 = c[1];
  final xf1q = xf1.q;
  final xf2q = xf2.q;

  // Get the normal of the reference edge in poly2's frame.
  final v = normals1[edge1];
  final tempx = xf1q.cos * v.x - xf1q.sin * v.y;
  final tempy = xf1q.sin * v.x + xf1q.cos * v.y;
  final normal1x = xf2q.cos * tempx + xf2q.sin * tempy;
  final normal1y = -xf2q.sin * tempx + xf2q.cos * tempy;

  // Find the incident edge on poly2.
  var index = 0;
  var minDot = double.maxFinite;
  for (var i = 0; i < count2; ++i) {
    final b = normals2[i];
    final dot = normal1x * b.x + normal1y * b.y;
    if (dot < minDot) {
      minDot = dot;
      index = i;
    }
  }

  // Build the clip vertices for the incident edge.
  final i1 = index;
  final i2 = i1 + 1 < count2 ? i1 + 1 : 0;

  final v1 = vertices2[i1];
  final out = c0.v;
  out.x = (xf2q.cos * v1.x - xf2q.sin * v1.y) + xf2.p.x;
  out.y = (xf2q.sin * v1.x + xf2q.cos * v1.y) + xf2.p.y;
  c0.id.indexA = edge1 & 0xFF;
  c0.id.indexB = i1 & 0xFF;
  c0.id.typeA = ContactIDType.face.index & 0xFF;
  c0.id.typeB = ContactIDType.vertex.index & 0xFF;

  final v2 = vertices2[i2];
  final out1 = c1.v;
  out1.x = (xf2q.cos * v2.x - xf2q.sin * v2.y) + xf2.p.x;
  out1.y = (xf2q.sin * v2.x + xf2q.cos * v2.y) + xf2.p.y;
  c1.id.indexA = edge1 & 0xFF;
  c1.id.indexB = i2 & 0xFF;
  c1.id.typeA = ContactIDType.face.index & 0xFF;
  c1.id.typeB = ContactIDType.vertex.index & 0xFF;
}