# Multiple particle systems

**URL:** <https://discourse.processing.org/t/multiple-particle-systems/7650>\
**Category:** Libraries\
**Created:** [January 19, 2019, 4:08pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650 "2019-01-19T16:08:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![larppan](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@larppan](https://discourse.processing.org/u/larppan)\
**Post date:** [January 19, 2019, 4:08pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650/1 "2019-01-19T16:08:36Z")

</div>

Hi guys!  
I´m writing code for a Uni project and I stumbled on a problem. Basically I´m making an interactive installation and I have a kinect with which I will track hands and create these particle effects.  
My problem is that i´m not sure how i could have multiple particle effects at the same time. Right now I only have it on one of the tracked hands.  
I´d appreciate any tips or guidance to an example!  
Thanks!

And here is the particle system tab code.

```auto
class ParticleSystem {
  ArrayList<Particle> particles;
  PShape particleShape;

  ParticleSystem(int n) {
    particles = new ArrayList<Particle>();
    particleShape = createShape(PShape.GROUP);

    for (int i = 0; i < n; i++) {
      Particle p = new Particle();
      particles.add(p);
      particleShape.addChild(p.getShape());
    }

  }

  void update() {
    for (Particle p : particles) {
      p.update();
    }
  }
  

  void setEmitter(float x, float y) {
    for (Particle p : particles) {
      if (p.isDead()) {
        p.rebirth(x, y);
      }
    }
  }

  void display() {

    shape(particleShape);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 19, 2019, 4:52pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650/2 "2019-01-19T16:52:33Z")

</div>

Mind sharing more of the code? I can’t help because I don’t have the `Particle` class!

---

<div class="post-metadata">

**Author:** ![larppan](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@larppan](https://discourse.processing.org/u/larppan)\
**Post date:** [January 19, 2019, 5:03pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650/3 "2019-01-19T17:03:18Z")

</div>

Sorry, here is the particle class:

```auto
class Particle {

  PVector velocity;
  float lifespan = 255;
  
  PShape part;
  float partSize;
  
  PVector gravity = new PVector(0,0.1);

  Particle() {
    partSize = random(10,60);
    part = createShape();
    part.beginShape(QUAD);
    part.noStroke();
    part.texture(sprite);
    part.normal(0, 0, 1);
    part.vertex(-partSize/2, -partSize/2, 0, 0);
    part.vertex(+partSize/2, -partSize/2, sprite.width, 0);
    part.vertex(+partSize/2, +partSize/2, sprite.width, sprite.height);
    part.vertex(-partSize/2, +partSize/2, 0, sprite.height);
    part.endShape();
    
    rebirth(width/2,height/2);
    lifespan = random(255);
  }

  PShape getShape() {
    return part;
  }
  
    void run() {
    update();

  }
  
  void rebirth(float x, float y) {
    float a = random(TWO_PI);
    float speed = random(0.5,4);
    velocity = new PVector(cos(a), sin(a));
    velocity.mult(speed);
    lifespan = 255;   
    part.resetMatrix();
    part.translate(x, y); 
  }
  
  boolean isDead() {
    if (lifespan < 0) {
     return true;
    } else {
     return false;
    } 
  }
  

  public void update() {
    lifespan = lifespan - 1;
    velocity.add(gravity);
    
    part.setTint(color(255,lifespan));
    part.translate(velocity.x, velocity.y);
  }
}

```

And the main tab:

```auto
import KinectPV2.KJoint;
import KinectPV2.*;
import processing.net.*;

KinectPV2 kinect;
float rightX;
float rightY;
float leftX;
float leftY;

float[] x = new float[50]; 
float[] y = new float[50]; 

ArrayList<ParticleSystem> systems;
ParticleSystem ps;
PImage sprite;  

void setup() {
  size(1200, 800, P3D);
  kinect = new KinectPV2(this);
  kinect.enableSkeletonColorMap(true);
  kinect.enableColorImg(true);
  kinect.init();
  noStroke(); 
  frameRate(60);
  for (int o = 0; o<x.length; o++) {
    x[o] = 0;
    y[o] = 0;
  }

  orientation(LANDSCAPE);
  sprite = loadImage("texture1.png");
  ps = new ParticleSystem(1000);

  hint(DISABLE_DEPTH_MASK);
} 

void draw () {

  background(0);
  ArrayList<KSkeleton> skeletonArray = kinect.getSkeletonColorMap();
  fill(0, 200, 0);

  for (int i = 0; i < skeletonArray.size(); i++) {
    KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
    if (skeleton.isTracked()) {
      KJoint[] joints = skeleton.getJoints();
      rightX = joints[KinectPV2.JointType_HandRight].getX();
      rightY = joints[KinectPV2.JointType_HandRight].getY();
      leftX = joints[KinectPV2.JointType_HandLeft].getX();
      leftY = joints[KinectPV2.JointType_HandLeft].getY();
    }

    ps.update();
    ps.display();

    ps.setEmitter(rightX, rightY);

    fill(255);
    textSize(16);
    text("Frame rate: " + int(frameRate), 10, 20);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 19, 2019, 5:33pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650/4 "2019-01-19T17:33:38Z")

</div>

Oh. Now I can see the problem without running any code. You have only 1 single `ParticleSystem`! And you tell it to `setEmitter(rightX, rightY);` only.

You could replace:

```auto
ParticleSystem ps;

```

With:

```auto
ParticleSystem psLeft;
ParticleSystem psRight;

```

And this:

```auto
  ps = new ParticleSystem(1000);

```

With:

```auto
  psLeft = new ParticleSystem(1000);
  psRight = new ParticleSystem(1000);

```

And this:

```auto
    ps.update();
    ps.display();
    ps.setEmitter(leftX, leftY);

```

With:

```auto
    psLeft.update();
    psLeft.display();
    psLeft.setEmitter(leftX, leftY);
    psRight.update();
    psRight.display();
    psRight.setEmitter(rightX, rightY);

```

Or maybe even make an entire `ParticleSystem[]` array for every part of the skeleton and do stuff on all of the systems in the `for` loop!

Does that help?

---

<div class="post-metadata">

**Author:** ![larppan](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@larppan](https://discourse.processing.org/u/larppan)\
**Post date:** [January 19, 2019, 5:41pm UTC](https://discourse.processing.org/t/multiple-particle-systems/7650/5 "2019-01-19T17:41:45Z")

</div>

That did it ! Thank you very much !
