Calculating pmouseX and pmouseY (Kinect)

Hey, I’ve got a visual which interacts with the mouseX, mouseY and pmouseX, pmouseY. I wanna change it into interacting with the Kinect body track, however my problem is that I have no idea about changing the pmouse value with anything similar. I mean I don’t know how the pmouse value is calculated upon the mouseX, mouseY value, to change it with the Kinect position instead of the mouse position.

here is the class code:

  Particle(float x, float y, float speed, float scatterX, float scatterY) {
    this.pos.set(x, y);
    PVector lastPos = new PVector(mouseX, mouseY);
    this.vel = new PVector(pmouseX, pmouseY);
    this.vel.sub(lastPos);
    this.vel.rotate(radians(random(-75, 75)));
    this.vel.limit(12);
    this.vel.mult(random(0.01*speed*(scatterX+1), 0.06*speed*(scatterY+1)));
  }

Do you mean that you need pmouseX equivalent for kinect joint positions?

Here is a pseudo code, it’s actually fairly simple…

int prevX = 0;
void draw() {
  int currentX = kinect.get(LEFT_ELBOW).getX(); // I just made this up
  ...
  float vX = currentX - prevX;
  ...
  prevX = currentX;
}

thanks for your respond. Yeah I have tried something similar but I got strange result, now that I try yours it gets the same strange result too. I would be glad if you run and see the result. I mean with the pmouse value when the mouse doesn’t move the particles will stop recreating but with your code though the mouse doesn’t move, the particles are still being created.

with the pmouse value:

ArrayList<Particle> allParticles = new ArrayList<Particle>();
ArrayList<PVector> memory = new ArrayList<PVector>();
float currentHue = 0;


class Particle {

  PVector pos = new PVector(0, 0);
  PVector vel;
  PVector acc = new PVector(0, 0);
  float h = currentHue;


  Particle(float x, float y, float speed, float scatterX, float scatterY) {
    this.pos.set(x, y);
    PVector lastPos = new PVector(x,y);
    this.vel = new PVector(pmouseX, pmouseY);
    this.vel.sub(lastPos);
    this.vel.rotate(radians(random(-75, 75)));
    this.vel.limit(12);
    this.vel.mult(random(0.01*speed*(scatterX+1), 0.06*speed*(scatterY+1)));
  }

  void move() {
    if (this.vel.mag() > 5) {
      this.vel.mult(0.98);
    } else {
      this.vel.mult(0.95);
    }

    this.vel.add(this.acc);
    this.pos.add(this.vel);
    this.acc.mult(0);
  }
}

int time = 0;

void setup() {
  size(1920,1080, P3D);
  textAlign(CENTER);
}

float speed = 1;
float threshhold=0.01;
float curved = 200;
float max_d = 130;
float scatterX = 0;
float scatterY = 0;


void draw() {
  background(0);
  time = millis();

  for (int i = allParticles.size()-1; i > -1; i--) {
    Particle p = allParticles.get(i);
    p.move();

    stroke(255);
    if (p.vel.mag()*1.25 <= 7) {
      strokeWeight(p.vel.mag()*1.25);
    } else {
      strokeWeight(7);
    }
    point(p.pos.x, p.pos.y);

    if (time>=15000) {
      if (speed<=5) {
        if (threshhold <=0.02) {
          threshhold += 0.0002;
        } else {
          threshhold = 0.02;
        }
      } else {
        if (threshhold<=0.1) {
          threshhold+=0.001;
        } else {
          threshhold = 0.1;
        }
      }
    }

    if (p.vel.mag() < threshhold) {
      allParticles.remove(p);
    }
  }

  for (int i = 0; i < allParticles.size(); i++) {
    Particle p1 = allParticles.get(i);
    for (int j = 0; j < allParticles.size(); j++) {
      Particle p2 = allParticles.get(j);

      if (p1 == p2) {
        continue;
      }

      stroke(255, p1.vel.mag()+20);
      noFill();
      float d = dist(p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y);
      if (d < max_d) {
        strokeWeight(3);
        if (speed <= 5) {
          scatterX = 500/(i+1) * sin(p1.pos.x * 0.03 + frameCount * 0.07 + i * TWO_PI / 3);
          scatterY = 500/(i+1) * sin(p1.pos.y * 0.03 + frameCount * 0.07 + i * TWO_PI / 3);
        } else {
          scatterX = 0;
          scatterY = 0;
        }
        curve(p1.pos.x, p1.pos.y + curved, p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y, p2.pos.x, p2.pos.y-(curved/2));
      }
    }
  }

  if (time>=15000) {
    if (speed<=6) {
      speed+=0.01;

      if (max_d>=100) {
        max_d -=0.2;
      } else {
        max_d = 100;
      }

      if (curved>=50) {
        curved-=0.2;
      } else {
        curved =50;
      }
    } else if (speed>=6 && speed<=10) {

      if (max_d>=100) {
        max_d -= 0.5;
      } else {
        max_d = 100;
      }

      speed += 0.5;
      if (curved>=0) {
        curved-=1;
      } else {
        curved = 0;
      }
    } else {
      speed = 10;
    }
  }

  allParticles.add(new Particle(mouseX, mouseY, speed, scatterX, scatterY));
}

