Flock/Shoal Simulation

The birds and the bees (and fish)

There are many animals that exhibit flocking behaviour, including humans. This is an attempt to recreate that behaviour using three simple rules:

  • Separation - avoid crowding neighbors (short range repulsion)
  • Alignment - steer towards average heading of neighbors
  • Cohesion - steer towards average position of neighbors (long range attraction)

Unfortunately, due to the nature of the problem this is an O(n^2) algorithm, as every fish/bird/thing has to check the position and velocity of every other entity (without doing special trickery like bin-lattice subdivision). Here's the link to the source code. If you're familiar with Java and LibGDX this should be pretty simple to run and modify. If not, it might be worth writing this in Javascript, I've seen some pretty successful examples online.

So what are the uses except for making pretty (but intensive) screensavers? Well I guess a similar, but probably more complex, simulation could be used to see how crowds of people react in certain situations. I can't for the life of me find the source, but I remember reading that putting a pillar in the middle of a doorway can speed up entry and exit of a tube station, as it forces commuters to line up. At present however, it just makes for a pretty video.


Easy Parallax Stars

Easy Parallax Stars

Don't these stars look great

I spent a long time messing around with chunks, loading, unloading, pooling, etc. before a friend suggested I came up with a much simpler way of getting parallax stars working easily.

Generate a load of random points in a square, each with a random z-value. Scale each point's speed according to the z-value to simulate the parralax (this way you can have as many layers of parallax as you want and to add more you can just increase the range of the z-value). Whenever a star leaves the screen, just spawn it in on the opposite side of the square. As long as your square is slightly bigger than the screen size then that's it, amazing-looking amazingly simple parallax stars. If you scale the star speed in the opposite way i.e. closer stars go faster then you get a king of underwater-y effect. Could be a nice look for a diving game.