You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: Follow a target nested in another component with FollowBehavior - #4239
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.
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.
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.
…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.
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
Convert viewport positions to parent-relative coordinates
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.
Avoid shared-ancestor transforms in nested coordinate conversion
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
FollowBehaviormoved its owner towardstarget.position, which is the target's position relative to its parent. So when the target was a child of anotherPositionComponent, like a character carried by a moving platform,CameraComponent.followpointed the camera at the target's position on the platform instead of its position in the world, andfollowwithsnap: truedid the same. AFollowBehaviorbetween two components with different parents had the same problem.When the target is a
PositionComponentwith 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 aPositionComponent, such as the camera'sViewfinder, through the target's ancestors up to theWorldthat it's in, since the camera renders the world without the transforms of the components that the world is in, and for aPositionComponentowner, into the coordinate system of the owner's parent throughabsoluteToLocal, 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.positionis used as before, and targets that aren'tPositionComponents, such as aPositionProviderImpl, are followed at their position as given.followwithsnap: trueuses 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.positionas 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
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Closes #4238