xxxxxxxxxx
45
class Particle {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
this.r = 4;
this.lifetime = 255;
}
applyForce(force) {
this.acceleration.add(force);
}
edges() {
if (this.position.y >= height - this.r) {
this.position.y = height - this.r;
this.velocity.y *= -1;
}
if (this.position.x >= width - this.r) {
this.position.x = width - this.r;
this.velocity.x *= -1;
}
if (this.position.x <= this.r) {
this.position.x = this.r;
this.velocity.x *= -1;
}
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.set(0, 0); //resetting acceleration to zero at every fraame
this.lifetime -=1;
}
display() {
stroke(255, this.lifetime);
strokeWeight(2);
fill(200, this.lifetime);
ellipse(this.position.x, this.position.y, this.r * 2);
}
}