intersectPolygons static method

({double depth, Vector2 normal}) intersectPolygons(
  1. ShapeHitbox shapeA,
  2. ShapeHitbox shapeB,
  3. PositionComponent other
)

Implementation

static ({Vector2 normal, double depth}) intersectPolygons(
  ShapeHitbox shapeA,
  ShapeHitbox shapeB,
  PositionComponent other,
) {
  var normal = Vector2.zero();
  var depth = double.maxFinite;

  final verticesA = CollisionUtil.getPolygonVertices(shapeA);
  final verticesB = CollisionUtil.getPolygonVertices(shapeB);

  final normalAndDepthA = CollisionUtil.getNormalAndDepth(
    verticesA,
    verticesB,
  );

  if (normalAndDepthA.depth < depth) {
    depth = normalAndDepthA.depth;
    normal = normalAndDepthA.normal;
  }
  final normalAndDepthB = CollisionUtil.getNormalAndDepth(
    verticesB,
    verticesA,
    insverted: true,
  );

  if (normalAndDepthB.depth < depth) {
    depth = normalAndDepthB.depth;
    normal = normalAndDepthB.normal;
  }

  final direction = shapeB.absoluteCenter - shapeA.absoluteCenter;

  if (direction.dot(normal) < 0) {
    normal = -normal;
  }

  return (normal: normal, depth: depth);
}