From c5ca5d465f7d5c50aeb1c15bcc23e1c1a7a24dd7 Mon Sep 17 00:00:00 2001 From: DevKage Date: Thu, 19 Nov 2026 17:40:55 +0530 Subject: [PATCH 1/2] fix: Follow a target nested in another component with FollowBehavior FollowBehavior moved its owner towards the target's position relative to its parent, so CameraComponent.follow pointed the camera at the wrong place when the target was a child of another PositionComponent, and so did follow with snap. The target's position is now converted into the coordinate system of the owner's position when the target is a PositionComponent with another parent than the owner. --- .../src/camera/behaviors/follow_behavior.dart | 34 ++++- .../lib/src/camera/camera_component.dart | 23 +-- .../behaviors/follow_behavior_test.dart | 144 ++++++++++++++++++ .../test/camera/camera_component_test.dart | 40 +++++ 4 files changed, 228 insertions(+), 13 deletions(-) diff --git a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart index bf977da7166..dcf622dbe7c 100644 --- a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart +++ b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart @@ -3,6 +3,7 @@ import 'package:flame/src/camera/viewport.dart'; import 'package:flame/src/components/core/component.dart'; import 'package:flame/src/components/position_component.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; +import 'package:meta/meta.dart'; import 'package:vector_math/vector_math.dart'; /// This behavior will make the [owner] follow the [target]. @@ -17,6 +18,10 @@ import 'package:vector_math/vector_math.dart'; /// /// The flags [horizontalOnly]/[verticalOnly] allow constraining the [owner]'s /// movement to the horizontal/vertical directions respectively. +/// +/// If the [target] is a [PositionComponent], the owner follows it wherever it +/// is in the component tree, even if it's a child of another +/// [PositionComponent]. class FollowBehavior({ required final ReadOnlyPositionProvider _target, var PositionProvider? _owner, @@ -51,11 +56,36 @@ class FollowBehavior({ } } + /// The position of the [target] in the coordinate system of the [owner]'s + /// position. + /// + /// The position of a [PositionComponent] is relative to its parent, so if + /// the target is a [PositionComponent] with another parent than the owner, + /// its absolute position is converted into the owner's coordinate system. + /// The position of an owner that isn't a [PositionComponent], such as a + /// [Viewfinder], is in world coordinates. + @internal + Vector2 get targetPosition { + final target = _target; + if (target is! PositionComponent) { + return target.position; + } + final owner = _owner; + if (owner is PositionComponent) { + if (owner.parent == target.parent) { + return target.position; + } + return owner.positionOf(owner.absoluteToLocal(target.absolutePosition)); + } + return target.absolutePosition; + } + @override void update(double dt) { + final targetPosition = this.targetPosition; _tempDelta.setValues( - verticalOnly ? 0 : target.position.x - owner.position.x, - horizontalOnly ? 0 : target.position.y - owner.position.y, + verticalOnly ? 0 : targetPosition.x - owner.position.x, + horizontalOnly ? 0 : targetPosition.y - owner.position.y, ); final distance = _tempDelta.length; diff --git a/packages/flame/lib/src/camera/camera_component.dart b/packages/flame/lib/src/camera/camera_component.dart index cff0c14a56e..685ab577c39 100644 --- a/packages/flame/lib/src/camera/camera_component.dart +++ b/packages/flame/lib/src/camera/camera_component.dart @@ -354,8 +354,10 @@ class CameraComponent({ /// Makes the [viewfinder] follow the given [target]. /// /// The [target] here can be any read-only [PositionProvider]. For example, a - /// [PositionComponent] is the most common choice of target. Alternatively, - /// you can use [PositionProviderImpl] to construct the target dynamically. + /// [PositionComponent] is the most common choice of target, and it's + /// followed at its position in the world even when it's a child of another + /// [PositionComponent]. Alternatively, you can use [PositionProviderImpl] to + /// construct the target dynamically. /// /// This method adds a [FollowBehavior] to the viewfinder. If there is another /// [FollowBehavior] currently applied to the viewfinder, it will be removed @@ -376,17 +378,16 @@ class CameraComponent({ bool snap = false, }) { stop(); - viewfinder.add( - FollowBehavior( - target: target, - owner: viewfinder, - maxSpeed: maxSpeed, - horizontalOnly: horizontalOnly, - verticalOnly: verticalOnly, - ), + final followBehavior = FollowBehavior( + target: target, + owner: viewfinder, + maxSpeed: maxSpeed, + horizontalOnly: horizontalOnly, + verticalOnly: verticalOnly, ); + viewfinder.add(followBehavior); if (snap) { - viewfinder.position = target.position; + viewfinder.position = followBehavior.targetPosition; } } diff --git a/packages/flame/test/camera/behaviors/follow_behavior_test.dart b/packages/flame/test/camera/behaviors/follow_behavior_test.dart index 36a2aa6d1d3..e9a9bd557d6 100644 --- a/packages/flame/test/camera/behaviors/follow_behavior_test.dart +++ b/packages/flame/test/camera/behaviors/follow_behavior_test.dart @@ -1,3 +1,5 @@ +import 'dart:math'; + import 'package:flame/camera.dart'; import 'package:flame/components.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; @@ -158,5 +160,147 @@ void main() { game.update(1); } }); + + group('when the target and the owner have different parents', () { + testWithFlameGame('follows a nested target', (game) async { + final container = PositionComponent( + position: Vector2(100, 50), + scale: Vector2.all(2), + angle: pi / 2, + )..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(game); + await game.ready(); + + game.update(0.01); + // (20, 30) scaled by 2 and turned by a quarter turn is (-60, 40). + expect(pursuer.position, closeToVector(Vector2(40, 90), 1e-4)); + }); + + testWithFlameGame('follows a target from a nested owner', (game) async { + final target = PositionComponent(position: Vector2(300, 200)) + ..addToParent(game); + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(200, 150)); + expect(pursuer.absolutePosition, target.absolutePosition); + }); + + testWithFlameGame( + 'follows a target within the same nested parent', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(20, 30)); + }, + ); + + testWithFlameGame( + 'follows a target nested deeper in the parent of the owner', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final group = PositionComponent( + position: Vector2(10, 10), + scale: Vector2.all(2), + )..addToParent(container); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(group); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(50, 70)); + expect(pursuer.absolutePosition, target.absolutePosition); + }, + ); + + testWithFlameGame( + 'follows a target in another branch of a transformed ancestor', + (game) async { + final level = PositionComponent( + position: Vector2(100, 100), + scale: Vector2.all(2), + angle: pi / 3, + )..addToParent(game); + final targetGroup = PositionComponent( + position: Vector2(10, 20), + angle: -pi / 4, + )..addToParent(level); + final pursuerGroup = PositionComponent( + position: Vector2(-30, 40), + scale: Vector2.all(0.5), + )..addToParent(level); + final target = PositionComponent(position: Vector2(25, -15)) + ..addToParent(targetGroup); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target)) + ..addToParent(pursuerGroup); + await game.ready(); + + game.update(0.01); + expect( + pursuer.absolutePosition, + closeToVector(target.absolutePosition, 1e-3), + ); + }, + ); + + testWithFlameGame( + 'follows a nested target with a custom owner', + (game) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(20, 30)) + ..addToParent(container); + final ownerPosition = Vector2.zero(); + final owner = PositionProviderImpl( + getValue: () => ownerPosition, + setValue: ownerPosition.setFrom, + ); + game.add(FollowBehavior(target: target, owner: owner)); + await game.ready(); + + game.update(0.01); + expect(ownerPosition, Vector2(120, 80)); + }, + ); + + testWithFlameGame('moves at max speed towards a nested target', ( + game, + ) async { + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final target = PositionComponent(position: Vector2(200, 350)) + ..addToParent(container); + final pursuer = PositionComponent() + ..add(FollowBehavior(target: target, maxSpeed: 50)) + ..addToParent(game); + await game.ready(); + + // The target is at (300, 400), which is 500 units away. + game.update(1); + expect(pursuer.position, closeToVector(Vector2(30, 40), 1e-4)); + }); + }); }); } diff --git a/packages/flame/test/camera/camera_component_test.dart b/packages/flame/test/camera/camera_component_test.dart index 425f1f83c63..318eefdb677 100644 --- a/packages/flame/test/camera/camera_component_test.dart +++ b/packages/flame/test/camera/camera_component_test.dart @@ -89,6 +89,46 @@ void main() { expect(camera.viewfinder.position, Vector2(100, 100)); }); + testWithFlameGame('follow a target nested in another component', ( + game, + ) async { + final world = World()..addToParent(game); + final camera = CameraComponent(world: world)..addToParent(game); + final ship = PositionComponent(position: Vector2(100, 200)) + ..addToParent(world); + final pilot = PositionComponent(position: Vector2(10, 20)) + ..addToParent(ship); + camera.follow(pilot); + await game.ready(); + + for (var i = 0; i < 20; i++) { + ship.position.add(Vector2(i * 5.0, 20.0 - i)); + game.update(0.01); + expect( + camera.viewfinder.position, + closeToVector(pilot.absolutePosition), + ); + } + }); + + testWithFlameGame('follow with snap a target nested in another component', ( + game, + ) async { + final world = World()..addToParent(game); + final ship = PositionComponent( + position: Vector2(100, 200), + scale: Vector2.all(2), + )..addToParent(world); + final pilot = PositionComponent(position: Vector2(10, 20)) + ..addToParent(ship); + final camera = CameraComponent(world: world) + ..follow(pilot, maxSpeed: 1, snap: true) + ..addToParent(game); + await game.ready(); + + expect(camera.viewfinder.position, Vector2(120, 240)); + }); + testWithFlameGame('moveTo', (game) async { final world = World()..addToParent(game); final camera = CameraComponent(world: world)..addToParent(game); From 84aee5401a0d941ccd17c043a1d141051927456b Mon Sep 17 00:00:00 2001 From: DevKage Date: Sat, 21 Nov 2026 00:59:58 +0530 Subject: [PATCH 2/2] fix: Keep the owner's scale and the world's ancestors out of FollowBehavior A follower with a zero scale couldn't convert the target's position through its own transform, so the position is now converted into the coordinate system of the follower's parent instead. For an owner that isn't a PositionComponent, such as the Viewfinder, the target's position is converted only up to the World that it's in, since the camera renders the world without the transforms of the components that the world is in. --- .../src/camera/behaviors/follow_behavior.dart | 28 +++++++++++++++---- .../behaviors/follow_behavior_test.dart | 18 ++++++++++++ .../test/camera/camera_component_test.dart | 23 +++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart index dcf622dbe7c..38d0858502c 100644 --- a/packages/flame/lib/src/camera/behaviors/follow_behavior.dart +++ b/packages/flame/lib/src/camera/behaviors/follow_behavior.dart @@ -1,5 +1,6 @@ import 'package:flame/src/camera/viewfinder.dart'; import 'package:flame/src/camera/viewport.dart'; +import 'package:flame/src/camera/world.dart'; import 'package:flame/src/components/core/component.dart'; import 'package:flame/src/components/position_component.dart'; import 'package:flame/src/effects/provider_interfaces.dart'; @@ -61,9 +62,11 @@ class FollowBehavior({ /// /// The position of a [PositionComponent] is relative to its parent, so if /// the target is a [PositionComponent] with another parent than the owner, - /// its absolute position is converted into the owner's coordinate system. - /// The position of an owner that isn't a [PositionComponent], such as a - /// [Viewfinder], is in world coordinates. + /// its position is converted into the coordinate system of the owner's + /// parent. The position of an owner that isn't a [PositionComponent], such + /// as a [Viewfinder], is in the coordinate system of the [World] that the + /// target is in, which is rendered without the transforms of the + /// components that the world itself is in. @internal Vector2 get targetPosition { final target = _target; @@ -75,9 +78,24 @@ class FollowBehavior({ if (owner.parent == target.parent) { return target.position; } - return owner.positionOf(owner.absoluteToLocal(target.absolutePosition)); + // The owner's parent is used rather than the owner itself, so that the + // owner's own transform, e.g. a zero scale, doesn't affect the result. + final absolutePosition = target.absolutePosition; + return owner.findParent()?.absoluteToLocal( + absolutePosition, + ) ?? + absolutePosition; } - return target.absolutePosition; + Vector2 position = target.position; + for (final ancestor in target.ancestors()) { + if (ancestor is World) { + break; + } + if (ancestor is PositionComponent) { + position = ancestor.positionOf(position); + } + } + return position; } @override diff --git a/packages/flame/test/camera/behaviors/follow_behavior_test.dart b/packages/flame/test/camera/behaviors/follow_behavior_test.dart index e9a9bd557d6..9cd2239cd4b 100644 --- a/packages/flame/test/camera/behaviors/follow_behavior_test.dart +++ b/packages/flame/test/camera/behaviors/follow_behavior_test.dart @@ -265,6 +265,24 @@ void main() { }, ); + testWithFlameGame( + 'follows a target with a different parent at a zero scale', + (game) async { + final target = PositionComponent(position: Vector2(300, 200)) + ..addToParent(game); + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + // A follower that scales in from zero, e.g. with a ScaleEffect. + final pursuer = PositionComponent(scale: Vector2.zero()) + ..add(FollowBehavior(target: target)) + ..addToParent(container); + await game.ready(); + + game.update(0.01); + expect(pursuer.position, Vector2(200, 150)); + }, + ); + testWithFlameGame( 'follows a nested target with a custom owner', (game) async { diff --git a/packages/flame/test/camera/camera_component_test.dart b/packages/flame/test/camera/camera_component_test.dart index 318eefdb677..4af088c2458 100644 --- a/packages/flame/test/camera/camera_component_test.dart +++ b/packages/flame/test/camera/camera_component_test.dart @@ -129,6 +129,29 @@ void main() { expect(camera.viewfinder.position, Vector2(120, 240)); }); + testWithFlameGame('follow a target in a world within a component', ( + game, + ) async { + // The camera renders the world without the transforms of the components + // that the world is in, so they don't move the camera either. + final container = PositionComponent(position: Vector2(100, 50)) + ..addToParent(game); + final world = World()..addToParent(container); + final ship = PositionComponent(position: Vector2(10, 20)) + ..addToParent(world); + final pilot = PositionComponent(position: Vector2(5, 5)) + ..addToParent(ship); + final camera = CameraComponent(world: world) + ..follow(ship, snap: true) + ..addToParent(game); + await game.ready(); + expect(camera.viewfinder.position, Vector2(10, 20)); + + camera.follow(pilot); + game.update(0.01); + expect(camera.viewfinder.position, Vector2(15, 25)); + }); + testWithFlameGame('moveTo', (game) async { final world = World()..addToParent(game); final camera = CameraComponent(world: world)..addToParent(game);