xxxxxxxxxx
140
function createGrid(cols, rows) {
let arr = new Array(cols);
for(let i = 0; i < arr.length; i++){
arr[i] = new Array(rows);
for(let j = 0; j < arr[i].length; j++){
arr[i][j] = 0;
}
}
return arr;
}
let w;
let nzoom;
let grid;
let cols;
let rows;
let grav;
let dots;
let friction;
class Force{
constructor(x, y){
this.pos = createVector(x, y);
this.vel = createVector(1, 0);
this.vel.setHeading(noise(this.pos.x / nzoom + this.pos.y / nzoom + t1) * TWO_PI)
}
update(){
this.vel.setHeading(this.vel.heading + sin(noise(this.pos.x / nzoom + this.pos.y / nzoom + t1)))
}
}
class Dot{
constructor(x, y, index){
this.pos = createVector(x, y);
this.vel = createVector(1, 0);
this.vel.setHeading(HALF_PI)
this.cpos = createVector(x, y);
this.index = index;
this.wind = createVector(1, 0);
this.wind.setMag(0.08)
this.wall = undefined;
this.grav = 0;
}
update(){
point(this.pos.x, this.pos.y);
// if(this.pos.x > 0 && this.pos.x < width && this.pos.y > 0 && this.pos.y < height){
// this.vel.add(grid[this.pos.x / w][this.pos.y / w].vel)
// }
this.wind.setHeading(map(noise(this.pos.x / nzoom, this.pos.y / nzoom, t1), 0, 1, -PI, -TWO_PI))
// this.vel.setHeading(this.vel.heading() + sin(t1) * 0.01)
this.vel.add(this.wind);
this.cpos.add(this.vel);
if(this.cpos.x <= 0){
this.wall = createVector(1, 0)
this.vel = p5.Vector.reflect(this.vel, this.wall);
// this.vel.setMag(5)
// grav = 0;
}
if(this.cpos.x >= width){
this.wall = createVector(-1, 0)
this.vel = p5.Vector.reflect(this.vel, this.wall);
// this.vel.setMag(5)
// grav = 0;
}
if(this.cpos.y <= 0){
this.wall = createVector(0, 1)
this.vel = p5.Vector.reflect(this.vel, this.wall);
// this.vel.setMag(5)
// grav = 0;
}
if(this.cpos.y >= height){
this.wall = createVector(0, -1)
this.vel = p5.Vector.reflect(this.vel, this.wall);
// this.vel.setMag(this.vel.mag() * 0.9);
// this.vel.setMag(5)
// grav = 0;
}
// if(grav == 1){
// grav = 1;
// }
this.vel.setMag(this.vel.mag() - friction);
this.vel.add(grav);
this.pos.add(this.vel);
}
}
function setup() {
createCanvas(900, 900);
w = 20;
rows = width / w;
cols = height / w;
nzoom = 1000;
grav = createVector(0, 1);
grav.setMag(0.2);
dots = [];
grid = createGrid(cols, rows);
t1 = 0;
friction = 0.005
for(let i = 0; i < rows; i++){
for(let j = 0; j < cols; j++){
grid[i][j] = new Force(i * w, j * w);
dots[dots.length] = new Dot(i * w, j * w, dots.length);
}
}
strokeWeight(5);
}
function draw() {
background(220);
t1 = t1 + 0.0001;
for(let i = 0; i < dots.length; i++){
dots[i].update();
}
for(let i = 0; i < rows; i++){
for(let j = 0; j < cols; j++){
grid[i][j].update();
}
}
}