steering_engine

Dart CI License: MIT

Composable 2D steering behaviors in pure Dart, for Flutter games, autonomous characters and simulations. No runtime dependencies, Flutter imports, IO or network access in the core.

Português · Getting started · Learning guide · Wiki · Online lab · Equations · Architecture · Flutter lab · Roadmap · Flocking · Obstacle avoidance · Path following · Contributing

Steering behavior concepts

Learn step by step

A behavior turns a goal into acceleration. The engine integrates it into velocity and position; your Flutter or Flame application draws the result. Start with units, vectors and a complete loop, then compare Seek (approach) with Arrive (approach and slow down).

The bilingual learning guide and GitHub Wiki explain all 19 behaviors and composition strategies with diagrams, pseudocode, parameters, Dart snippets, experiments and limits.

Learn Chapters
Move one agent Seek, Flee, Arrive, Brake
Anticipate and explore Pursuit, Evade, Wander
Combine decisions Weighted blend, Priority
Navigate geometry Path, Obstacles, Walls
Move together Separation, Alignment, Cohesion, Flocking
Organize and scale Leader, Queue, Spatial index

For practical combinations and common problems, see recipes and troubleshooting.

Status and installation

Source version 0.2.0, prepared for the first hosted release. Publication is verified separately from source versioning; see release validation. The core requires Dart >=3.3.0 <4.0.0, with no Flutter or Flame dependency. Before hosted availability is confirmed, use a local path or a reviewed Git ref.

dependencies:
  steering_engine:
    path: ../steering-engine

The Flutter lab and Flame example have their own SDK constraints and are validated independently.

Hosted installation after publication is confirmed

Once version 0.2.0 is confirmed on pub.dev, the hosted dependency is:

dependencies:
  steering_engine: ^0.2.0

Until then, use the local-path setup above. Publisher ownership and hosted availability are tracked independently in the release audit; this example does not confirm an upload. See the 0.2.0 implementation record for design decisions and measured limits.

Make a character arrive

import 'package:steering_engine/steering_engine.dart';

void main() {
  const engine = SteeringEngine();
  var agent = SteeringAgent(
    position: Vec2.zero,
    maxSpeed: 80,
    maxAcceleration: 160,
  );
  final behavior = Arrive(Vec2(200, 100), slowingRadius: 80);

  for (var frame = 0; frame < 600; frame++) {
    agent = engine.step(agent, behavior, 1 / 60);
  }
  print(agent.position); // Near (200, 100), with bounded deceleration.
}

Position uses your distance units; velocity is units/second, acceleration is units/second², and dt is seconds. Render agent.position with Flutter CustomPainter, or copy it into a Flame component in your game loop.

Choose a behavior

Behavior Goal Important detail
Seek Approach a position Can overshoot; does not plan a stop
Flee Move away from a position Exact overlap has no escape direction
Arrive Approach and slow down Brakes inside the stop radius; no teleportation
Pursuit Intercept a moving target Linear prediction, bounded by maxPrediction
Evade Escape a moving threat Flees the predicted position
Wander Explore without a fixed destination Own one seeded, stateful instance per agent
Brake Reduce velocity toward zero Obeys the acceleration limit

Pursuit and Evade capture immutable target snapshots: rebuild them when the target updates. All built-in behaviors return bounded acceleration, not force.

Continuous segment following (0.2.0)

Use PolylinePath and FollowPolyline for predicted-position projection and an arc-distance target. Retain one follower per agent; share geometry. Normal progress is forward-only; call reacquire(agent) after a teleport. Corner preview is a configurable desired-speed policy, with no exact-tracking guarantee. Guide and measured comparison · Online scene.

final geometry = PolylinePath(SteeringPath([
  Vec2(0, 0), Vec2(100, 0), Vec2(100, 100),
]));
final follower = FollowPolyline(geometry, lookAheadDistance: 30);

Follow a route

final route = SteeringPath([
  Vec2(80, 0), Vec2(80, 80), Vec2(160, 80),
]);
final follower = FollowPath(route, waypointRadius: 5, slowingRadius: 35);
// Keep the follower instance across frames: it owns the current waypoint.
agent = engine.step(agent, follower, 1 / 60);

Use loop: true on SteeringPath for a repeating route. Inspect follower.waypointIndex and currentTarget, or call reset() to rewind. Run dart run example/path_following.dart. See the route guide.

Steer around static obstacles

final avoidance = ObstacleAvoidance([
  CircleObstacle(Vec2(80, 0), radius: 15),
], agentRadius: 5, safetyMargin: 4, lookAheadTime: 1.5);
final steering = PrioritySteering([
  avoidance,
  Arrive(Vec2(180, 0), slowingRadius: 40),
]);

