Documentation
    Preparing search index...

    Function wander

    • Wander (§12, §111): steer toward a target that jitters around a circle projected ahead of the agent — the classic formulation, made deterministic.

      angle  += uniform(−jitter · dt, +jitter · dt)      (one draw from `random`)
      centre  = position + normalize(velocity) · distance
      target  = centre + (cos angle, sin angle, 0) · radius
      result  = seek(target)
      

      The angle is a bounded random walk rather than a fresh direction per step, which is the entire point: an independent draw every frame averages out to nothing and looks like vibration, while a walking angle produces smooth, plausible meandering. The circle rides ahead of the agent so the target stays mostly in front of it, and radius / distance sets how sharply it can turn.

      Exactly one draw from random per call, always, so a stream feeding a fixed-step loop is a function of step count alone: two agents seeded alike trace identical paths, and different seeds diverge. tests/steering.test.ts asserts both.

      The circle is defined on the XY plane (§7a: 2D is XY, Y-up), so 2D agents — the ones §111 has in mind — get exactly the classic behaviour and never leave z = 0. A 3D agent still wanders, but only in heading, not in pitch: the circle stays XY-aligned while its centre follows the 3D heading. wanderSpherical is the 3D form: a jittered point on a sphere in a heading-aligned frame. Planar wander stays one SeededRandom draw per call; the spherical form takes two.

      The angle is held in the world frame, not the agent's local frame as Buckland's variant does; the two differ by whether the wander target rotates with the agent, and the world-frame form has no frame construction and no accumulated rotation error.

      jitter is a rate (radians per second, §7a) and is scaled by deltaTime here. The angular random walk's diffusion therefore scales with dt rather than being step-size invariant (invariance would want a √dt scaling); with the §10 fixed step this is constant and reproducible, and the classic formulation is what the parameter names mean elsewhere in the literature. A zero-velocity agent has no heading, so the circle is centred on the agent itself and wander pushes it off in the current angle's direction.

      Parameters

      Returns Vector3