Simulating Basic Swarm Behavior in Elm
Swarm behavior is a fascinating field where simple rules can lead to complex and often mesmerizing patterns. In a recent live coding session, I explored this by implementing a rudimentary swarm simulation in Elm. The finalized code provides a look at the principles involved.
To simulate this behaviour, we need to follow three simple rules:
- If my closest neighbour is too far away, move toward him.
- If my closest neighbour is too near, move away from him.
- Otherwise, move in the same direction as my closest neighbour.
By adhering to these rules, each agent in the swarm interacts with its local environment, eventually leading to complex global behavior.
The complete Elm implementation can be found on Github.