From 91f7b8cb6ab11d4d48b0dc19f3e5f462c8cdf6e3 Mon Sep 17 00:00:00 2001 From: Hwanseok Date: Fri, 24 Mar 2023 17:55:57 +0900 Subject: [PATCH 1/9] feat: Add efficient argument to moveToTarget --- packages/flame/lib/src/extensions/vector2.dart | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index ce5237e4269..4fa001e24d3 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -125,10 +125,16 @@ extension Vector2Extension on Vector2 { /// **not** a percentage (relative value). void moveToTarget( Vector2 target, - double ds, - ) { + double ds, { + Vector2? efficientDiff, + }) { if (this != target) { - final diff = target - this; + var diff = efficientDiff + ?..setFrom(target) + ..sub(this); + + diff ??= target - this; + if (diff.length < ds) { setFrom(target); } else { From ed7503db9ce076cec8ccc431c43b10f8b88511f6 Mon Sep 17 00:00:00 2001 From: Hwanseok Date: Fri, 24 Mar 2023 18:04:17 +0900 Subject: [PATCH 2/9] test: Extend tests --- .../flame/test/extensions/vector2_test.dart | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/flame/test/extensions/vector2_test.dart b/packages/flame/test/extensions/vector2_test.dart index 5cba3ea1bce..18531cb45cb 100644 --- a/packages/flame/test/extensions/vector2_test.dart +++ b/packages/flame/test/extensions/vector2_test.dart @@ -3,6 +3,7 @@ import 'dart:math'; import 'package:flame/extensions.dart'; import 'package:flame_test/flame_test.dart'; +import 'package:meta/meta.dart'; import 'package:test/test.dart'; void main() { @@ -306,11 +307,23 @@ void main() { }); group('Moving', () { - test('moveToTarget - fully horizontal', () { + @isTestGroup + void testWithAndWithoutEfficientDiff( + String desc, + dynamic Function(Vector2?) testBody, + ) { + final efficientDiff = Vector2.all(0); + test('$desc - with efficient', () => testBody(efficientDiff)); + test('$desc - without efficient', () => testBody(null)); + } + + testWithAndWithoutEfficientDiff('moveToTarget - fully horizontal', ( + Vector2? efficientDiff, + ) { final current = Vector2(10.0, 0.0); final target = Vector2(20.0, 0.0); - current.moveToTarget(target, 0); + current.moveToTarget(target, 0, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 0.0)); current.moveToTarget(target, 1); @@ -323,7 +336,9 @@ void main() { expect(current, Vector2(20.0, 0.0)); }); - test('moveToTarget - fully vertical', () { + testWithAndWithoutEfficientDiff('moveToTarget - fully vertical', ( + Vector2? efficientDiff, + ) { final current = Vector2(10.0, 0.0); final target = Vector2(10.0, 100.0); @@ -340,7 +355,9 @@ void main() { expect(current, Vector2(10.0, 100.0)); }); - test('moveToTarget - arbitrary direction', () { + testWithAndWithoutEfficientDiff('moveToTarget - arbitrary direction', ( + Vector2? efficientDiff, + ) { final current = Vector2(2.0, 2.0); final target = Vector2(4.0, 6.0); // direction is 1,2 From 21e0def5afb67fbd238304a4e2fcb937bd13e1bd Mon Sep 17 00:00:00 2001 From: Hwanseok Date: Fri, 24 Mar 2023 18:06:40 +0900 Subject: [PATCH 3/9] test: Fix test --- .../flame/test/extensions/vector2_test.dart | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/flame/test/extensions/vector2_test.dart b/packages/flame/test/extensions/vector2_test.dart index 18531cb45cb..93dc946e338 100644 --- a/packages/flame/test/extensions/vector2_test.dart +++ b/packages/flame/test/extensions/vector2_test.dart @@ -326,13 +326,13 @@ void main() { current.moveToTarget(target, 0, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 0.0)); - current.moveToTarget(target, 1); + current.moveToTarget(target, 1, efficientDiff: efficientDiff); expect(current, Vector2(11.0, 0.0)); - current.moveToTarget(target, 6); + current.moveToTarget(target, 6, efficientDiff: efficientDiff); expect(current, Vector2(17.0, 0.0)); - current.moveToTarget(target, 5); + current.moveToTarget(target, 5, efficientDiff: efficientDiff); expect(current, Vector2(20.0, 0.0)); }); @@ -342,16 +342,16 @@ void main() { final current = Vector2(10.0, 0.0); final target = Vector2(10.0, 100.0); - current.moveToTarget(target, 0); + current.moveToTarget(target, 0, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 0.0)); - current.moveToTarget(target, 1); + current.moveToTarget(target, 1, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 1.0)); - current.moveToTarget(target, 80); + current.moveToTarget(target, 80, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 81.0)); - current.moveToTarget(target, 19); + current.moveToTarget(target, 19, efficientDiff: efficientDiff); expect(current, Vector2(10.0, 100.0)); }); @@ -361,13 +361,21 @@ void main() { final current = Vector2(2.0, 2.0); final target = Vector2(4.0, 6.0); // direction is 1,2 - current.moveToTarget(target, 0); + current.moveToTarget(target, 0, efficientDiff: efficientDiff); expect(current, Vector2(2.0, 2.0)); - current.moveToTarget(target, math.sqrt(5)); + current.moveToTarget( + target, + math.sqrt(5), + efficientDiff: efficientDiff, + ); expect(current, Vector2(3.0, 4.0)); - current.moveToTarget(target, math.sqrt(5)); + current.moveToTarget( + target, + math.sqrt(5), + efficientDiff: efficientDiff, + ); expect(current, Vector2(4.0, 6.0)); }); }); From 92520dcb7cdbc6df4efcf3852e9d6dad457d1bd9 Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sat, 25 Mar 2023 00:54:23 +0900 Subject: [PATCH 4/9] docs: Write about efficientDiff --- packages/flame/lib/src/extensions/vector2.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index 4fa001e24d3..52a2977bf4a 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -123,6 +123,9 @@ extension Vector2Extension on Vector2 { /// /// Note: [ds] is the displacement vector in units of the vector space. It is /// **not** a percentage (relative value). + /// + /// The [efficientDiff] is an optional parameter that can be used to avoid + /// creating a new [Vector2] when calling this method multiple times. void moveToTarget( Vector2 target, double ds, { From 5f6d4b2d66fa6f892235ced6e238f365d009af27 Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sat, 25 Mar 2023 01:03:12 +0900 Subject: [PATCH 5/9] test: Fix test name --- packages/flame/test/extensions/vector2_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/flame/test/extensions/vector2_test.dart b/packages/flame/test/extensions/vector2_test.dart index 93dc946e338..ff302cc88a2 100644 --- a/packages/flame/test/extensions/vector2_test.dart +++ b/packages/flame/test/extensions/vector2_test.dart @@ -313,8 +313,8 @@ void main() { dynamic Function(Vector2?) testBody, ) { final efficientDiff = Vector2.all(0); - test('$desc - with efficient', () => testBody(efficientDiff)); - test('$desc - without efficient', () => testBody(null)); + test('$desc - with efficient diff', () => testBody(efficientDiff)); + test('$desc - without efficient diff', () => testBody(null)); } testWithAndWithoutEfficientDiff('moveToTarget - fully horizontal', ( From 5b5b750072c20f2a44b1f7607bf4bbef639abcef Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 26 Mar 2023 14:35:00 +0900 Subject: [PATCH 6/9] Change vector argument to the static member --- .../flame/lib/src/extensions/vector2.dart | 21 ++++---- .../flame/test/extensions/vector2_test.dart | 53 +++++-------------- 2 files changed, 25 insertions(+), 49 deletions(-) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index 52a2977bf4a..656fb5dfeea 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -6,6 +6,13 @@ import 'package:vector_math/vector_math_64.dart'; export 'package:vector_math/vector_math_64.dart' hide Colors; extension Vector2Extension on Vector2 { + /// This is a reusable vector that can be used with the [Vector2Extension] + /// to avoid creation of new Vector2 instances. + /// + /// Avoid using this in async extension methods, as it can lead to race + /// conditions. + static final _reusableVector = Vector2.zero(); + /// Creates an [Offset] from the [Vector2] Offset toOffset() => Offset(x, y); @@ -123,21 +130,15 @@ extension Vector2Extension on Vector2 { /// /// Note: [ds] is the displacement vector in units of the vector space. It is /// **not** a percentage (relative value). - /// - /// The [efficientDiff] is an optional parameter that can be used to avoid - /// creating a new [Vector2] when calling this method multiple times. void moveToTarget( Vector2 target, - double ds, { - Vector2? efficientDiff, - }) { + double ds, + ) { if (this != target) { - var diff = efficientDiff - ?..setFrom(target) + final diff = _reusableVector + ..setFrom(target) ..sub(this); - diff ??= target - this; - if (diff.length < ds) { setFrom(target); } else { diff --git a/packages/flame/test/extensions/vector2_test.dart b/packages/flame/test/extensions/vector2_test.dart index ff302cc88a2..5cba3ea1bce 100644 --- a/packages/flame/test/extensions/vector2_test.dart +++ b/packages/flame/test/extensions/vector2_test.dart @@ -3,7 +3,6 @@ import 'dart:math'; import 'package:flame/extensions.dart'; import 'package:flame_test/flame_test.dart'; -import 'package:meta/meta.dart'; import 'package:test/test.dart'; void main() { @@ -307,75 +306,51 @@ void main() { }); group('Moving', () { - @isTestGroup - void testWithAndWithoutEfficientDiff( - String desc, - dynamic Function(Vector2?) testBody, - ) { - final efficientDiff = Vector2.all(0); - test('$desc - with efficient diff', () => testBody(efficientDiff)); - test('$desc - without efficient diff', () => testBody(null)); - } - - testWithAndWithoutEfficientDiff('moveToTarget - fully horizontal', ( - Vector2? efficientDiff, - ) { + test('moveToTarget - fully horizontal', () { final current = Vector2(10.0, 0.0); final target = Vector2(20.0, 0.0); - current.moveToTarget(target, 0, efficientDiff: efficientDiff); + current.moveToTarget(target, 0); expect(current, Vector2(10.0, 0.0)); - current.moveToTarget(target, 1, efficientDiff: efficientDiff); + current.moveToTarget(target, 1); expect(current, Vector2(11.0, 0.0)); - current.moveToTarget(target, 6, efficientDiff: efficientDiff); + current.moveToTarget(target, 6); expect(current, Vector2(17.0, 0.0)); - current.moveToTarget(target, 5, efficientDiff: efficientDiff); + current.moveToTarget(target, 5); expect(current, Vector2(20.0, 0.0)); }); - testWithAndWithoutEfficientDiff('moveToTarget - fully vertical', ( - Vector2? efficientDiff, - ) { + test('moveToTarget - fully vertical', () { final current = Vector2(10.0, 0.0); final target = Vector2(10.0, 100.0); - current.moveToTarget(target, 0, efficientDiff: efficientDiff); + current.moveToTarget(target, 0); expect(current, Vector2(10.0, 0.0)); - current.moveToTarget(target, 1, efficientDiff: efficientDiff); + current.moveToTarget(target, 1); expect(current, Vector2(10.0, 1.0)); - current.moveToTarget(target, 80, efficientDiff: efficientDiff); + current.moveToTarget(target, 80); expect(current, Vector2(10.0, 81.0)); - current.moveToTarget(target, 19, efficientDiff: efficientDiff); + current.moveToTarget(target, 19); expect(current, Vector2(10.0, 100.0)); }); - testWithAndWithoutEfficientDiff('moveToTarget - arbitrary direction', ( - Vector2? efficientDiff, - ) { + test('moveToTarget - arbitrary direction', () { final current = Vector2(2.0, 2.0); final target = Vector2(4.0, 6.0); // direction is 1,2 - current.moveToTarget(target, 0, efficientDiff: efficientDiff); + current.moveToTarget(target, 0); expect(current, Vector2(2.0, 2.0)); - current.moveToTarget( - target, - math.sqrt(5), - efficientDiff: efficientDiff, - ); + current.moveToTarget(target, math.sqrt(5)); expect(current, Vector2(3.0, 4.0)); - current.moveToTarget( - target, - math.sqrt(5), - efficientDiff: efficientDiff, - ); + current.moveToTarget(target, math.sqrt(5)); expect(current, Vector2(4.0, 6.0)); }); }); From 4c0940aaa82b94f91e7263e953825e2281bf1260 Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 26 Mar 2023 14:39:02 +0900 Subject: [PATCH 7/9] feat: Reuse vector in lerp --- packages/flame/lib/src/extensions/vector2.dart | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index 656fb5dfeea..9b091f76f76 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -38,7 +38,13 @@ extension Vector2Extension on Vector2 { /// Linearly interpolate towards another Vector2 void lerp(Vector2 to, double t) { - setFrom(this + (to - this) * t); + setFrom( + _reusableVector + ..setFrom(to) + ..sub(this) + ..scale(t) + ..add(this), + ); } /// Whether the [Vector2] is the zero vector or not From cf37e38dcbf94af47544e8472c8e368756d3c73e Mon Sep 17 00:00:00 2001 From: Hwan-seok Date: Sun, 26 Mar 2023 14:44:47 +0900 Subject: [PATCH 8/9] feat: Reuse vector in the screenAngle --- packages/flame/lib/src/extensions/vector2.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index 9b091f76f76..84706d89120 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -165,7 +165,8 @@ extension Vector2Extension on Vector2 { /// Down: Vector(0.0, 1.0).screenAngle == +-pi /// Left: Vector(-1.0, 0.0).screenAngle == -pi/2 /// Right: Vector(-1.0, 0.0).screenAngle == pi/2 - double screenAngle() => (clone()..y *= -1).angleToSigned(Vector2(0.0, 1.0)); + double screenAngle() => (_reusableVector..setValues(x, y * (-1))) + .angleToSigned(Vector2(0.0, 1.0)); /// Modulo/Remainder Vector2 operator %(Vector2 mod) => Vector2(x % mod.x, y % mod.y); From 80cdebf4e5922b7392c2b8543b73e9284acc2bc5 Mon Sep 17 00:00:00 2001 From: Hwanseok Barth Kang Date: Sun, 26 Mar 2023 17:14:50 +0900 Subject: [PATCH 9/9] Update packages/flame/lib/src/extensions/vector2.dart Co-authored-by: Lukas Klingsbo --- packages/flame/lib/src/extensions/vector2.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/flame/lib/src/extensions/vector2.dart b/packages/flame/lib/src/extensions/vector2.dart index 84706d89120..f294de701ef 100644 --- a/packages/flame/lib/src/extensions/vector2.dart +++ b/packages/flame/lib/src/extensions/vector2.dart @@ -6,7 +6,7 @@ import 'package:vector_math/vector_math_64.dart'; export 'package:vector_math/vector_math_64.dart' hide Colors; extension Vector2Extension on Vector2 { - /// This is a reusable vector that can be used with the [Vector2Extension] + /// This is a reusable vector that can be used within the [Vector2Extension] /// to avoid creation of new Vector2 instances. /// /// Avoid using this in async extension methods, as it can lead to race