Steering, not scripting
A butterfly in this simulation is not following a pre-drawn path — it is governed by steering behaviours, a technique introduced by Craig Reynolds in his 1999 paper "Steering Behaviors For Autonomous Characters" (the same researcher behind the 1987 Boids flocking model). Instead of computing a full trajectory in advance, each agent asks a much simpler question every single frame: given where I am and where I want to go, what small steering force should I apply right now?
The core building block is seek: compute the vector from the agent's current position to its target, treat that as the desired velocity (scaled to the agent's maximum speed), and subtract the agent's current velocity to get a steering force. Apply that force, clamped to a maximum turning strength, and the agent curves smoothly toward the target instead of snapping onto a straight line.
desired = normalize(target - position) * maxSpeed steer = desired - velocity steer = clamp(steer, maxForce) velocity += steer position += velocity
Wander: looking purposeful while going nowhere in particular
Real butterflies do not fly in straight lines between flowers — they flutter, loop and drift. The simulation reproduces this with a wander behaviour: a small target point is projected a short distance ahead of the butterfly, then nudged around the circumference of an invisible circle by a slowly-changing random angle each frame. Steering toward that wandering point produces the same gently curving, unpredictable-but-continuous paths real butterflies trace, without ever needing to look genuinely random frame to frame — the wander target only changes a little each step, which is what keeps the motion smooth instead of jittery.
Attraction to flowers and the flocking connection
When a flower is within range, the wander force is blended with (or replaced by) a seek force toward the nearest flower, weighted by distance so a butterfly can be "interested" in a bloom without snapping toward it instantly. Click to plant a new flower and every nearby butterfly's target list updates on the next frame — there is no global planner, just each individual agent re-evaluating its local neighbourhood, which is exactly the same decentralised principle behind Reynolds' original boids: complex, organic-looking group behaviour emerging from agents that only ever look at themselves and their immediate surroundings, never the whole flock.
This is why steering behaviours scale so well — doubling the number of butterflies does not require rethinking the algorithm, because every agent's decision costs roughly the same small, constant amount of work regardless of how many other agents exist.
Frequently asked questions
Do the butterflies plan a route to the flowers in advance?
No. Each butterfly recomputes a single steering force every frame based only on its current position, velocity and the nearest flower or wander target — there is no path planning or lookahead, which is why the paths look organic and adapt instantly when you plant a new flower.
Why don't the butterflies fly in straight lines?
A wander behaviour continuously nudges an invisible target point around a small circle ahead of the butterfly, and the butterfly steers toward that drifting point. Because the target only moves a little each frame, the resulting flight path curves smoothly instead of jumping around.
Is this the same technique used for flocking birds or fish in games?
Yes — it is the same family of steering behaviours (seek, wander, separation, alignment) introduced by Craig Reynolds' boids model, just applied to a single fluttering agent attracted to flowers instead of a synchronised flock.
Try it live
Everything above runs in your browser — open Butterflies & Flowers and change the parameters while it is running. Nothing is installed, nothing is uploaded, the whole model lives in one tab.
▶ Open Butterflies & Flowers simulation