xxxxxxxxxx
106
let tars = [];
// let img;
// function preload() {
// // img = loadImage('yam.jpg');
// }
function setup() {
createCanvas(600, 600);
for (i = 0; i < width; i++) {
tars[i] = new Tar(random(10, 30));
}
}
function draw() {
for (var x = 0; x < 70; x++) {
for (var y = 0; y < 35; y++) {
fill(0, 40, 255)
rect(x * 10, y * 10, 30, 20);
}
}
let dx = 0.05;
let dy = 0.05;
let wd = 5;
let wh = 150;
let noiseZ = frameCount * 0.01;
let noiseY = 0;
fill(0, 100, 200)
rect(0, 300, 600, 600)
fill(224, 224, 224);
for (var y = height / 4; y < 600; y += wd) {
let noiseX = 0;
beginShape();
for (var x = -wd; x <= width + wd * 2; x += wd) {
let n = noise(noiseX, noiseY, noiseZ);
let value = map(n, 0, 1, 0, wh);
curveVertex(x, y + value);
noiseX += dx;
}
endShape();
wd *= 1.1;
dx /= 1.2;
noiseY += dy;
dy /= 1.2;
}
for (i = 0; i < tars.length; i++) {
var wind = createVector(random(-0.8, 0.8), 0);
var gravity = createVector(0, 0.1, 0);
var c = 0.1;
var normal = 1;
var frictionMag = c * normal;
var friction = tars[i].vel.copy();
friction.mult(-1);
friction.normalize();
friction.mult(frictionMag);
tars[i].applyForce(wind);
tars[i].applyForce(gravity);
tars[i].applyForce(friction);
tars[i].update();
tars[i].checkEdges();
tars[i].display();
}
}
function Tar(massTar) {
this.pos = createVector(300, 320);
this.vel = createVector(0, 0);
this.acc = createVector(0, 0);
this.mass = massTar;
this.applyForce = function(force) {
let f = p5.Vector.div(force, this.mass);
this.acc.add(f);
}
this.update = function() {
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
this.checkEdges = function() {
if (this.pos.x < 0) {
this.pos.x = 0;
this.vel.x *= -1;
}
if (this.pos.x > width) {
this.pos.x = width;
this.vel.x *= -1;
}
if (this.pos.y < 0) {
this.pos.y = 0;
this.vel.y *= -1;
}
this.display = function() {
noStroke();
fill(30, 27, 27,10);
circle(this.pos.x, this.pos.y, this.mass*1.5);
}
}
}