xxxxxxxxxx
92
let particles1 = [];
let particles2 = [];
const numParticles = 500; // Number of particles per cluster
let attractor1, attractor2;
function setup() {
createCanvas(800, 600);
strokeWeight(2);
// Initialize clusters of particles for each person
for (let i = 0; i < numParticles; i++) {
particles1.push(
new Particle(random(200, 300), random(height / 2 - 20, height / 2 + 20))
);
particles2.push(
new Particle(random(500, 600), random(height / 2 - 20, height / 2 + 20))
);
}
// Invisible attractors placed in the middle horizontally, just like the previous code
attractor1 = createVector(400, 200); // Attraction for person 1
attractor2 = createVector(400, 400); // Attraction for person 2
}
function draw() {
background(0,15);
noStroke();
fill(255);
for (let i = 0; i < 1; i++) {
let xrandom = random(width);
let yrandom = random(height);
ellipse(xrandom, yrandom, 3, 3);
}
// Draw connection between the two clusters (just like the line between the two balls in the original code)
stroke(255, 150);
line(
particles1[0].pos.x,
particles1[0].pos.y,
particles2[0].pos.x,
particles2[0].pos.y
);
// Update and display each particle in both clusters
updateCluster(particles1, attractor1, "red");
updateCluster(particles2, attractor2, "white");
}
function updateCluster(particles, attractor, color) {
stroke(color);
for (let i = 0; i < numParticles; i++) {
let p = particles[i];
p.attracted(attractor); // Attract particles toward their invisible attractor
p.update(); // Update particle position
p.display(); // Display the particle
}
}
// Particle class (similar to the Mover class but for multiple particles)
class Particle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = 1; // Added mass to simulate the same behavior as the "Mover"
}
// Function to apply attraction toward an invisible attractor
attracted(target) {
let force = p5.Vector.sub(target, this.pos);
let distance = constrain(force.mag(), 5, 25); // Limit the distance just like in the original Mover code
let strength = (1 * this.mass) / (distance * distance);
force.setMag(strength);
this.acc.add(force);
}
// Update position and velocity
update() {
this.vel.add(this.acc);
this.vel.limit(2); // Limit the speed just like in the original Mover code
this.pos.add(this.vel);
this.acc.mult(0); // Reset acceleration after each frame
}
// Display the particle as a point
display() {
point(this.pos.x, this.pos.y);
}
}