Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions packages/flame/lib/src/collisions/hitboxes/polygon_hitbox.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 38 additions & 2 deletions packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class PolygonComponent extends ShapeComponent {

final _cachedGlobalVertices = ValueCache<List<Vector2>>();

/// 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,
Expand Down Expand Up @@ -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(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert to regular like you had initially, it doesn't make it any clearer by repeating polygon.
I had forgot regular is the actual math term for these types of polygons, so let's go with that.

int sides,
double radius, {
Vector2? position,
Vector2? size,
Vector2? scale,
double? angle,
Anchor? anchor,
Iterable<Component>? children,
int? priority,
Paint? paint,
List<Paint>? 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<Vector2> normalsToVertices(
List<Vector2> normals,
Expand Down
Loading