class Part
{
Vector3D l;
Vector3D v;
Vector3D a;
float m;
float RMIN;
float RMAX;
int cl = 0;
Part(Vector3D l,Vector3D v,Vector3D a, float m)
{
this.l = l;
this.v = v;
this.a = a;
this.m = m;
}
void live()
{
update();
}
void update()
{
v.add(a);
l.add(v);
a.setX(0.0f);
a.setY(0.0f);
// disegna
float col = map(m, 1, 10, 1, 360);
stroke(col, 100, 100, 10);
//print(col+" ");
point(l.x, l.y);
}
boolean pop() {
if (((l.x > width-0) || (l.x < 0)) || ((l.y > height-0) || (l.y < 0))){
return true;
}
else return false;
}
void setLoc(Vector3D v) {
l = v.copy();
}
void setVel(Vector3D v) {
v = v.copy();
}
void setAcc(Vector3D v) {
a = v.copy();
}
void add_force(Vector3D force) {
force.div(m);
a.add(force);
}
Vector3D getVel() {
return v.copy();
}
Vector3D getAcc() {
return a.copy();
}
Vector3D getLoc() {
return l.copy();
}
}
import noc.*;
int PMAX = 35000;
ArrayList p = new ArrayList(PMAX);
//Part[] p = new Part[20000];
float speed = 0;
float yCoord = 0;
float myr;
float c = 0.0;
void setup()
{
size(800,600, P3D);
//smooth();
colorMode(HSB, 360, 100, 100, 100);
for(int i=0; i<PMAX; i++){
Vector3D a = new Vector3D();
Vector3D v = new Vector3D();
Vector3D l = new Vector3D(random(10, width-10),random(10, height-10));
float m = random(1, 10);
// float r = noise(off)*60; // estrae un numero che vale 1/2 di di r
// off += 0.01;
// print(" " + r);
p.add(new Part (l, v, a, m));
}
}
void draw()
{
//background(1, 0, 100, 100);
noStroke();
fill(1, 100, 0, 75);
rect(0, 0, width, height);
yCoord += speed;
speed += 0.01;
stroke(1, 0, 100, 50);
line(1,yCoord, 4, yCoord);
line(width-4,yCoord, width-2, yCoord);
float progAttrito = map(yCoord, 0, height-10, -0.10, -0.80);
c = progAttrito;
//print(" "+c);
if (yCoord > height-10) {
speed = speed * -0.995;
}
for(int i = p.size()-1; i >= 0; i--){
Part particle = (Part) p.get(i);
//print(" " +i);
particle.live();
// forza grav
Vector3D grav = new Vector3D(0.0,0.40);
particle.add_force(grav);
// attrito
Vector3D actualVel = particle.getVel();
Vector3D attrito = Vector3D.mult(actualVel,c);
particle.add_force(attrito);
if(mousePressed == true){
// il mouse attrae
Vector3D mouseLoc = new Vector3D(mouseX,mouseY);
Vector3D diff = Vector3D.sub(mouseLoc,particle.getLoc());
diff.normalize();
float factor = 0.90;
diff.mult(factor);
particle.setAcc(diff);
}
if (particle.pop()) {
p.remove(i); // rimuove
Vector3D a = new Vector3D();
Vector3D v = new Vector3D();
Vector3D l = new Vector3D(random(10, width-10),random(100)+yCoord);
myr = map(yCoord, 0, height-10, 1, 10);
p.add(new Part (l, v, a, myr)); // aggiunge
}
}
//if(p.isEmpty())
//print(" end ");
}
void keyPressed() {
saveFrame();
}