diff --git a/packages/flame/lib/src/geometry/polygon_component.dart b/packages/flame/lib/src/geometry/polygon_component.dart index 2ced1a334c7..db9b03e6333 100644 --- a/packages/flame/lib/src/geometry/polygon_component.dart +++ b/packages/flame/lib/src/geometry/polygon_component.dart @@ -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; diff --git a/packages/flame/test/components/shape_component_test.dart b/packages/flame/test/components/shape_component_test.dart index fa3ab417297..e12e1338b31 100644 --- a/packages/flame/test/components/shape_component_test.dart +++ b/packages/flame/test/components/shape_component_test.dart @@ -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,