Multiple particle systems

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.

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

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

Sorry, here is the particle class:

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:

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

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:

ParticleSystem ps;

With:

ParticleSystem psLeft;
ParticleSystem psRight;

And this:

  ps = new ParticleSystem(1000);

With:

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

And this:

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

With:

    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?

3 Likes

That did it ! Thank you very much !

1 Like