xxxxxxxxxx
223
//- questions: how to run() particles in 2D array? number of particles
//why not drop back, finally moves in even velocity?//NEED TO SET ACC, VEL(OTHER WAY THAN TESTING THE NUMBER)//when exceed edge, appear again at the original location. 规律of motion
//set the forceorigin.y to voltage readings, and x goes up gradully// strength of beats.
//sliders to change vel
//how to embody the quality of dust, randomness
//use nature of code to code the nature
//soil directed dance/performance
let count=0;
let grid = 3;
let xcount;
let ycount;
let ylevel;
let dustsize = 1;
let radius = 30;
let dust = []; //let dust or let dist=[]?
let oriPos=[];
let forceorigin;
let gravity;
function setup() {
createCanvas(400, 400);
ylevel = height / 5;
xcount = floor(width / grid);
ycount = floor(ylevel / grid);
forceorigin = createVector();
gravity=createVector(0,0.03);
//axis direction. gravity >0 to point down
dust = new Array(xcount * ycount);
for (let i = 0; i < xcount * ycount; i++) {
dust[i] = new Particle(
(i % xcount) * grid,
height - floor(i / xcount) * grid
);
// oriPos[i]=createVector(dust[i].xStart(),dust[i].yStart());
// oriPos[i]=createVector(dust[i].position.x,dust[i].yStart());
}
}
function draw() {
background(0);
fill(200, 190, 20);
noStroke();
rect(0,height-ylevel,width, ylevel);
// for (let i = 0; i < dust.length; i++) {
// if (dust[i].isFly(forceorigin) == true) {
// let forcei=dust[i].setForce();
// dust[i].applyForce(forcei);
// }
// dust[i].update();
// dust[i].show();
// }
//for (let i = 0; i < dust.length; i++) {
// dust[i]
// oriPos[i]
//}
for (let dustpar of dust) {
if (dustpar.isFly(forceorigin) == true) {
let vel=dustpar.setDire();
dustpar.applyVel(vel);
dustpar.applyForce(gravity);
count=50;
}
dustpar.update();
dustpar.show();
}
count--;
if(count==0){
console.log("hello");
forceorigin = createVector();
for (let dustpar of dust) {
dustpar.goHome();
}
}
// for(let i=0;i<dust.length;i++){
// if(dust[i].x<0||dust[i].x>width||dust[i].y<0||dust[i].y>height){
// dust[i].goHome();
// }
// // dust[i].update();
// // dust[i].show();
// }
}
// function keyPressed() {
// if (key == ' ') {
// console.log("hello");
// forceorigin = createVector();
// for (let dustpar of dust) {
// dustpar.goHome();
// }
// }
// }
class Particle {
constructor(x, y) {
this.position = createVector(x, y); //dust position
//let oriX=x;
//let oriY=y;
this.origPos = this.position.copy();
this.velocity = createVector(0,0);
this.acceleration = createVector(0, 0);
// this.lifespan = 255.0;
}
xStart(){
return this.position.x;
}
yStart(){
return this.position.y;
}
isFly(forceorigin) {
let diff = p5.Vector.sub(this.position, forceorigin);
if (diff.mag() < radius) {
return true;
} else {
return false;
}
}
setDire() {
let dire=p5.Vector.sub(this.position, forceorigin);
let dis=dire.mag();//how far from the forceorigin?
let strength=map(dis,0,radius,1.5,0.4); // the shorter the dis, the stronger the force
dire.normalize();
dire.mult(strength);
if(dire.heading()>0){ //if heading down
dire.mult(-1); //flip direction
}
return dire;
}
applyVel(vel){
this.velocity.add(vel);
}
applyForce(force) {
this.acceleration.add(force);
}
goHome() {
this.position.x = this.origPos.x;
this.position.y = this.origPos.y;
this.velocity.mult(0);
this.acceleration.mult(0);
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
//this.acceleration.mult(0);
}
show() {
fill(200, 190, 20);
noStroke();
square(this.position.x, this.position.y, 1);
//circle(this.position.x, this.position.y, dustsize);
}
highlight(){
fill(100, 190, 20);
noStroke();
circle(this.position.x, this.position.y, dustsize*2);
}
print(){
// stroke(200);
// line(100,100,this.velocity.x,this.velocity.y);
console.log(this.velocity.mag());
console.log(this.acceleration.mag());
}
stop(){
if(this.position.x<0||this.position.y>width||this.position.y<0||this.position.y>height){
this.position.x=x;
this.position.y=y;
}
}
back(){
this.position.x=xStart();
this.position.y=yStart();
}
}
//1. update force 2. check affected or not 3. affected particles set their own force 4. move towards the force 5.受力分析
function mousePressed() {
forceorigin.x = mouseX;
forceorigin.y = mouseY;
}