How do I merge these two sketches? Particles moving away from tracked distance with Kinect (v1)

Hello! I have tried and tried and am now close to banging my head on the wall. There is probably something incredibly simple I’m missing, but I just can’t figure this one out on my own.

Basically, I am trying to merge two sketches. I’d like the particles to move away from the user when they approach the right distance from the Kinect (v1). I’ve gone through all the tutorials Shiffman has provided, but I’m pretty stuck. I’ll give the two seperate codes I am aiming to combine, and lastly I’ll add in how I’ve been trying to combine them. I’ve managed to get the particles in to the sketch, but they’re not responding to my movement.

This is the first sketch of the particles (I basically want the kinect to replace the mouse):

ArrayList <Particle> particles;
int num = 300;

void setup() {
  size(800, 600);
  smooth();
  particles = new ArrayList<Particle>();
  for (int i = 0; i < num; i++) {
    particles.add( new Particle() );
  }
}

void draw() {
  background(255);
  //for (int i = 0; i < num; i++) {
  // particles.get(i).move();
  // particles.get(i).draw();
  //}
  
  //p.addParticle(mouseX,mouseY);
   
  for (Particle p : particles) {
    p.move();
    p.sketch();
    p.checkMouse();
  }
}


class Particle {
  float px, py;
  float dx, dy;
  float damp;
  color sc;

  Particle() {
    px = width / 2;
    py = height/2;
    dx = random(width);
    dy = random(height);
    damp = random(0.01, 0.04);
    sc = color(random(100, 255), 0, random(15, 25));
  }

  void move() {
    dx += random(-5, +5);
    dy += random(-5, +5);
    px += (dx - px) * damp;
    py += (dy - py) * damp;
  }

  void sketch() {

    fill(sc, 30);
    noStroke();
    ellipse(px, py, 20, 20);
    fill(100);
    noStroke();
    ellipse(px, py, 5, 5);
  }

  void mouse() {
    dx = mouseX;
    dy = mouseY;
  }

  void checkMouse() {
    float mouseRadius = 50;
    float d = dist(mouseX, mouseY, px, py);
    if (d<mouseRadius && d > 1) {
      dx += (px-mouseX) / d*mouseRadius;
      dy += (py-mouseY) / d*mouseRadius;
    }
  }
}

This is my kinekt sketch (which has a tab for the main sketch, and a second tab for the kinect tracker):
TAB1:

import org.openkinect.freenect.*;
import org.openkinect.processing.*;

//ParticleSystem ps;


// The kinect stuff is happening in another class
KinectTracker tracker;
Kinect kinect;


void setup() {
  size(640, 520);
  kinect = new Kinect(this);
  tracker = new KinectTracker();
  // ps = new ParticleSystem(new PVector(width/2,50));
}

void draw() {
  background(255);

  // Run the tracking analysis
  tracker.track();
  // Show the image
  tracker.display();

  // Let's draw the raw location
  PVector v1 = tracker.getPos();
  fill(50, 100, 250, 200);
  noStroke();
  ellipse(v1.x, v1.y, 20, 20);

  // Let's draw the "lerped" location
  PVector v2 = tracker.getLerpedPos();
  fill(100, 250, 50, 200);
  noStroke();
  ellipse(v2.x, v2.y, 20, 20);

  // Display some info
  int t = tracker.getThreshold();
  fill(0);
  text("threshold: " + t + "    " +  "framerate: " + int(frameRate) + "    " + 
    "UP increase threshold, DOWN decrease threshold", 10, 500);
    rect(0,0,width,height);
    
   // for (int i = 0; i < 4; i++){
     // ps.addParticle(v2.x, v2.y);
//}
//ps.run();
}
// Adjust the threshold with key presses
void keyPressed() {
  int t = tracker.getThreshold();
  if (key == CODED) {
    if (keyCode == UP) {
      t+=5;
      tracker.setThreshold(t);
    } else if (keyCode == DOWN) {
      t-=5;
      tracker.setThreshold(t);
    }
  }
}

