LoginSignup
22
21

More than 5 years have passed since last update.

processingで引力と粒子の動き

Last updated at Posted at 2015-06-25

マウスの位置に向かって集まってきますが、引力・質量をパラメータにすることで
ちょっと面白い動きになります。
20150210.jpg

int p_num;

float offsetx;
float offsety;

ArrayList<Particles> p_set = new ArrayList<Particles> ();

void setup () {

    size (1920, 1080);
    colorMode (RGB, 256);
    background (255);
    frameRate (60);

    p_num = 1000;

    offsetx = width / 2;
    offsety = height / 2;

    for (int i = 0; i < p_num; i++) {
        p_set.add (new Particles ());
    }

}

void draw () {

    fade ();

    //background (255);

    //translate (offsetx, offsety);

    for (Particles p: p_set) {
        p.refresh ();
        p.draw ();
    }

    //saveFrame ();

}

void fade () {

    noStroke ();
    fill (255, 5);
    rectMode (CORNER);
    rect (0, 0, width, height);

}

class Particles {

    float mass;
    float px, py;
    float vx, vy;
    float ax, ay;
    float sc;
    float distance;

    Particles () {

        mass = 0;
        px = random (width);
        py = random (height);
        vx = random (-0.5, 0.5);
        vy = random (-0.5, 0.5);
        ax = 0;
        ay = 0;
        sc = 1;

    }

    void refresh () {

        mass = random (1.0, 10.0);
        ax = -1 * vx * 0.2;
        ay = -1 * vy * 0.2;
        ax += (mouseX - px) * 0.1;
        ay += (mouseY - py) * 0.1;

        distance = dist (mouseX, mouseY, px, py);
        if (distance < 10) {
            sc = 11;
        } else {
            sc = 1 / (distance / 100);
        }

        vx += ax / mass * 0.4;
        vy += ay / mass * 0.4;
        px += vx;
        py += vy;

    }

    void draw () {

        stroke (40);
        strokeWeight (1);
        fill (random (255));
        ellipseMode (CENTER);
        ellipse (px, py, 4 * sc, 4 * sc);

    }

}
22
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
21