Dancing with friends and enemies: boids' swarm intelligence

… and this is the code for generating animated GIFs using Spheres instead of Points:

n = 1000;
r := RandomInteger[{1, n}];
f := (#/(.01 + Sqrt[#.#])) & /@ (x[[#]] - x) &;
s := With[{r1 = r}, p[[r1]] = r; q[[r1]] = r];
x = RandomReal[{-1, 1}, {n, 3}];
{p, q} = RandomInteger[{1, n}, {2, n}];
Export["Dancing_Spheres.gif",
 Table[
Graphics3D[
Rotate[{Yellow, Dynamic[If[r < 100, s]; Sphere[x = 0.995 x + 0.02 f[p] - 0.01 f[q], 0.04]]}, th, {0, 0, 1}],
 ImageSize -> {700, 500}, Background -> Black, Boxed -> False, PlotRange -> 2], 
{th, 0, 2 Pi, 0.03}]]

Dancing_Spheres.gif

For two-dimensional animations, one can use the method explained in my discussion Visualizing Animated GIFs Along The Time Dimension With Image3D to keep track of the swarm’s complex dynamics.

DancersC.gif ?

? Time

7 Likes