Flocking and Vector-Fields

21. December 2024

A lot of different forces are interacting to produce this diverse swarming behavior

This is a simple program illustrating flocking mechanisms and the effect of a vector field that changes over time. In the past I have done a lot of experimenting with particle systems and vector fields and how particles interact with one another with different forces. This is largely due to Daniel Shiffmans excellent The Coding Train (formerly The Coding Rainbow) Youtube-Channel in which he teaches programming in a very applied and diverse way by solving challenges. Through this series I initially learned programming and can recommend it to everyone, even now, as a continuous source of interesting areas of programming.

In this flocking simulation 4 forces are at work

  1. The vector field (indicated in grey) applies a force on each particle based on their absolute positions. This force changes continuously in time and space.
  2. The particles attract each other based on the inverse of their distance.
  3. The particles repel each other based on the inverse of their distance squared.
  4. The particles align their direction of movement relative to their neighbours.

With each of these fundamental forces comes at least one constant, used to scale their effect. This makes getting realistic flocking behavior, as seen in flocks of birds for example, pretty difficult. As a rule of thumb I found that the attraction constant should be larger than the repelling constant. It is very important that the repelling force falls of quicker than the attracting force since we want particles that are far apart to move towards each other to form groups. Without a repelling force all particles would just bundle up into a single point and it would be uninteresting. This is almost exactly the same as the balance of electrical and gravitational forces in particles.

No flocking-behaviour, just forces from the vector field

An image of the particles being moved by the vector field without any flocking forces being active. This can create stunning imagery if done right and I hope I get to showcase this project soon.

References