Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Misalignment of the hittest area of PolygonHitbox #2930

Merged
merged 6 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,8 @@ class PolygonComponent extends ShapeComponent {
}
for (var i = 0; i < _vertices.length; i++) {
final edge = getEdge(i, vertices: vertices);
final isOutside = (edge.to.x - edge.from.x) *
(point.y - edge.from.y + _topLeft.y) -
(point.x - edge.from.x + _topLeft.x) * (edge.to.y - edge.from.y) >
final isOutside = (edge.to.x - edge.from.x) * (point.y - edge.from.y) -
(point.x - edge.from.x) * (edge.to.y - edge.from.y) >
0;
if (isOutside) {
return false;
Expand Down
13 changes: 13 additions & 0 deletions packages/flame/test/components/shape_component_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ void main() {
);
});

test('polygon contains point in local', () {
final polygon = PolygonComponent(
[
Vector2(0.5, 0.5),
Vector2(0.5, 1.5),
Vector2(1.5, 1.5),
Vector2(1.5, 0.5),
],
);
expect(polygon.containsLocalPoint(Vector2(0.25, 0.25)), isFalse);
expect(polygon.containsLocalPoint(Vector2(0.75, 0.75)), isTrue);
});

test('rotated circle does not contain point', () {
final component = CircleComponent(
radius: 1.0,
Expand Down