with your solution:

ArrayList<Particle> allParticles = new ArrayList<Particle>();
ArrayList<PVector> memory = new ArrayList<PVector>();
float currentHue = 0;


class Particle {

  PVector pos = new PVector(0, 0);
  PVector vel;
  PVector acc = new PVector(0, 0);
  float h = currentHue;


  Particle(float x, float y, float px, float py, float speed, float scatterX, float scatterY) {
    this.pos.set(x, y);
    PVector lastPos = new PVector(x,y);
    this.vel = new PVector(px, py);
    this.vel.sub(lastPos);
    this.vel.rotate(radians(random(-75, 75)));
    this.vel.limit(12);
    this.vel.mult(random(0.01*speed*(scatterX+1), 0.06*speed*(scatterY+1)));
  }

  void move() {
    if (this.vel.mag() > 5) {
      this.vel.mult(0.98);
    } else {
      this.vel.mult(0.95);
    }

    this.vel.add(this.acc);
    this.pos.add(this.vel);
    this.acc.mult(0);
  }
}

int time = 0;

void setup() {
  size(1920,1080, P3D);
  textAlign(CENTER);
}

float speed = 1;
float threshhold=0.01;
float curved = 200;
float max_d = 130;
float scatterX = 0;
float scatterY = 0;

int prevX = 0;
int prevY = 0;

void draw() {
  background(0);
  time = millis();
  
  int currentX = mouseX; // I just made this up
  int currentY = mouseY;
  
  float vX = currentX - prevX;
  float vY = currentY - prevY;
  
  prevX = currentX;
  prevY = currentY;

  for (int i = allParticles.size()-1; i > -1; i--) {
    Particle p = allParticles.get(i);
    p.move();

    stroke(255);
    if (p.vel.mag()*1.25 <= 7) {
      strokeWeight(p.vel.mag()*1.25);
    } else {
      strokeWeight(7);
    }
    point(p.pos.x, p.pos.y);

    if (time>=15000) {
      if (speed<=5) {
        if (threshhold <=0.02) {
          threshhold += 0.0002;
        } else {
          threshhold = 0.02;
        }
      } else {
        if (threshhold<=0.1) {
          threshhold+=0.001;
        } else {
          threshhold = 0.1;
        }
      }
    }

    if (p.vel.mag() < threshhold) {
      allParticles.remove(p);
    }
  }

  for (int i = 0; i < allParticles.size(); i++) {
    Particle p1 = allParticles.get(i);
    for (int j = 0; j < allParticles.size(); j++) {
      Particle p2 = allParticles.get(j);

      if (p1 == p2) {
        continue;
      }

      stroke(255, p1.vel.mag()+20);
      noFill();
      float d = dist(p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y);
      if (d < max_d) {
        strokeWeight(3);
        if (speed <= 5) {
          scatterX = 500/(i+1) * sin(p1.pos.x * 0.03 + frameCount * 0.07 + i * TWO_PI / 3);
          scatterY = 500/(i+1) * sin(p1.pos.y * 0.03 + frameCount * 0.07 + i * TWO_PI / 3);
        } else {
          scatterX = 0;
          scatterY = 0;
        }
        curve(p1.pos.x, p1.pos.y + curved, p1.pos.x, p1.pos.y, p2.pos.x, p2.pos.y, p2.pos.x, p2.pos.y-(curved/2));
      }
    }
  }

  if (time>=15000) {
    if (speed<=6) {
      speed+=0.01;

      if (max_d>=100) {
        max_d -=0.2;
      } else {
        max_d = 100;
      }

      if (curved>=50) {
        curved-=0.2;
      } else {
        curved =50;
      }
    } else if (speed>=6 && speed<=10) {

      if (max_d>=100) {
        max_d -= 0.5;
      } else {
        max_d = 100;
      }

      speed += 0.5;
      if (curved>=0) {
        curved-=1;
      } else {
        curved = 0;
      }
    } else {
      speed = 10;
    }
  }

  allParticles.add(new Particle(mouseX, mouseY, vX, vY, speed, scatterX, scatterY));
}

sorry vX and vY are only for the “illustration purpose” to show how to calculate the velocity. prevX and prevY correspond to pmouseX and pmouseY. However you need to assign these values after you finish the drawing:

  allParticles.add(new Particle(mouseX, mouseY, prevX, prevY, speed, scatterX, scatterY));
  prevX = currentX;
  prevY = currentY;

and thanks for the nice visuals! Good luck with your project :slight_smile:

1 Like

They are not calculated, they are just copied from the previous mouse position. It is just useful to recognize where the mouse has been moved

Oh thanks that makes me so glad.
I appreciate your help! thankyou so much:)

1 Like