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

  // Calculate the distance of end points to the line
  double distance0 = normal.dot(vIn0v) - offset;
  double 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
    double interp = distance0 / (distance0 - distance1);

    ClipVertex 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;
}