xxxxxxxxxx
51
let maxVel = 0,
alpha = 360,
G = 6.67408, // Universal gravitational constant
particles = [],
trails = false;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
colorMode(HSB, 360);
background(5);
}
function draw() {
if (!trails) background(5);
let bestVel = 0;
for (let i = 0; i < particles.length; i++) {
for (let j = 0; j < particles.length; j++) {
if (particles[i] !== particles[j]) {
particles[i].attracted(particles[j]);
}
}
particles[i].update();
particles[i].show();
const tV = abs(particles[i].vel.x + particles[i].vel.y);
if (tV > bestVel) {
bestVel = tV;
}
}
maxVel = bestVel;
noStroke();
fill(255);
textSize(16);
text("click to add a particle", 0, height - 65);
text("particles are attracted to each other", 0, height - 45);
text("t to toggle trails", 0, height - 25);
text("r to reset", 0, height - 5);
}
function keyPressed() {
if (key === "t") {
trails = !trails;
} else if (key === "r") {
particles = [];
}
}
function mousePressed(e) {
particles.push(new Particle(mouseX, mouseY, true));
}