Particle constructor()
The constructor runs once when a Particle object is created with `new Particle(...)`. It sets up all the starting values - position, velocity, acceleration, lifespan and size - that the update() and display() methods will later read and modify.
🔬 This aims every particle at the mouse. What happens if you remove the normalize() call entirely - would particles far from the mouse suddenly launch much faster than particles near it?
this.vel = createVector(targetX - x, targetY - y);
this.vel.normalize(); // Normalize it to get a unit vector (direction only)
constructor(x, y, gravityForce, targetX, targetY) {
// Initial position of the particle
this.pos = createVector(x, y);
// Initial velocity: shoot towards the mouse position from the fountain source
// Create a vector from the fountain source to the mouse
this.vel = createVector(targetX - x, targetY - y);
this.vel.normalize(); // Normalize it to get a unit vector (direction only)
// Scale the velocity to control the initial speed of the particles
let speed = random(8, 18); // Random speed for variation
this.vel.mult(speed);
// Acceleration (gravity) affecting the particle
this.acc = gravityForce.copy();
// Lifespan of the particle, used for alpha (transparency)
this.lifespan = 255;
// Random size for particle variation
this.size = random(4, 10);
// Store initial speed to help map colors more consistently
this.initialSpeed = this.vel.mag();
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
this.vel = createVector(targetX - x, targetY - y);
Builds a vector pointing from the particle's start position toward the mouse so every particle launches in that direction
let speed = random(8, 18); // Random speed for variation
Picks a random speed so particles don't all move at identical velocity, adding natural variation
this.pos = createVector(x, y);- Stores the particle's starting position as a p5.Vector - this is where the fountain launches from
this.vel = createVector(targetX - x, targetY - y);- Creates a vector pointing from the fountain source toward the mouse position by subtracting the coordinates
this.vel.normalize();- Shrinks the vector down to length 1 while keeping its direction, so it's a pure 'compass heading' with no speed baked in yet
let speed = random(8, 18); // Random speed for variation- Picks a random number between 8 and 18 to use as this particle's launch speed
this.vel.mult(speed);- Multiplies the direction vector by the random speed, turning it into a real velocity with both direction and magnitude
this.acc = gravityForce.copy();- Copies the global gravity vector so each particle has its own independent acceleration object to modify safely
this.lifespan = 255;- Starts the particle at full opacity (255), which will count down toward 0 over time
this.size = random(4, 10);- Gives each particle a random diameter between 4 and 10 pixels for visual variety
this.initialSpeed = this.vel.mag();- Records the particle's starting speed (magnitude) so display() can later compare current speed against it to pick a color