Skip to content

fix: Follow a target nested in another component with FollowBehavior - #4239

Merged
ufrshubham merged 4 commits into
mainfrom
devkage/fix-camera-follow-nested-target
Nov 23, 2026
Merged

ufrshubham merged 4 commits into
mainfrom
devkage/fix-camera-follow-nested-target

Conversation

@ufrshubham

@ufrshubham ufrshubham commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Description

FollowBehavior moved its owner towards target.position, which is the target's position relative to its parent. So when the target was a child of another PositionComponent, like a character carried by a moving platform, CameraComponent.follow pointed the camera at the target's position on the platform instead of its position in the world, and follow with snap: true did the same. A FollowBehavior between two components with different parents had the same problem.

When the target is a PositionComponent with another parent than the owner, its position is now converted into the coordinate system of the owner's position: for an owner that isn't a PositionComponent, such as the camera's Viewfinder, through the target's ancestors 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, and for a PositionComponent owner, into the coordinate system of the owner's parent through absoluteToLocal, so that the owner's own transform, like a zero scale, doesn't affect it. When the target and the owner have the same parent, target.position is used as before, and targets that aren't PositionComponents, such as a PositionProviderImpl, are followed at their position as given. follow with snap: true uses the same position.

issue-4238-repro_compare.mp4

A camera following a direct child of the world, and followers that have the same parent as their targets, use target.position as before. Converting the position of a nested target costs a little more: in a release build, about 9 ns for each level that a camera's target is nested in, and around 90 ns per update for a follower in another container than its target. With one camera, the difference doesn't show up in the time of a whole game tick.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #4238

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Active coordinate-transform defects can stop following when the follower has zero scale and make viewfinder follow and snap focus incorrectly when the viewed world has transformed ancestors.

2 open issues
What changed in this PR

This PR addresses #4238 by making FollowBehavior account for nested targets and different parent coordinate systems, including camera snapping.

Changes:

  • Convert target positions into the follower’s coordinate system.
  • Reuse the conversion for CameraComponent.follow(snap: true) and document nested-target support.
  • Add tests for nested targets, transformed ancestors, custom owners, and speed limits.
File Description
packages/​flame/​test/​camera/​camera_component_test.dart Tests camera following and snapping to nested targets.
packages/​flame/​test/​camera/​behaviors/​follow_behavior_test.dart Tests following across component hierarchies and transforms.
packages/​flame/​lib/​src/​camera/​camera_component.dart Uses the converted target position for snapping and updates documentation.
packages/​flame/​lib/​src/​camera/​behaviors/​follow_behavior.dart Adds coordinate conversion for targets with different parents.

Review effort: Balanced

…havior

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Coordinate conversion can misposition followers when a shared ancestor has zero scale or a viewport owner’s parent-relative position is incorrectly treated as world-relative.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Convert viewport positions to parent-relative coordinates

packages/​flame/​lib/​src/​camera/​behaviors/​follow_behavior.dart:77

Viewport is also a supported owner, but its position is parent-relative (viewport.dart:36–45), not world-relative. If a camera and target are inside a container at (100, 50), following a target at (20, 30) with the viewport now sets its position to (120, 80), applying the container's translation twice. Please include viewport owners in the parent-coordinate conversion instead of sending them through the world-coordinate branch.

Medium severity Avoid shared-ancestor transforms in nested coordinate conversion

packages/​flame/​lib/​src/​camera/​behaviors/​follow_behavior.dart:87

The absolute-coordinate round trip loses valid local coordinates when a shared ancestor has a zero scale on either axis, even if that ancestor is above the rendered World. For example, setting the shared container's scale to Vector2(0, 1) in the new nested-target test makes the destination (0, 0) instead of (50, 70), incorrectly moving the follower on both axes. Convert along the paths below the lowest common ancestor without applying or inverting its transform, and add a regression test for a zero-scaled shared ancestor.

2 resolved since last review

Review effort: Balanced

@ufrshubham
ufrshubham enabled auto-merge (squash) November 23, 2026 17:24
@ufrshubham
ufrshubham merged commit 36c307b into main Nov 23, 2026
9 checks passed
@ufrshubham
ufrshubham deleted the devkage/fix-camera-follow-nested-target branch November 23, 2026 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CameraComponent.follow follows the local position of a nested target

3 participants