onCollision method

  1. @override
void onCollision(
  1. Set<Vector2> intersectionPoints,
  2. PositionComponent other
)
override

onCollision is called in every tick when this object is colliding with other.

Implementation

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
  if (other is Sensor || !_blockMovementCollisionEnabled) {
    super.onCollision(intersectionPoints, other);
    return;
  }
  bool stopOtherMovement = true;
  bool stopMovement = other is GameComponent
      ? onBlockMovement(intersectionPoints, other)
      : true;
  if (other is BlockMovementCollision) {
    stopOtherMovement = other.onBlockMovement(
      intersectionPoints,
      this,
    );
  }

  if (!stopMovement || !stopOtherMovement) {
    super.onCollision(intersectionPoints, other);
    return;
  }

  if (_collisionsResolution.containsKey(other)) {
    onBlockedMovement(
      other,
      _collisionsResolution[other]!,
    );
    _collisionsResolution.remove(other);
    super.onCollision(intersectionPoints, other);
    return;
  }

  ShapeHitbox? shape1 = _getCollisionShapeHitbox(
    shapeHitboxes,
    intersectionPoints,
  );
  ShapeHitbox? shape2 = _getCollisionShapeHitbox(
    other.children.query<ShapeHitbox>(),
    intersectionPoints,
  );

  if (shape1 == null || shape2 == null) {
    super.onCollision(intersectionPoints, other);
    return;
  }

  ({Vector2 normal, double depth})? colisionResult;

  if (_isPolygon(shape1)) {
    if (_isPolygon(shape2)) {
      colisionResult = _intersectPolygons(shape1, shape2, other);
    } else if (shape2 is CircleHitbox) {
      colisionResult = _intersectCirclePolygon(shape1, shape2, other);
    }
  } else if (shape1 is CircleHitbox) {
    if (_isPolygon(shape2)) {
      colisionResult = _intersectCirclePolygon(
        shape2,
        shape1,
        other,
        inverted: true,
      );
    } else if (shape2 is CircleHitbox) {
      colisionResult = _intersectCircles(shape1, shape2);
    }
  }

  if (colisionResult != null) {
    final data = CollisionData(
      normal: colisionResult.normal,
      depth: colisionResult.depth,
      intersectionPoints: intersectionPoints.toList(),
      direction: colisionResult.normal.toDirection(),
    );
    onBlockedMovement(other, data);
    if (other is BlockMovementCollision) {
      other.setCollisionResolution(this, data.inverted());
    }
  }
  super.onCollision(intersectionPoints, other);
}