TAB2

// Daniel Shiffman
// Tracking the average location beyond a given depth threshold
// Thanks to Dan O'Sullivan

// https://github.com/shiffman/OpenKinect-for-Processing
// http://shiffman.net/p5/kinect/

class KinectTracker {

  // Depth threshold
  int threshold = 745;

  // Raw location
  PVector loc;

  // Interpolated location
  PVector lerpedLoc;

  // Depth data
  int[] depth;
  
  // What we'll show the user
  PImage display;
   
  KinectTracker() {
    // This is an awkard use of a global variable here
    // But doing it this way for simplicity
    kinect.initDepth();
    kinect.enableMirror(true);
    // Make a blank image
    display = createImage(kinect.width, kinect.height, RGB);
    // Set up the vectors
    loc = new PVector(0, 0);
    lerpedLoc = new PVector(0, 0);
  }

  void track() {
    // Get the raw depth as array of integers
    depth = kinect.getRawDepth();

    // Being overly cautious here
    if (depth == null) return;

    float sumX = 0;
    float sumY = 0;
    float count = 0;

    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {
        
        int offset =  x + y*kinect.width;
        // Grabbing the raw depth
        int rawDepth = depth[offset];

        // Testing against threshold
        if (rawDepth < threshold) {
          sumX += x;
          sumY += y;
          count++;
        }
      }
    }
    // As long as we found something
    if (count != 0) {
      loc = new PVector(sumX/count, sumY/count);
    }

    // Interpolating the location, doing it arbitrarily for now
    lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
    lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
  }

  PVector getLerpedPos() {
    return lerpedLoc;
  }

  PVector getPos() {
    return loc;
  }

  void display() {
    PImage img = kinect.getDepthImage();

    // Being overly cautious here
    if (depth == null || img == null) return;

    // Going to rewrite the depth image to show which pixels are in threshold
    // A lot of this is redundant, but this is just for demonstration purposes
    display.loadPixels();
    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {

        int offset = x + y * kinect.width;
        // Raw depth
        int rawDepth = depth[offset];
        int pix = x + y * display.width;
        if (rawDepth < threshold) {
          // A red color instead
          display.pixels[pix] = color(150, 50, 50);
        } else {
          display.pixels[pix] = img.pixels[offset];
        }
      }
    }
    display.updatePixels();

    // Draw the image
    image(display, 0, 0);
  }

  int getThreshold() {
    return threshold;
  }

  void setThreshold(int t) {
    threshold =  t;
  }
}

And lastly, here is the mess I am making. It keeps telling me v2 can’t be resolved to a variable so I tried making it global. I feel as though I need to move the particle stuff in to a separate tab but I can’t figure out how to do it:

TAB1

/////KINECT/////
import org.openkinect.freenect.*;
import org.openkinect.processing.*;
KinectTracker tracker;
Kinect kinect;





ArrayList <Particle> particles;
int num = 300;

void setup() {
  size(640, 420);
  /////KINECT/////
    kinect = new Kinect(this);
  tracker = new KinectTracker();
  
  smooth();
  particles = new ArrayList<Particle>();
  for (int i = 0; i < num; i++) {
    particles.add(new Particle());
  }
}

void draw() {
  background(255);

/////KINECT/////
 // Run the tracking analysis
  tracker.track();
  // Show the image
  tracker.display();
  // Let's draw the raw location
  PVector v1 = tracker.getPos();
  fill(50, 100, 250, 200);
  noStroke();
  ellipse(v1.x, v1.y, 20, 20);

  // Let's draw the "lerped" location
  PVector v2 = tracker.getLerpedPos();
  fill(100, 250, 50, 200);
  noStroke();
  ellipse(v2.x, v2.y, 20, 20);


   
  for (Particle p : particles) {
    p.move();
    p.sketch();
    //p.checkMouse();
    
        
  }
float dx = v2.x;
   float  dy = v2.y;
  float px = width / 2;
   float py = height/2;
  //void checkMouse() {
    float mouseRadius = 50;
    float d = dist(v2.x,v2.y, px, py);
    if (d<mouseRadius && d > 1) {
      dx += (px-v2.x) / d*mouseRadius;
      dy += (py-v2.y) / d*mouseRadius;
    }
  }
