xxxxxxxxxx
106
let movers = [];
let windForce = 5;
let gravForce = 3;
let numberOfMovers = 800;
class Mover {
constructor(x, y, r, angle, weight) {
this.position = createVector(x, y);
this.velocity = createVector(0, 0);
this.accel = createVector(0, 0);
this.mass = weight;
}
applyForce(force) {
var f = p5.Vector.div(force, this.mass);
this.accel.add(f);
}
// TODO: add update(), draw(), checkEdges()
}
// "constructor" function: constructor: "create an object"
// function Mover(x, y, r, angle, weight) {
// }
var makeMover = function(x, y, r, angle, weight) {
return {
position: createVector(x, y),
velocity: createVector(0, 0),
accel: createVector(0, 0),
mass: weight,
applyForce: function(force) {
var f = p5.Vector.div(force, this.mass);
this.accel.add(f);
},
update: function(v) {
// this.velocity.limit(5);
this.velocity.add(this.accel);
this.position.add(this.velocity);
// This is playing a big role
// Keep this at zero?
this.accel.mult(0);
},
draw: function() {
noStroke();
fill(map(this.position.y, 0, height, 255, 0), map(this.position.x, 0, width, 255, 0), 80);
ellipse(this.position.x, this.position.y, 15);
},
checkEdges: function() {
if (this.position.x > width) {
this.position.x = width / 2;
this.velocity.x *= -1;
} else if (this.position.x < 0) {
this.velocity.x *= -1;
this.position.x = 0;
}
if (this.position.y > height) {
this.velocity.y *= -1;
this.position.y = height;
}
}
}
};
function setup() {
createCanvas(650, 600);
for (var i = 0; i < numberOfMovers; i++) {
// change constructor function to class
// movers[i] = new Mover(random(width), random(height), 20, 10, 50)
movers[i] = makeMover(random(width), random(height), 20, 10, 50);
}
}
function draw() {
background(17, 103, 177);
// console.log(windForce);
// Maybe play with adjusting the windforce rather than acceleration
// in particle
windForce *= 1.01;
var wind = createVector(windForce, 0);
var gravity = createVector(0, gravForce);
for (var i = 0; i < movers.length; i++) {
movers[i].applyForce(wind);
movers[i].applyForce(gravity);
movers[i].update();
movers[i].draw();
movers[i].checkEdges();
}
}
//INSPO https://editor.p5js.org/mgs/sketches/ryBgD8vue