The scan accounts for agent size and selects the first predicted circle surface. It requests lateral acceleration and braking, or zero when the local path is clear. Compose it before a destination or route follower. findThreat exposes read-only debug geometry. This reactive method does not resolve or guarantee prevention of collisions. See the visual guide and limits. Run dart run example/obstacle_avoidance.dart for a complete checked simulation.

Combine behaviors

final steering = WeightedBlend([
  WeightedBehavior(Arrive(Vec2(300, 100)), weight: 0.8),
  WeightedBehavior(Wander(seed: 42), weight: 0.2),
]);

Weights multiply accelerations without automatic normalization. The sum is capped to the agent's acceleration limit. Zero weight skips evaluation entirely. For ordered alternatives, PrioritySteering([primary, fallback], epsilon: 0.001) chooses the first output whose magnitude exceeds the threshold. Later behaviors are not evaluated. It is selection, not an accumulated force budget.

Implement SteeringBehavior.calculate(agent, dt) to add a custom algorithm. The engine caps custom outputs before integrating motion.

Flock with local neighbors

Build one AgentNeighborhood(agents) per frame and evaluate all agents against that shared observation. Separation, Alignment and Cohesion can be used independently; Flocking composes them through WeightedBlend.

final observed = AgentNeighborhood(agents);
final flocking = Flocking(observed,
  separationRadius: 25, neighborRadius: 80,
  separationWeight: 1.5, alignmentWeight: 1, cohesionWeight: 0.7,
);
agents = [
  for (final agent in agents)
    const SteeringEngine().step(agent, flocking, 1 / 60),
];

Replace the complete frame after evaluation. Neighbor queries exclude the observer by identity; use the original snapshot, not a copy. The current query is linear in this constructor, so full-flock work is O(n²); use AgentNeighborhood.spatial(agents) for optional grid lookup. See the equations and visual guide and run dart run example/flocking.dart. The lab compares each rule and lets you tune weights for 24, 100, 500 or 1000 agents. Flocking does not provide collision resolution.

Explore visually

Open the online laboratory or the Flame demo. Wiki chapters link directly to the relevant scene, for example wall following. To run locally:

cd example/flutter_lab
flutter pub get
flutter run -d chrome

The lab offers individual and collective behaviors, path following, obstacle avoidance and compositions, click/drag targets, a moving target, a seeded wander stream, velocity and acceleration arrows, trails, adjustable limits, pause, reset and fixed-step advancement. World edges wrap in the demo; the core leaves boundaries to callers.

Numerical model and limits

  • Immutable vectors and agent snapshots; copied, unmodifiable composition lists.
  • Validated finite parameters, nonnegative limits and dt; positive response time.
  • Semi-implicit Euler: update velocity, cap speed, then update position.
  • dt == 0 is an engine no-op and does not evaluate stateful behaviors.
  • Small fixed steps are recommended. Large steps can overshoot, and blending conflicting goals may cancel motion or prevent arrival.
  • Wander seeds reproduce matching call sequences on the same runtime; trajectories are not promised identical across different time steps or runtimes.
  • Collision resolution, vehicle dynamics and moving-obstacle prediction remain future work. Obstacle avoidance supports static circles. Wall following provides local segment guidance, without contact resolution or wall-avoidance guarantees. Steering can consume a planned route; it does not replace pathfinding.

See equations and conventions for exact formulas.

Spatial index and following modes

Use AgentNeighborhood.spatial(agents) for optional grid lookup with the same neighbor order and exact filtering. Compare populations and measured tick cost in the lab. Read spatial lookup, benchmarks, leader / queue / wall following and Flame integration. All four following families, including the existing waypoint follower, are covered.

Development

dart pub get
dart analyze --fatal-infos
dart test
dart run example/steering_engine_example.dart
dart run example/composition.dart

CI also checks JavaScript compilation, the Flutter lab and a publication dry run. See CONTRIBUTING.md for the complete checks.

Scientific references

  1. Reynolds, C. W. (1999). Steering Behaviors for Autonomous Characters. Game Developers Conference. Conceptual basis for local movement and behavior composition.
  2. Reynolds, C. W. (1987). Flocks, Herds, and Schools: A Distributed Behavioral Model. SIGGRAPH. Conceptual foundation for the collective behaviors.

This implementation uses an explicit acceleration/response-time convention and bounded prediction, documented above; it is not a verbatim port of Reynolds's demos.

Libraries

steering_engine
Composable, acceleration-based 2D steering with no runtime dependencies.