Particle constructor
The constructor runs once when 'new Particle(x, y)' is called in setup(). It's where each particle gets its starting position, random velocity, and mass before the draw loop takes over.
constructor(x, y) {
this.pos = createVector(x, y); // Position vector
this.vel = p5.Vector.random2D(); // Random initial velocity vector (magnitude 1)
this.vel.mult(random(1, 3)); // Scale initial velocity for faster movement
this.mass = 1; // Mass of the particle (used for gravity calculation)
}
Line-by-line explanation (4 lines)
this.pos = createVector(x, y);- Creates a p5.Vector to store the particle's x/y position on the canvas.
this.vel = p5.Vector.random2D();- Generates a random unit-length vector pointing in a random direction, giving each particle a different starting heading.
this.vel.mult(random(1, 3));- Scales that direction vector by a random amount between 1 and 3, so particles start at different speeds.
this.mass = 1;- Stores the particle's mass, used later in the gravitational force formula (F = G*m1*m2/d^2).