clipSegmentToLine static method

int clipSegmentToLine(
  1. List<ClipVertex> vOut,
  2. List<ClipVertex> vIn,
  3. Vector2 normal,
  4. double offset,
  5. int vertexIndexA,
)

Clipping for contact manifolds. Sutherland-Hodgman clipping.

Implementation

static int clipSegmentToLine(
  List<ClipVertex> vOut,
  List<ClipVertex> vIn,
  Vector2 normal,
  double offset,
  int vertexIndexA,
) {
  // Start with no _output points
  var numOut = 0;
  final vIn0 = vIn[0];
  final vIn1 = vIn[1];
  final vIn0v = vIn0.v;
  final vIn1v = vIn1.v;

  // Calculate the distance of end points to the line
  final distance0 = normal.dot(vIn0v) - offset;
  final distance1 = normal.dot(vIn1v) - offset;

  // If the points are behind the plane
  if (distance0 <= 0.0) {
    vOut[numOut++].set(vIn0);
  }
  if (distance1 <= 0.0) {
    vOut[numOut++].set(vIn1);
  }

  // If the points are on different sides of the plane
  if (distance0 * distance1 < 0.0) {
    // Find intersection point of edge and plane
    final interp = distance0 / (distance0 - distance1);

    final vOutNO = vOut[numOut];
    // vOut[numOut].v = vIn[0].v + interp * (vIn[1].v - vIn[0].v);
    vOutNO.v.x = vIn0v.x + interp * (vIn1v.x - vIn0v.x);
    vOutNO.v.y = vIn0v.y + interp * (vIn1v.y - vIn0v.y);

    // VertexA is hitting edgeB.
    vOutNO.id.indexA = vertexIndexA & 0xFF;
    vOutNO.id.indexB = vIn0.id.indexB;
    vOutNO.id.typeA = ContactIDType.vertex.index & 0xFF;
    vOutNO.id.typeB = ContactIDType.face.index & 0xFF;
    ++numOut;
  }

  return numOut;
}