xxxxxxxxxx
78
// The Nature of Code
// Daniel Shiffman
// http://natureofcode.com
let m;
let howMany;
function setup() {
createCanvas(640, 360);
m = [];
howMany = 1
for(let i=0; i<howMany; i++){
m = new Mover();
}
}
function draw() {
background(51);
let wind = createVector(0.01, 0);
//let gravity = createVector(0, 0.1);
//m.applyForce(gravity);
for(let i=0; i<howMany; i++)
{
m[i].applyForce(wind);
m[i].update();
m[i].display();
m[i].checkEdges();
}
}
class Mover {
constructor() {
this.mass = 1;
this.position = createVector(30, 30);
this.velocity = createVector(0, 0);
this.acceleration = createVector(0, 0);
}
applyForce(force) {
var f = p5.Vector.div(force, this.mass);
this.acceleration.add(f);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.acceleration.mult(0);
}
display() {
stroke(0);
strokeWeight(2);
fill(255, 127);
ellipse(this.position.x, this.position.y, 48, 48);
}
checkEdges() {
if (this.position.x > width) {
this.position.x = width;
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;
}
}
}