//}


class Particle {
  float px, py;
  float dx, dy;
  float damp;
  color sc;

  Particle() {
    px = width / 2;
    py = height/2;
    dx = random(width);
    dy = random(height);
    damp = random(0.01, 0.04);
    sc = color(random(100, 255), 0, random(15, 25));
  }

  void move() {
    dx += random(-5, +5);
    dy += random(-5, +5);
    px += (dx - px) * damp;
    py += (dy - py) * damp;
  }

  void sketch() {

    fill(sc, 30);
    noStroke();
    ellipse(v2.x, v2.y, 20, 20);
    fill(100);
    noStroke();
    ellipse(v2.x, v2.y, 5, 5);
  }

  //void mouse() {
  //  dx = mouseX;
  //  dy = mouseY;
  //}

  //void checkMouse() {
  //  float mouseRadius = 50;
  //  float d = dist(float x, float y, px, py);
  //  if (d<mouseRadius && d > 1) {
  //    dx += (px-mouseX) / d*mouseRadius;
  //    dy += (py-mouseY) / d*mouseRadius;
  //  }
  //}
}

TAB2

  int[] depth;
  
  // What we'll show the user
  PImage display;
   
  KinectTracker() {
    // This is an awkard use of a global variable here
    // But doing it this way for simplicity
    kinect.initDepth();
    kinect.enableMirror(true);
    // Make a blank image
    display = createImage(kinect.width, kinect.height, RGB);
    // Set up the vectors
    loc = new PVector(0, 0);
    lerpedLoc = new PVector(0, 0);
  }

  void track() {
    // Get the raw depth as array of integers
    depth = kinect.getRawDepth();

    // Being overly cautious here
    if (depth == null) return;

    float sumX = 0;
    float sumY = 0;
    float count = 0;

    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {
        
        int offset =  x + y*kinect.width;
        // Grabbing the raw depth
        int rawDepth = depth[offset];

        // Testing against threshold
        if (rawDepth < threshold) {
          sumX += x;
          sumY += y;
          count++;
        }
      }
    }
    // As long as we found something
    if (count != 0) {
      loc = new PVector(sumX/count, sumY/count);
    }

    // Interpolating the location, doing it arbitrarily for now
    lerpedLoc.x = PApplet.lerp(lerpedLoc.x, loc.x, 0.3f);
    lerpedLoc.y = PApplet.lerp(lerpedLoc.y, loc.y, 0.3f);
  }

  PVector getLerpedPos() {
    return lerpedLoc;
  }

  PVector getPos() {
    return loc;
  }

  void display() {
    PImage img = kinect.getDepthImage();

    // Being overly cautious here
    if (depth == null || img == null) return;

    // Going to rewrite the depth image to show which pixels are in threshold
    // A lot of this is redundant, but this is just for demonstration purposes
    display.loadPixels();
    for (int x = 0; x < kinect.width; x++) {
      for (int y = 0; y < kinect.height; y++) {

        int offset = x + y * kinect.width;
        // Raw depth
        int rawDepth = depth[offset];
        int pix = x + y * display.width;
        if (rawDepth < threshold) {
          // A red color instead
          display.pixels[pix] = color(150, 50, 50);
        } else {
          display.pixels[pix] = img.pixels[offset];
        }
      }
    }
    display.updatePixels();

    // Draw the image
    image(display, 0, 0);
  }

  int getThreshold() {
    return threshold;
  }

  void setThreshold(int t) {
    threshold =  t;
  }
}

Thank you in advance for any help! I really appreciate anyone taking their time for this :slight_smile: