kinectpv2/toxiclibs particle interaction. (multiple users)

edited March 2017 in Kinect

The kinectpv2 library allows for up to 6 simultaneous users, I am using this library to interact with particles using the toxilibs verlet library. My question is, how can I get all 6 users to attract with particles. I have tried using multiple Vec2D's for my mouseAttractor/mousePos with no luck.

My goal is to have 6 independent users with their own respective attraction forces.

Thank you.

EnergyFloor floorEnergy;
AttractionBehavior mouseAttractor_FloorEnergy;
Vec2D mousePos_FloorEnergy;
AttractionBehavior mouseAttractor_FloorEnergy2;
Vec2D mousePos_FloorEnergy2;
AttractionBehavior mouseAttractor_P;
Vec2D mousePos_P;

void setup() {
  fullScreen(P3D, 2);
  floorEnergy = new EnergyFloor();

  kinect = new KinectPV2(this);
  kSkel = new KinectSkeleton();
}

void draw() {
  background(0, 0, 33);
  kSkel.display();

  floorEnergy.physics.update();
  floorEnergy.drawParticles();
  floorEnergy.drawLines();

  mousePos_P = new Vec2D(mouseX, mouseY);

  floorEnergy.physics.removeBehavior(mouseAttractor_P);
  mouseAttractor_P = new AttractionBehavior(mousePos_P, 250, 13);
  floorEnergy.physics.addBehavior(mouseAttractor_P);
}



import toxi.geom.*;
import toxi.physics2d.*;
import toxi.physics2d.behaviors.*;
import toxi.math.*;
import megamu.mesh.*;




public class EnergyFloor {
  int NUM_PARTICLES = 2000;

  VerletPhysics2D physics;
  AttractionBehavior mouseAttractor;
  float [][] pos;
  Vec2D mousePos;
  Delaunay delaunay;
  int [] polygons;

  EnergyFloor() {
    //blendMode(SUBTRACT);
    physics = new VerletPhysics2D();
    physics.setDrag(0.2);
    physics.setWorldBounds(new Rect(0, 0, width, height));
    //physics.setWorldBounds(new Rect(width/2-33, 0, 90, height));
    polygons = new int[NUM_PARTICLES];
    // the NEW way to add gravity to the simulation, using behaviors
    physics.addBehavior(new GravityBehavior(new Vec2D(0, 0f)));
    for (int i = 0; i<NUM_PARTICLES; i++) {
      addParticle();
    }
  }
  void addParticle() {
    VerletParticle2D p = new VerletParticle2D(Vec2D.randomVector().scale(666).addSelf(width / 2, random(height)));
    physics.addParticle(p);
    // add a negative attraction force field around the new particle
    physics.addBehavior(new AttractionBehavior(p, 0.3, 3f, 0.01f));
  }

  void drawParticles() {
    // store particles positions to do delaunay triangulation
    pos = new float[NUM_PARTICLES][2];

    for ( int i=0; i<NUM_PARTICLES; i++) {
      // particle system using verlet integration
      VerletParticle2D p = physics.particles.get(i);

      pos[i][0] = physics.particles.get(i).x;
      pos[i][1] = physics.particles.get(i).y;

      if (p.x < 0) {
        p.x = width;
      }

      if (p.x > width) {
        p.x = 0;
      }

      if (p.y < 0) {
        p.y = height;
      }

      if (p.y > height) {
        p.y = 0;
      }
    }
  }

  // delaunay triangulation logic taken from here : 
  // http://www.openprocessing.org/sketch/43503
  void drawLines() {
    // delaunay triangulation
    delaunay = new Delaunay(pos);
    // getEdges returns a 2 dimensional array for the lines
    float[][] edges = delaunay.getEdges();
    for (int i=0; i<edges.length; i++) {
      // use the edges values to draw the lines
      float startX2 = edges[i][0];
      float startY2 = edges[i][3];
      float endX2 = edges[i][1];
      float endY2 = edges[i][3];

      float startX = edges[i][0];
      float startY = edges[i][1];
      float endX = edges[i][2];
      float endY = edges[i][3];
      float distance = dist(startX, startY, endX, endY);
      //float distance2 = dist(startX2, startY2, endX2, endY2);
      // remap the distance to opacity values
      float trans = 255-map(distance, 0, 33, 0, 255);
      // stroke weight based on distance
      // fast invert square root helps for performance
      float sw = 3f/sqrt(distance*3);

      pushStyle();
      strokeWeight(sw);
      stroke(233, 113, 33, trans);
      line(startX, startY, endX, endY);
      popStyle();
      pushStyle();
      ellipseMode(CENTER);
      noStroke();
      fill(133, 1);
      ellipse(startX2, startY2, endX2/23, endY2/23);
      popStyle();
    }
  }
}



import java.util.ArrayList;
import KinectPV2.KJoint;
import KinectPV2.*;

KinectPV2 kinect;
KinectSkeleton kSkel;

public class KinectSkeleton {


  KinectSkeleton() {
    kinect.enableSkeleton3DMap(true);
    kinect.init();
  }

  void display() {
    pushMatrix();
    translate(width/2, height/2, 0);

    ArrayList<KSkeleton> skeletonArray =  kinect.getSkeleton3d();

    //individual JOINTS
    for (int i = 0; i < skeletonArray.size(); i++) {
      KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
      if (skeleton.isTracked()) {
        KJoint[] joints = skeleton.getJoints();


        drawBody(joints);
      }
    }
    popMatrix();
  }
}

void drawBody(KJoint[] joints) {
  drawBone(joints, KinectPV2.JointType_SpineBase, KinectPV2.JointType_Head);
}

void drawBone(KJoint[] joints, int jointType1, int jointType2) {

  float xMapped = map(joints[jointType1].getX(), -1.28, 1, 0, width);
  //float yMapped = map(joints[jointType1].getY(), -0.3, 0.07, 0, height);
  float zMapped = map(joints[jointType1].getZ(), 1, 8, 0, height*2);


  mousePos_FloorEnergy = new Vec2D(xMapped, zMapped);
  //mousePos_FloorEnergy2 = new Vec2D(xMapped, zMapped);

  floorEnergy.physics.removeBehavior(mouseAttractor_FloorEnergy);
  mouseAttractor_FloorEnergy = new AttractionBehavior(mousePos_FloorEnergy, 250, 13);
  floorEnergy.physics.addBehavior(mouseAttractor_FloorEnergy);

  //println(xMapped);
  //println(zMapped);
}

KinectPV2: http://codigogenerativo.com/code/kinectpv2-k4w2-processing-library/

Toxiclibs: http://toxiclibs.org/downloads/

Sign In or Register to comment.