Created
June 12, 2022 23:52
-
-
Save eseidel/5ddd15f9806bb514f06f3cbab2306445 to your computer and use it in GitHub Desktop.
Seek is not stabilizing as expected.
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
Log: | |
flutter: position: [575.9139264665203,261.86483938500254] velocity: [9.772995914806668,-2.1186200341666246] maxSpeed: 10.0 adjustedMaxSpeed: 10.0 dt: 0.016667 distance: 2.8728173518205047 newVelocity: [-9.772995914806673,2.118620034166597] | |
flutter: position: [566.1409305517136,263.98345941916915] velocity: [-9.772995914806673,2.118620034166597] maxSpeed: 10.0 adjustedMaxSpeed: 10.0 dt: 0.016666 distance: 7.127182648179469 newVelocity: [9.772995914806668,-2.1186200341666246] | |
flutter: position: [575.9139264665203,261.86483938500254] velocity: [9.772995914806668,-2.1186200341666246] maxSpeed: 10.0 adjustedMaxSpeed: 10.0 dt: 0.016667 distance: 2.8728173518205047 newVelocity: [-9.772995914806673,2.118620034166597] | |
flutter: position: [566.1409305517136,263.98345941916915] velocity: [-9.772995914806673,2.118620034166597] maxSpeed: 10.0 adjustedMaxSpeed: 10.0 dt: 0.016667 distance: 7.127182648179469 newVelocity: [9.772995914806668,-2.1186200341666246] | |
flutter: position: [575.9139264665203,261.86483938500254] velocity: [9.772995914806668,-2.1186200341666246] maxSpeed: 10.0 adjustedMaxSpeed: 10.0 dt: 0.016666 distance: 2.8728173518205047 newVelocity: [-9.772995914806673,2.118620034166597] | |
diff --git a/lib/src/steering/behaviors/seek.dart b/lib/src/steering/behaviors/seek.dart | |
index db85949..b4f17c4 100644 | |
--- a/lib/src/steering/behaviors/seek.dart | |
+++ b/lib/src/steering/behaviors/seek.dart | |
@@ -44,9 +44,12 @@ class SeekLight extends Seek { | |
final offset = target - own.position; | |
final distance = offset.normalize(); | |
var maxSpeed = kinematics.maxSpeed; | |
+ var originalMaxSpeed = maxSpeed; | |
if (maxSpeed * dt > distance) { | |
maxSpeed = distance / dt; | |
} | |
+ print( | |
+ "position: ${own.position} velocity: ${own.velocity} maxSpeed: $originalMaxSpeed adjustedMaxSpeed: $maxSpeed dt: $dt distance: $distance newVelocity: ${offset.scaled(maxSpeed)}"); | |
if (maxSpeed == 0) { | |
kinematics.stop(); | |
} else { | |
class NavigationComponent extends Component with Steerable { | |
@override | |
Vector2 get position => positionParent?.position ?? Vector2.zero(); | |
@override | |
Vector2 velocity = Vector2.zero(); | |
@override | |
double get angle => positionParent?.angle ?? 0; | |
@override | |
double angularVelocity = 0; | |
@override | |
Kinematics kinematics = LightKinematics(10); | |
PositionComponent? get positionParent => parent as PositionComponent; | |
@override | |
set angle(double value) => positionParent!.angle = value; | |
Behavior? behavior; | |
void moveTo(Vector2 target) { | |
behavior = kinematics.seek(target); | |
} | |
@override | |
void onMount() { | |
kinematics.handleAttach(this); | |
super.onMount(); | |
} | |
@override | |
void update(double dt) { | |
if (parent == null || behavior == null) { | |
return; | |
} | |
behavior!.update(dt); | |
// Seek seems to have trouble stablizing? | |
if (velocity.length2 < 10) { | |
behavior = null; | |
} else { | |
positionParent!.position += velocity; | |
} | |
super.update(dt); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment