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 4 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
4 changes: 2 additions & 2 deletions packages/flame/lib/src/geometry/polygon_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,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) >
(point.y - edge.from.y) -
(point.x - edge.from.x) * (edge.to.y - edge.from.y) >
0;
if (isOutside) {
return false;
Expand Down
40 changes: 40 additions & 0 deletions packages/flame/test/geometry/polygon_component_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flame/components.dart';
import 'package:flame/geometry.dart';

Check notice on line 2 in packages/flame/test/geometry/polygon_component_test.dart

View workflow job for this annotation

GitHub Actions / analyze

The import of 'package:flame/geometry.dart' is unnecessary because all of the used elements are also provided by the import of 'package:flame/components.dart'.

Try removing the import directive. See https://dart.dev/diagnostics/unnecessary_import to learn more about this problem.

Check notice on line 2 in packages/flame/test/geometry/polygon_component_test.dart

View workflow job for this annotation

GitHub Actions / analyze-latest

The import of 'package:flame/geometry.dart' is unnecessary because all of the used elements are also provided by the import of 'package:flame/components.dart'.

Try removing the import directive. See https://dart.dev/diagnostics/unnecessary_import to learn more about this problem.
import 'package:test/test.dart';

void main() {
group('PolygonComponent', () {
Copy link
Member

Choose a reason for hiding this comment

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

Could you move this to shape_component_test where the other tests for this are?
There are some inconsistencies of which directories the tests live currently so it can be quite hard to find the correct place. 😅

test('HitTest for PolygonComponent', () {
final polygon = PolygonComponent(
[
Vector2(50, 50),
Vector2(50, 150),
Vector2(150, 150),
Vector2(150, 50),
],
);
expect(polygon.containsLocalPoint(Vector2(75, 25)), isFalse);
expect(polygon.containsLocalPoint(Vector2(75, 75)), isTrue);
expect(polygon.containsLocalPoint(Vector2(75, 125)), isTrue);
expect(polygon.containsLocalPoint(Vector2(25, 75)), isFalse);
expect(polygon.containsLocalPoint(Vector2(125, 75)), isTrue);
});

test('HitTest for PolygonComponent.relative', () {
final polygon = PolygonComponent.relative(
[
Vector2(-0.5, -0.5),
Vector2(-0.5, 0.5),
Vector2(0.5, 0.5),
Vector2(0.5, -0.5),
],
parentSize: Vector2.all(200),
);
expect(polygon.containsLocalPoint(Vector2(75, 25)), isFalse);
expect(polygon.containsLocalPoint(Vector2(75, 75)), isTrue);
expect(polygon.containsLocalPoint(Vector2(75, 125)), isTrue);
expect(polygon.containsLocalPoint(Vector2(25, 75)), isFalse);
expect(polygon.containsLocalPoint(Vector2(125, 75)), isTrue);
});
});
}
Loading