xxxxxxxxxx
// パーティクルクラス
class P{
float x;
float y;
float dx; // 目的地座標
float dy;
float vx; // 速度
float vy;
P parentP; // 親パーティクル
int index; // インデックス(周囲回転時の角度に使用)
float l; // 回転位置
float o; // 回転速度
color c; // 色
float w; // 待機時間
P(float _x, float _y, P p, int i, color _c){
x = _x;
y = _y;
parentP = p;
index = i;
init();
childInit();
c = _c;
w = 0;
}
void init(){
dx = random(width);
dy = random(height);
}
void childInit(){
l = random(100);
o = random(4) - 2;
}
void act(){
// この場合のみ親の周囲を目標位置にする
if(parentP != null){
dx = parentP.x + cos(radians(6 * index + t * o)) * l;
dy = parentP.y + sin(radians(6 * index + t * o)) * l;
if(random(1) < 0.001){
childInit();
}
}
// 速度計算
float tx = dx - x;
float ty = dy - y;
vx = (vx + (tx / 100.0)) / 2;
vy = (vy + (ty / 100.0)) / 2;
// 移動
x += vx;
y += vy;
// 描画
float sz = parentP == null ? 5 : 1;
fill(c);
ellipse(x, y, sz, sz);
// 目標点に到達したら、少し休憩して次の目標点へ移動
float d = sqrt(tx * tx + ty * ty);
if(parentP == null && d < 10){
if(w == 0){
w = random(200);
}else{
w--;
if(w < 0){
w = 0;
init();
}
}
}
}
}
ArrayList<P> lists = new ArrayList<P>();
float t;
color[] cs = [color(255, 255, 0), color(255, 0, 255), color(0, 255, 255), color(0, 255, 0), color(255)];
void setup() {
size(500, 500)
background(0);
noStroke();
for(int i = 0; i < 5; i++){
P p = new P(random(width), random(height), null, 0, cs[i]);
lists.add(p);
for(int j = 0; j < 50; j++){
P child = new P(random(width), random(height), p, j, cs[i]);
lists.add(child);
}
}
}
void draw() {
t += 1;
fill(0, 16);
rect(0, 0, width, height);
fill(255);
for(P p : lists){
p.act();
}
}