xxxxxxxxxx
103
// Daniel Shiffman
// http://codingtra.in
// https://youtu.be/CKeyIbT3vXI
class Particle {
constructor(x, y, hu, firework) {
this.pos = createVector(x, y);
this.firework = firework;
this.lifespan = 200;
this.hu = hu;
this.acc = createVector(0, 0);
if (this.firework) {
this.vel = createVector(0, random(-10, -20));
} else {
// this.vel = p5.Vector.random2D();
// this.vel.mult(random(2, 10));
// //normal ring
// this.vel = p5.Vector.random2D().mult(10);;
// //normal firework
// this.vel = p5.Vector.random2D().mult(random(4, 10)).mult(4);
// // dubble ring
// this.vel = p5.Vector.random2D();
// if (random(1) < 0.5) {
// this.vel.mult(random(20, 20));
// } else {
// this.vel.mult(random(10, 10) * 4 + 3);
// }
// // oval shape
// let rotAngle = 0// -PI / 8; // (-PI/2 ~ PI/2)
// let a = random(TWO_PI);
// let m = 5, n = 10;
// let velX = cos(a) * m * n / sqrt(pow(m * sin(a), 2) + pow(n * cos(a), 2));
// let velY = sin(a) * m * n / sqrt(pow(m * sin(a), 2) + pow(n * cos(a), 2));
// velX = (velX * cos(rotAngle) - velY * sin(rotAngle)) * 10;
// velY = (velX * sin(rotAngle) + velY * cos(rotAngle)) * 10;
// this.vel = createVector(velX, velY);//.mult(10);
// // heart shape
// let a = random(TWO_PI);
// // let tempVel = p5.Vector.fromAngle(angle, 1);
// let velX = 16 * pow(sin(a), 3);
// let velY = -1 * (13 * cos(a) - 5 * cos(2 * a) - 2 * cos(3 * a) - cos(4 * a));
// this.vel = createVector(velX, velY);
// four-leave roses
let a = random(TWO_PI);
let leaveN = 4;
let velX = cos(leaveN / 2 * a) * cos(a) * 10;
let velY = cos(leaveN / 2 * a) * sin(a) * 10;
this.vel = createVector(velX, velY);
}
}
applyForce(force) {
let airforce = this.vel.copy().normalize().mult(this.vel.magSq() * (-0.005));
this.acc.add(airforce);
this.acc.add(gravity);
}
update() {
if (!this.firework) {
this.vel.mult(0.9);
this.lifespan -= 4;
}
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
done() {
if (this.lifespan < 0) {
return true;
} else {
return false;
}
}
show() {
colorMode(HSB);
if (!this.firework) {
strokeWeight(2);
stroke(this.hu, 255, 255, this.lifespan);
} else {
strokeWeight(4);
stroke(this.hu, 255, 255);
}
point(this.pos.x, this.pos.y);
}
}