xxxxxxxxxx
/*
Particle Life Simulation
Dased upon Hunar Ahmad' Particle Life Simulation
https://github.com/hunar4321/particle-life
https://www.youtube.com/watch?v=0Kx4Y9TVMGg&ab_channel=Brainxyz
This version by Juan Carlos Ponce Campuzano
https://dynamicmath.xyz
9/May/2024
*/
let wht, rd, bl;
// Change the poputation to see what happens
let populationW = 100;
let populationR = 100;
let populationB = 300;
function setup() {
createCanvas(800,500);
wht = create(populationW, "white");
rd = create(populationR, "red");
bl = create(populationB, "blue");
frameRate(40)
}
function draw() {
background(0, 90);
rule(bl, bl, -0.32);
rule(bl, rd, -0.17);
rule(bl, wht, 0.34);
rule(rd, rd, -0.1);
rule(rd, bl, -0.34);
rule(wht, wht, 0.15);
rule(wht, bl, -0.2);
for (i = 0; i < atoms.length; i++) {
drawCreature(atoms[i].x, atoms[i].y, atoms[i].color, 10);
}
}
let drawCreature = (x, y, c, s) => {
noStroke();
fill(c);
circle(x, y, s);
};
let atoms = [];
let atom = (x, y, c) => {
return { x: x, y: y, vx: 0, vy: 0, color: c };
};
let create = (number, color) => {
group = [];
for (let i = 0; i < number; i++) {
group.push(atom(random() * width, random() * height, color));
atoms.push(group[i]);
}
return group;
};
let rule = (atoms1, atoms2, factor) => {
for (let i = 0; i < atoms1.length; i++) {
fx = 0;
fy = 0;
for (let j = 0; j < atoms2.length; j++) {
a = atoms1[i];
b = atoms2[j];
dx = a.x - b.x;
dy = a.y - b.y;
d = Math.sqrt(dx * dx + dy * dy);
if (d > 0 && d < 85) {
F = (factor * 1) / d;
fx += F * dx;
fy += F * dy;
}
}
a.vx = (a.vx + fx) * 0.45;
a.vy = (a.vy + fy) * 0.45;
a.x += a.vx;
a.y += a.vy;
if (a.x <= 0 || a.x >= width) {
a.vx *= -1;
}
if (a.y <= 0 || a.y >= height) {
a.vy *= -1;
}
}
};
//function windowResized() {
// resizeCanvas(windowWidth, windowHeight);
//}