# Calculating pmouseX and pmouseY (Kinect)

**URL:** https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051
**Category:** Libraries
**Created:** [May 11, 2021, 5:59pm UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051 "2021-05-11T17:59:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![l3ehfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l3ehfar/32/4074_2.png) [@l3ehfar](https://discourse.processing.org/u/l3ehfar)
#### Post date: [May 11, 2021, 5:59pm UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/1 "2021-05-11T17:59:14Z")

</div>

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:

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

```

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [May 11, 2021, 6:20pm UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/2 "2021-05-11T18:20:23Z")

</div>

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

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

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

```

---

<div class="post-metadata">

### Author: ![l3ehfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l3ehfar/32/4074_2.png) [@l3ehfar](https://discourse.processing.org/u/l3ehfar)
#### Post date: [May 11, 2021, 6:30pm UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/3 "2021-05-11T18:30:58Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [May 12, 2021, 5:52am UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/4 "2021-05-12T05:52:54Z")

</div>

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:

```auto
  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 🙂

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [May 12, 2021, 7:29am UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/5 "2021-05-12T07:29:06Z")

</div>

> [@micuat](#):
>
> `pmouseX` and `pmouseY`

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

---

<div class="post-metadata">

### Author: ![l3ehfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/l3ehfar/32/4074_2.png) [@l3ehfar](https://discourse.processing.org/u/l3ehfar)
#### Post date: [May 12, 2021, 9:39am UTC](https://discourse.processing.org/t/calculating-pmousex-and-pmousey-kinect/30051/6 "2021-05-12T09:39:35Z")

</div>

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