Обработка. Система частиц - как заставить частицы входить один за другим - PullRequest
0 голосов
/ 15 октября 2019

Я пытаюсь заставить мою систему частиц генерировать частицы одну за другой, а не все одновременно. Мой код в настоящее время генерирует все 100 частиц мгновенно.

Я не очень старался, так как я новичок в кодировании.

У меня есть установка, где я вызываю и обновляю свой класс частиц, и классу которого есть все мои параметры системы частиц.

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);
    }
}

1 Ответ

0 голосов
/ 15 октября 2019

Создайте 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);
    }
} 

Обратите внимание, чтоКласс Particle должен быть адаптирован, поскольку он должен работать с 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);
    }
}
...