class Particle {
constructor(color) {
this.acceleration = p5Instance.createVector(0, 0);
this.velocity = P5.Vector.random2D();
this.position = p5Instance.createVector(
Math.random() * width,
Math.random() * height
);
this.color = color;
}
getForce(flowfield) {
return flowfield[roundAbs(this.position.x / size)][
roundAbs(this.position.y / size)
];
}
applyForce(flowfield) {
let forceVector = this.getForce(flowfield);
this.acceleration.add(forceVector);
}
receiveForceAndUpdatePosition() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.velocity.limit(max_speed);
this.acceleration.setMag(0);
}
show(context) {
context.beginPath();
context.globalAlpha = 0.1;
context.arc(this.position.x, this.position.y, 1.2, 0, 2 * Math.PI);
context.fillStyle = this.color;
context.fill();
}
goBackToField() {
if (this.position.x > width) {
this.position.x = 0;
} else if (this.position.x < -size) {
this.position.x = width - 1;
}
if (this.position.y > height) {
} else if (this.position.y < -size) {
this.position.y = height - 1;
}
}
}