102



int num = 80;
LimitedParticle[] p = new LimitedParticle[num];

void setup(){
size(600,400);
noStroke();
smooth();
for(int i =0; i < num; i++){
float vxx = random(-4,4);
float vyy = random(-10,0);
p[i] = new LimitedParticle(width/2,height/2, vxx, vyy, 5.0, 0.999);
}
}
void draw(){
fill(0,10);
rect(0,0,width,height);
fill(255);
for(int i =0; i < num; i++){
p[i].update();
p[i].display();
}
}

class Particle{
float x,y;
float vx,vy;
float radius;
float gravity = 0.1;
Particle(int x,int y,float vx, float vy, float radius){
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.radius = radius;
}

void update(){
vy = vy + gravity;
y += vy;
x += vx;
}
void display(){
ellipse(x,y,radius*2,radius*2);
}
}

class LimitedParticle extends Particle{
float friction = 0.99;
LimitedParticle(int x, int y, float vx, float vy, float radius,float friction){
super(x,y,vx,vy,radius);
this.friction = friction;
}
void update(){
vy*=friction;
vx *= friction;
super.update();
limit();
}
void limit(){
if(y>height-radius){
vy =-vy;
y = constrain(y,-height*height,height-radius);
}
if((x < radius)||(x >width-radius)){
vx = -vx;
x = constrain(x,radius,width-radius);
}
}
}

コメント