xxxxxxxxxx
93
class Column {
constructor(x, speed, numParticles)
{
this.x = x;
this.speed = speed;
this.particles = [];
// This loop initializes the array of particles within each column
for (let j = 0; j < numParticles; j++)
{
// y coordinate of particle is randomly generated based on height of canvas. x coordinate of particle is based on the column's x coordinate this.x
let y = random(height);
this.particles.push(new Particle(this.x, y));
}
}
//... This function updates the state of each particle within the column ...
update() {
// Iterates through each particle in the array that makes up the column
for (let particle of this.particles) {
// Determines the speed and direction of which the paritcle should be updated (based on the globa;l variables)
particle.update(this.speed * moveDirection * scrollSpeed);
particle.edges();
particle.applyGlitch();
}
}
// // ... This function measures distance between particles within adjacent columns to apply force of repulsion and change colors ...
// // Takes in particles other than this.particle, the particle its comparing
// interactWith(otherParticles)
// {
// //Nested for loop iterates through all the particles in this column (all the particles in that specific array that makes up the column)
// for (let p1 of this.particles)
// {
// // Then iterates through the array of the adjacent column
// for (let p2 of otherParticles)
// {
// // Calculating the distance between all the particles based on their positions
// let d = dist(p1.position.x, p1.position.y, p2.position.x, p2.position.y);
// // If distance is less than the constant distance threshold declared then apply force and change color
// if (d < PARTICLES_DISTANCE)
// {
// // Force calculated based on position of both particles
// let force = p5.Vector.sub(p1.position, p2.position);
// // Set the magnitude of the repulsion force initially declared to variable force while keeping the force's calculated direction
// force.setMag(DISPERSION_FORCE);
// //Applying the force on particles
// p1.applyForce(force);
// p2.applyForce(force.mult(-1));
// //Changing color of particles
// p1.color = color(random(255), random(255), random(255));
// p2.color = color(random(255), random(255), random(255));
// }
// }
// }
// }
// }
interactWith(otherParticles) {
for (let p1 of this.particles) {
for (let p2 of otherParticles) {
let d = dist(p1.position.x, p1.position.y, p2.position.x, p2.position.y);
if (d < PARTICLES_DISTANCE) {
let force = p5.Vector.sub(p1.position, p2.position);
force.setMag(DISPERSION_FORCE);
// Use Perlin noise to control the spiraling effect
let noiseValX = noise(p1.position.x * 0.01, p1.position.y * 0.01, frameCount * 0.01);
let noiseValY = noise(p2.position.x * 0.01, p2.position.y * 0.01, frameCount * 0.01);
// Apply forces to create a spiraling motion
let spiralForce = createVector(noiseValX - 0.5, noiseValY - 0.5); // Centering the noise values
spiralForce.mult(DISPERSION_FORCE); // Adjust the magnitude
p1.applyForce(spiralForce);
p2.applyForce(spiralForce.mult(-1));
// Changing color of particles
p1.color = color(random(255), random(255), random(255));
p2.color = color(random(255), random(255), random(255));
}
}
}
}
}