computeAabb method

  1. @override
  2. @protected
void computeAabb(
  1. Aabb2 aabb
)
override

Computes the axis-aligned bounding box for this hitbox.

Subclasses can override this to provide a tighter AABB. The default implementation uses the absolute scaled size and rotation.

Implementation

@override
@protected
void computeAabb(Aabb2 aabb) {
  final vertices = globalVertices();
  if (vertices.isEmpty) {
    super.computeAabb(aabb);
    return;
  }
  var minX = vertices[0].x;
  var minY = vertices[0].y;
  var maxX = vertices[0].x;
  var maxY = vertices[0].y;
  for (var i = 1; i < vertices.length; i++) {
    final v = vertices[i];
    if (v.x < minX) {
      minX = v.x;
    }
    if (v.y < minY) {
      minY = v.y;
    }
    if (v.x > maxX) {
      maxX = v.x;
    }
    if (v.y > maxY) {
      maxY = v.y;
    }
  }
  // Add a small epsilon since points on the AABB edge are counted as outside.
  const epsilon = 0.000000000000001;
  aabb.min.setValues(minX - epsilon, minY - epsilon);
  aabb.max.setValues(maxX + epsilon, maxY + epsilon);
}