Le concept
L'algorithme de flocking (Reynolds, 1987) simule des comportements collectifs avec 3 règles : séparation (éviter les voisins proches), alignement (s'orienter comme eux), cohésion (se rapprocher du centre du groupe). Chaque agent n'a qu'une vision locale, mais le comportement global est réaliste.
Le code
// Boids: 50 agents with separation/alignment/cohesion
let boids = [];
function setup() {
createCanvas(400, 280);
for (let i = 0; i < 50; i++) boids.push({x:random(width),y:random(height),vx:random(-1,1),vy:random(-1,1)});
}
function draw() {
background(11,15,20);
for (let b of boids) {
// Simplified: steer toward center + avoid close
let cx=0,cy=0,n=0;
for (let o of boids) { if(o!==b&&dist(b.x,b.y,o.x,o.y)<50){cx+=o.x;cy+=o.y;n++;}}
if(n){b.vx+=(cx/n-b.x)*0.003;b.vy+=(cy/n-b.y)*0.003;}
b.x+=b.vx;b.y+=b.vy; b.x=(b.x+width)%width;b.y=(b.y+height)%height;
fill(55,227,195);noStroke();ellipse(b.x,b.y,5,5);
}
}Pour aller plus loin
Explorez les autres cours gratuits sur la page Apprendre, ou réservez un appel pour discuter de votre projet.
Envie d'aller plus loin ?
Réserver un appel découverte →