Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Use a temp vector for delta calculations of FollowBehavior #3230

Merged
merged 2 commits into from
Jul 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/flame/lib/src/camera/behaviors/follow_behavior.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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:vector_math/vector_math_64.dart';

/// This behavior will make the [owner] follow the [target].
///
Expand Down Expand Up @@ -46,6 +47,8 @@ class FollowBehavior extends Component {
final bool horizontalOnly;
final bool verticalOnly;

final _tempDelta = Vector2.zero();

@override
void onMount() {
if (_owner == null) {
Expand All @@ -59,19 +62,17 @@ class FollowBehavior extends Component {

@override
void update(double dt) {
final delta = target.position - owner.position;
if (horizontalOnly) {
delta.y = 0;
}
if (verticalOnly) {
delta.x = 0;
}
final distance = delta.length;
_tempDelta.setValues(
verticalOnly ? 0 : target.position.x - owner.position.x,
horizontalOnly ? 0 : target.position.y - owner.position.y,
);

final distance = _tempDelta.length;
if (distance > _speed * dt) {
delta.scale(_speed * dt / distance);
_tempDelta.scale(_speed * dt / distance);
ufrshubham marked this conversation as resolved.
Show resolved Hide resolved
}
if (delta.x != 0 || delta.y != 0) {
owner.position = delta..add(owner.position);
if (_tempDelta.x != 0 || _tempDelta.y != 0) {
owner.position = _tempDelta..add(owner.position);
Copy link
Member

Choose a reason for hiding this comment

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

to go all the way avoiding object creation entirely, this could be owner.position.setValues(owner.position.x + delta.x, ...)

sadly we lose all the easiness of using vectorial math at all when doing things like this

Copy link
Member Author

Choose a reason for hiding this comment

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

this could be owner.position.setValues(owner.position.x + delta.x, ...)

I thought the same. But unfortunately doing so causes some of the tests to fail, because the .setValues bypasses the position setter of PositionProvider. Some components do extra stuff in that setter.

Copy link
Member

Choose a reason for hiding this comment

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

oof, seems like a footgun waiting to happen :(

}
}
}
Loading