加工。粒子系统——如何让粒子一个一个进来

Processing. Particle sytem - how to make particles come in one by one

我正在尝试让我的粒子系统一个接一个地生成粒子,而不是同时生成所有粒子。我的代码目前会立即生成所有 100 个粒子。

我还没有尝试太多,因为我是编码新手。

我有一个调用和更新我的粒子 class 的设置,还有一个 class 包含我的粒子系统的所有参数。

int num = 100;
Particle[] p = new Particle[num];

void setup() {
    size(1080, 720);
    colorMode(HSB);
    for (int i = 0; i < num; i ++) {
        p[i] = new Particle(new PVector(random(width), random(height)), 100, 150);
    }
    stroke(255);
}

void draw() {
    background(0);
    for (int i = 0; i < num; i ++) {
        p[i].update(p, i);
    }
}

class Particle {

    PVector pos;
    PVector vel;

    float r, mr;

    float spd = 0.1;
    float max = 2;

    Particle(PVector pos, float r, float mr) {
        this.pos = pos;
        this.r = r;
        this.mr = mr;
        vel = new PVector(random(-1, 1), random(-1, 1));
    }

    void update(Particle[] p, int i) {
        float h = map(mouseX, 0, width, 0, 255);
        pos.add(vel);

        if (pos.x < -10) pos.x = width;
        if (pos.x > width + 10) pos.x = 0;
        if (pos.y < -10) pos.y = height;
        if (pos.y > height + 10) pos.y = 0;

        vel.x = constrain(vel.x + random(-spd, spd), -max, max);
        vel.y = constrain(vel.y + random(-spd, spd), -max, max);

        for (int j = i + 1; j < p.length; j ++) {
            float ang = atan2(pos.y - p[j].pos.y, pos.x - p[j].pos.x);
            float dist = pos.dist(p[j].pos);

            if (dist < r) {
                stroke(h, 255, map(dist, 0, r, 255, 0));
                strokeWeight(map(dist, 0, r, 3, 0));
                line(pos.x, pos.y, p[j].pos.x, p[j].pos.y);

                float force = map(dist, 0, r, 4, 0);
                vel.x += force * cos(ang);
                vel.y += force * sin(ang);
            }
        }

        float ang = atan2(pos.y - mouseY, pos.x - mouseX);
        float dist = pos.dist(new PVector(mouseX, mouseY));

        if (dist < r) {
            stroke(0, 0, map(dist, 0, r, 255, 0));
            strokeWeight(map(dist, 0, r, 3, 0));
            line(pos.x, pos.y, mouseX, mouseY);

            float force = map(dist, 0, r, 30, 0);
            vel.x += force * cos(ang);
            vel.y += force * sin(ang);
        }
        noStroke();
        fill(h, 255, 255);
        ellipse(pos.x, pos.y, 5, 5);
    }
}

创建一个 ArrayList 粒子,但不在 setup() 中添加任何粒子:

ArrayList<Particle> paticles = new ArrayList<Particle>();

void setup() {
    size(400, 400);
    colorMode(HSB);
    stroke(255);
}

连续添加draw()中的粒子。函数millis()用于获取程序启动后的时间:

void draw() {

  int num = 100;
    int interval = 100; // 0.5 seconds
    int time = millis();   // milliseconds  since starting the program
    if (paticles.size() < num && paticles.size()*interval+5000 < time) {
        paticles.add(new Particle(new PVector(random(width), random(height)), 100, 150));
    }

    background(0);
    for (int i = 0; i < paticles.size(); i ++) {
        Particle p = paticles.get(i);
        p.update(paticles, i);
    }
} 

注意,classParticle要适配,因为它要操作变长的ArrayList,而不是定长的数组:

class Particle {

    PVector pos;
    PVector vel;

    float r, mr;

    float spd = 0.1;
    float max = 2;

    Particle(PVector pos, float r, float mr) {
        this.pos = pos;
        this.r = r;
        this.mr = mr;
        vel = new PVector(random(-1, 1), random(-1, 1));
    }

    void update(ArrayList<Particle> paticles, int i) {
        float h = map(mouseX, 0, width, 0, 255);
        pos.add(vel);

        if (pos.x < -10) pos.x = width;
        if (pos.x > width + 10) pos.x = 0;
        if (pos.y < -10) pos.y = height;
        if (pos.y > height + 10) pos.y = 0;

        vel.x = constrain(vel.x + random(-spd, spd), -max, max);
        vel.y = constrain(vel.y + random(-spd, spd), -max, max);

        for (int j = i + 1; j < paticles.size(); j ++) {
            Particle pj = paticles.get(j);
            float ang = atan2(pos.y - pj.pos.y, pos.x - pj.pos.x);
            float dist = pos.dist(pj.pos);

            if (dist < r) {
                stroke(h, 255, map(dist, 0, r, 255, 0));
                strokeWeight(map(dist, 0, r, 3, 0));
                line(pos.x, pos.y, pj.pos.x, pj.pos.y);

                float force = map(dist, 0, r, 4, 0);
                vel.x += force * cos(ang);
                vel.y += force * sin(ang);
            }
        }

        float ang = atan2(pos.y - mouseY, pos.x - mouseX);
        float dist = pos.dist(new PVector(mouseX, mouseY));

        if (dist < r) {
            stroke(0, 0, map(dist, 0, r, 255, 0));
            strokeWeight(map(dist, 0, r, 3, 0));
            line(pos.x, pos.y, mouseX, mouseY);

            float force = map(dist, 0, r, 30, 0);
            vel.x += force * cos(ang);
            vel.y += force * sin(ang);
        }
        noStroke();
        fill(h, 255, 255);
        ellipse(pos.x, pos.y, 5, 5);
    }
}