xxxxxxxxxx
162
class Particle {
constructor(position, r, myNote, col) {
this.position = position;
this.velocity = createVector();
this.acceleration = createVector();
this.maxSpeed = 40;
//radius of particle
this.r = r;
this.whiteFlash = 0;
//to track the original radius when growing/shrinking
this.origR = r;
//color of particle
this.ogCol = col;
this.col = col;
//myNote is a MIDI value taken from scaleNotes + root note
//we compare this value to note from input
this.myNote = myNote;
this.triggerWallNote = false;
this.ang = 0;
this.grow = false;
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
this.velocity.limit(this.maxSpeed, this.maxSpeed/4);
}
applyForce(force) {
this.acceleration.add(force);
}
//if note matches the particle's note, add a randomized acceleration
noteAction() {
// let rand = createVector(random(4, 5) * random(choose), - height / 40);
let rand = createVector(random(1, 3) * random(choose), -height/10);
if (this.myNote == note && prevNote != note) {
this.whiteFlash = 50;
// this.origR = this.r;
// let rand = createVector(random(0.2, -0.2), random(-0.01, -1.5));
this.col = 255;
// delay(10);
this.acceleration.add(rand);
for (let i = 0; i < 10; i++) {
if (this.r < this.r * 2) {
this.r += 5;
}
}
prevNote = note;
}
}
display() {
this.whiteFlash--;
push();
if (this.whiteFlash < 10) {
fill(this.ogCol);
}
if (this.whiteFlash < 0) this.whiteFlash = 0;
noStroke();
ellipse(this.position.x, this.position.y, this.r);
pop();
//write the midi note on the particle
// fill(255);
// textSize(10);
// text(this.myNote, this.position.x, this.position.y + 5);
//if the radius is greater than 2x, constrain it
if (this.r > this.origR*2) {
this.r = this.origR*2;
}
//if the particle is greater than the original radius, shrink it
if (this.r > this.origR) {
this.r -= 1;
//stop shrinking the radius when it reaches the original value
if (this.r < this.origR) {
this.r = this.origR;
this.whiteFlash = 0;
}
}
}
checkEdges(reflectNote) {
//establish a floor and ceiling for movement
if (this.position.y >= (height - this.origR * 0.5)) {
this.position.y = (height - this.origR * 0.5);
this.velocity.y *= -0.9;
this.acceleration.mult(0);
} else if (this.position.y <= (this.origR * 0.75)) {
this.triggerWallNote = true;
this.velocity.y = this.velocity.y * -1;
// this.acceleration.mult(0);
}
//establish walls
// if (this.position.x > (width - this.r * 0.75)) {
if (this.position.x + (this.origR / 1.8) > width) {
this.triggerWallNote = true;
this.velocity.x = this.velocity.x * -1;
this.acceleration.mult(0);
// midiOutput.playNote(this.myNote, 1);
}
if (this.position.x - (this.origR / 1.8) < 0) {
this.triggerWallNote = true;
// midiOutput.playNote(this.myNote - 12, 1);
this.velocity.x = this.velocity.x * -1;
this.acceleration.mult(0);
}
if (this.triggerWallNote) {
// midiOutput.playNote(this.myNote, 1);
if (millis() % 20 < 1) {
this.triggerWallNote = false;
}
}
// if (this.position.x > (hailuX - this.r/2) && this.position.y > (hailuY - this.r/2) && this.position.x < (hailuX + hailu.width + this.r/2) && this.position.y < (hailuY + hailu.height + this.r/2)) {
// this.velocity.mult(-1);
// this.acceleration.mult(0);
// }
}
growMe() {
if (this.grow) {
this.ang++;
let inc = sin(this.ang);
this.r += inc;
if (this.r <= this.origR) {
this.grow = false;
this.r = this.origR;
}
}
}
calculateAttraction(m) {
// Calculate direction of force
let force = p5.Vector.sub(this.position, m.position);
// Distance between objects
let distance = force.mag();
// Limiting the distance to eliminate "extreme" results for very close or very far objects
distance = constrain(distance, 5.0, 25.0);
// Normalize vector (distance doesn't matter here, we just want this vector for direction
force.normalize();
// Calculate gravitional force magnitude
let strength = (G * this.mass * m.mass) / (distance * distance);
// Get force vector --> magnitude * direction
force.mult(strength);
return force;
}
}