xxxxxxxxxx
114
let attractors = []; //initialise the array of objects and other variables
let movers = [];
let mouseAttractor;
let startTime;
function setup() {
createCanvas(500, 500);
background(255);
// initialise list of attractors and have them randomly generated
for (let i = 0; i < 10; i++) {
attractors.push(createVector(random(width), random(height)));
}
// Create mouse tatractor
mouseAttractor = createVector(width / 2, height / 2);
// have list of movers created
for (let i = 0; i < 100; i++) {
movers.push(new Mover(random(width), random(height)));
}
// start contodwn for the 8 seconds
startTime = millis();
}
function draw() {
// time calculation
let elapsedTime = millis() - startTime;
// check 15 secs
if (elapsedTime < 8000) {
// add turbulence/ repulsion forces
for (let mover of movers) {
let turbulence = createVector(random(-0.5, 0.5), random(-0.5, 0.5));
let repulsion = createVector(0, 0); //have this changed to see what kind of direction the art should go
// repulsion force vector calculation
for (let attractor of attractors) {
let d = p5.Vector.dist(mover.position, attractor);
if (d < 50) {
let force = p5.Vector.sub(mover.position, attractor);
force.normalize(); //calcultion
force.div(d);
repulsion.add(force);
}
}
// calc repulsion force from mouse
let dMouse = p5.Vector.dist(mover.position, mouseAttractor);
if (dMouse < 100) {
let force = p5.Vector.sub(mover.position, mouseAttractor);
force.normalize();
force.div(dMouse);
force.mult(120); //repel force multiplier
repulsion.add(force);
}
//basic method handling
mover.applyForce(turbulence);
mover.applyForce(repulsion);
mover.update();
mover.display();
}
}
}
// update mouse attr position from mouse movement
function mouseMoved() {
mouseAttractor.x = mouseX;
mouseAttractor.y = mouseY;
}
class Mover {
constructor(x, y) {
this.position = createVector(x, y);
this.velocity = createVector();
this.acceleration = createVector();
this.size = random(3, 18);
// colorMode(HSB);
// this.color = color(random(360), random(360), random(360),100);
this.color = color(random(255), random(255), random(255), 100);
}
//basic adding force method
applyForce(force) {
this.acceleration.add(force);
}
//basic update method
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0); // reset accel
// check if mover is outside borders and have it rejoin on other side
if (this.position.x < 0) {
this.position.x = width;
} else if (this.position.x > width) {
this.position.x = 0;
}
if (this.position.y < 0) {
this.position.y = height;
} else if (this.position.y > height) {
this.position.y = 0;
}
}
//basic function method
display() {
noStroke();
fill(this.color);
ellipse(this.position.x, this.position.y, this.size);
}
}