miaoda-game-steering2d-core
v0.1.1
Published
Deterministic engine-independent 2D steering: seek, wander, host-queried obstacle avoidance, exact pursuit/interception, flocking, and bounded composition.
Maintainers
Readme
miaoda-game-steering2d-core
Deterministic, engine-independent steering for autonomous actors in continuous 2D space. It provides seek, flee, arrive, exact constant-velocity pursuit/interception, evade, stateful wander, host-queried obstacle avoidance, separation, alignment, cohesion, and bounded steering composition.
Use it for top-down action and shooting enemies, flying tower-defense units, RTS squads, traffic, flocking, homing enemies, and moving bosses. It is not grid pathfinding: navigation chooses a route, while steering chooses the desired velocity and acceleration for the next simulation step.
pnpm add miaoda-game-steering2d-coreconst chase = pursuit(enemy, { position: player.pos, velocity: player.velocity }, {
maxPrediction: 2,
});
const flockAcceleration = combineWeighted([
{ acceleration: separation(enemy, neighbors, { radius: 36 }).acceleration, weight: 1.5 },
{ acceleration: alignment(enemy, neighbors, { radius: 96 }).acceleration, weight: 0.7 },
{ acceleration: cohesion(enemy, neighbors, { radius: 96 }).acceleration, weight: 0.5 },
], enemy.maxAcceleration);
// The host owns and snapshots both rng and wanderState.
const roaming = wander(enemy, wanderState, () => rng.next(), {
circleDistance: 24,
circleRadius: 12,
maxAngleChange: Math.PI / 8,
});
wanderState = roaming.state;
// `castActor` belongs in the Phaser/Cocos/kinematic adapter. It returns data only.
const avoiding = avoidObstacles(enemy, roaming.desiredVelocity, castActor, {
lookAhead: 48,
radius: 8,
sidePreference: 1,
});The package only calculates desiredVelocity and bounded acceleration. Your fixed-step loop integrates velocity;
miaoda-game-kinematic2d-core or engine physics owns position and collision response. A behavior tree or FSM decides
when to chase, flee, or flock. Neighbor arrays can come from miaoda-game-targeting2d-core without a package-level
dependency.
wander never calls Math.random: it consumes exactly one value from the supplied random source and returns the
angle state needed for deterministic continuation. Persist that angle beside the caller-owned RNG snapshot.
avoidObstacles emits a read-only circle/capsule-cast request through ObstacleQuery, validates the nearest hit,
and calculates a deflected desired velocity. The adapter may implement that query with Phaser ray/shape tests,
Cocos physics queries, kinematic2d-core, a tile map, or a custom navigation world. The function never writes a
body velocity, integrates position, or resolves penetration, so engine physics remains the sole movement and
collision authority.
solveInterceptTime solves the constant-velocity interception quadratic and returns the earliest non-negative time.
It returns null when interception is impossible or beyond maxPrediction; pursuit and evade then use the
target's current position. Flocking excludes self by stable id, and coincident pairs receive deterministic opposite
separation directions.
Vector helpers require finite inputs. normalize preserves direction for subnormal and extreme finite vectors and
returns {x:0,y:0} only for the zero vector. limitMagnitude uses the same scale-stable direction. Arithmetic that
would turn finite operands into an unrepresentable Infinity/NaN result throws RangeError.
