xxxxxxxxxx
class Particle {
constructor(x,y){
this.locked = false;
this.acceleration = createVector(0,0.0);
this.velocity = createVector(0,0);
this.position = createVector(x,y);
this.prevPosition = createVector(x,y);
this.mass = 2;
this.influencer = {on: false, x: x, y: y};
}
applyForce(force){
let f = force.copy();
f.div(this.mass);
this.acceleration.add(f);
}
//update position
update(){
if(!this.locked){
// behviour A - regular particle on a spring
this.velocity.mult(0.99);
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}else if(this.influencer.on){
// behaviour B - controlled from outside
this.position.set( this.influencer.x, this.influencer.y);
//this.prevPosition.set( this.influencer.x, this.influencer.y);
this.diff = createVector( this.position.x - this.prevPosition.x , this.position.y - this.prevPosition.y);
// keep last difference before letting go
if(this.diff.mag() > 0 && this.diff.mag() < 10){
this.velocity.set(this.diff.copy());
}
}else{
// behaviour C - locked
// keep moving
this.checkWalls();
this.velocity.mult(0.98);
this.position.add(this.velocity);
// store previous posittion
this.prevPosition = this.position.copy();
// let noisX = noise(this.position.x*0.1, frameCount * 0.01 + 24234) - 0.5;
// let noisY = noise(this.position.y*0.1, frameCount * 0.01 - 24234) - 0.5;
// this.position.add(createVector(noisX*4 , noisY*4));
}
// store previous position
this.prevPosition = this.position.copy();
}
checkWalls(){
if(this.position.x > width || this.position.x < 0){
this.velocity.x *= -1;
}
if(this.position.y > height || this.position.y < 0){
this.velocity.y *= -1;
}
}
show(){
if(this.locked){
noFill();
stroke(0);
strokeWeight(2);
ellipse(this.position.x, this.position.y, 12, 12);
}else{
noStroke();
fill(255);
ellipse(this.position.x, this.position.y, 4, 4);
stroke(2);
}
}
}