diff --git a/packages/flame/lib/src/collisions/hitboxes/polygon_hitbox.dart b/packages/flame/lib/src/collisions/hitboxes/polygon_hitbox.dart index 878458de07e..302f81ee2f4 100644 --- a/packages/flame/lib/src/collisions/hitboxes/polygon_hitbox.dart +++ b/packages/flame/lib/src/collisions/hitboxes/polygon_hitbox.dart @@ -40,6 +40,21 @@ class PolygonHitbox extends PolygonComponent this.collisionType = collisionType; } + /// With this constructor you create a regular (equiangular and equilateral) + /// polygon hitbox from number of sides and radius. + PolygonHitbox.regularPolygon( + super.sides, + super.radius, { + super.position, + super.angle, + super.anchor, + bool isSolid = false, + CollisionType collisionType = CollisionType.active, + }) : super.regularPolygon() { + this.isSolid = isSolid; + this.collisionType = collisionType; + } + @override @protected void computeAabb(Aabb2 aabb) { diff --git a/packages/flame/lib/src/geometry/polygon_component.dart b/packages/flame/lib/src/geometry/polygon_component.dart index 7c528584e71..93734b50675 100644 --- a/packages/flame/lib/src/geometry/polygon_component.dart +++ b/packages/flame/lib/src/geometry/polygon_component.dart @@ -22,9 +22,9 @@ class PolygonComponent extends ShapeComponent { final _cachedGlobalVertices = ValueCache>(); - /// With this constructor you create your [PolygonComponent] from positions in + /// With this constructor you create your [PolygonComponent] from positions /// anywhere in the 2d-space. It will automatically calculate the [size] of - /// the Polygon (the bounding box) if no size it given. + /// the Polygon (the bounding box) if no size is given. PolygonComponent( this._vertices, { super.position, @@ -93,6 +93,42 @@ class PolygonComponent extends ShapeComponent { children: children, ); + /// With this constructor you create a regular (equiangular and equilateral) + /// polygon from number of sides and radius anywhere in the 2d-space. It will + /// automatically calculate the [size] of the Polygon (the bounding box) if no + /// size is given. + PolygonComponent.regularPolygon( + int sides, + double radius, { + Vector2? position, + Vector2? size, + Vector2? scale, + double? angle, + Anchor? anchor, + Iterable? children, + int? priority, + Paint? paint, + List? paintLayers, + ComponentKey? key, + bool? shrinkToBounds, + }) : this( + List.generate(sides, (i) { + final angle = 2 * pi * i / sides; + return Vector2(radius * cos(angle), radius * sin(angle)); + }, growable: false), + position: position, + size: size, + scale: scale, + angle: angle, + anchor: anchor, + children: children, + priority: priority, + paint: paint, + paintLayers: paintLayers, + key: key, + shrinkToBounds: shrinkToBounds, + ); + @internal static List normalsToVertices( List normals,