Kinect, how to create different rotation if i map to distance hands & how to move peasyCam with hand

edited April 2015 in Kinect

Dear all, I've connected this code to the kinect, what happens is that cause I map the rotation of the cubes to the distance of the hands, they all rotate at the same time and with same degree of rotation. Originally I had map the rotation to distance between each element to mouse so it was gradual and different. Here I started creating an array and a function so that the kinect sends the data with certain delay to each element so they rotate differently

  void trailTempo(float new_value) {
  for ( int i = 0; i < arrayTempoXrotation.length; ++i )
  {
    // pass old value to previous array slot
    if(i>0) arrayTempoXrotation[i-1] = arrayTempoXrotation[i];
    //set to last value the new value
    if(i==(arrayTempoXrotation.length-1))arrayTempoXrotation[i] = new_value;
  }
}

but I got stuck not sure how to assign it. Another question is how can I control the peasyCam with the hands instead of with the mouse. Any help greatly appreciated!!!!

        
 //kinect code
 
import kinect4WinSDK.Kinect;
import kinect4WinSDK.SkeletonData;

Kinect kinect;
ArrayList  bodies;
PVector shoulder = new PVector();
PVector handLeft = new PVector();
PVector handRight = new PVector();
// paola code
import processing.pdf.*;
import peasy.*;

//--------------------------------for 3d environment
PeasyCam cam;
PMatrix3D currCameraMatrix;
PGraphics3D g3;
//--------------------------------



boolean record;
ArrayList manyOmetti = new ArrayList();

boolean cambiaRotazioneOmetti=true;
boolean stretchOmetti=false;

float variazioneX;
float Xref, Yref, Zref;//-------------------------------
int direct=1;
float mov;

// variabili x fare cambio di file usando tempo
float lastMillis;
float timer;
float durata=50000;
float durata2=100000;
float durata3=150000;
boolean spazio=true;

boolean movimentoQuality=true;
boolean rotX=true;

// ARRAY AVERAGE para ke valores kinect no sean tan locos

float[] arrayDistance; 
float[] arrayTempoXrotation; 

// default setup
int averageSampling = 100;
int sampleOmetti = 182;
 

void setup() {
  size(900, 700, P3D); //------------------------------------P3D for 3d
  frameRate(30);
  smooth();
  
  //------------------------------------camSettings
    g3 = (PGraphics3D)g;
  cam = new PeasyCam(this,800,800,0, 1500); 
  /*los valores de la camara van al reves que e
  los del screen de processing para que salga arriba es + y para abajo es -*/
  cam.setMinimumDistance(1100);
  cam.setMaximumDistance(2000);

  //------------------------------------
  
  Xref= 0;
  Yref= height/2;
  Zref=0;//-------------------------------
  
  //variabili kinect
   kinect = new Kinect(this);
  smooth();
  bodies = new ArrayList();
  
  //variabili durata x creare struttura temporale
  timer=millis();
  
  //EL ARRAY
  arrayDistance = new float[averageSampling];
for ( int i = 0; i < arrayDistance.length; ++i )
{
  arrayDistance[i]= 0.0;
}


arrayTempoXrotation = new float[sampleOmetti];
for ( int i = 0; i < arrayTempoXrotation.length; ++i )
{
  arrayTempoXrotation[i]= 0.0;
}

}



void draw() {


  background(#F2F1E8);
  mov=map(mouseX, 0, width, 0.3, 1.5);
  // questo e`il punto di riferenza che ora ho tolto e messo il map alla kinect
  //pero muove lo spazio e gli da molta + dinamica

  Xref+=mov*direct;
  if (Xref>width||Xref<0) {
    direct*=-1;
  }
  
  // struttura timer
if(millis()-timer<durata){
  spazio=true;
  movimentoQuality=true;
}
else if(millis()-timer>durata &&millis()-timerdurata2 &&millis()-timerdurata3){
  timer=millis();
}

  //we add a box to the bottom of the sketch as a "reference"
  pushMatrix();
  noFill();
  stroke(1);
  strokeWeight(1);
  translate(width/2, height/2, 0);
 // box(width-10, height-10, 1);
  popMatrix();

  translate(Xref, Yref, Zref);//------------------------------------
  sphere( 10);//------------------------------------

    iniziaMarkers2();

  for (Ometti ometti : manyOmetti) {
    ometti.display();
  }
  
  
 //KINECT CODE
 
 cam.beginHUD();
 image(kinect.GetImage(), 320, 0, 320, 240);
 image(kinect.GetDepth(), 320, 240, 320, 240);
  image(kinect.GetMask(), 0, 240, 320, 240);
  cam.endHUD();
  
  for (int i=0; i0) arrayDistance[i-1] = arrayDistance[i];
    //set to last value the new value
    if(i==(arrayDistance.length-1))arrayDistance[i] = new_value;
  }

  // This is where we compute the average
  float average = 0; 
  for ( int i = 0; i < arrayDistance.length; ++i ) 
  { 
    average += arrayDistance[i];
  } 
  average /= (float)(arrayDistance.length); 

  // The average should be close to zero
  return average;
}

void trailTempo(float new_value) {
  for ( int i = 0; i < arrayTempoXrotation.length; ++i )
  {
    // pass old value to previous array slot
    if(i>0) arrayTempoXrotation[i-1] = arrayTempoXrotation[i];
    //set to last value the new value
    if(i==(arrayTempoXrotation.length-1))arrayTempoXrotation[i] = new_value;
  }

}

void getData(String csvFile) {
  manyOmetti.clear();

  String[] lines = loadStrings(csvFile);
  String data = join(lines, ",");
  String[] e = split(data, ",");
 // println(e.length);
  for (int i = 0; i < e.length; i += 5) {
    float x = Float.valueOf(e[i+1]);
    float y = Float.valueOf(e[i+2]);

    float z = Float.valueOf(e[i+1]);//------------------------------------random value here for Z

    float r = Float.valueOf(e[i+3]);
 
    float scala = Float.valueOf(e[i+4]);
    if (e[i].equals("O")) 
      manyOmetti.add(new Ometti(x, y, z, scala, r));
  }
}




void iniziaMarkers2() {
  if (spazio==true) {

    getData("omettiAli.csv");
  } else if (spazio==false) {

    getData("Ali4Aprile.csv");
  } 
}

void keyPressed() {


  if (key=='t') {
    cambiaRotazioneOmetti=!cambiaRotazioneOmetti;
  }
  if (key=='s') {
    stretchOmetti=!stretchOmetti;
  }


  if (key=='p') {
    String image_name = day()+""+hour()+""+minute()+""+second()+""+millis()+".png";
    save(image_name);
  }

  //PDF
  if (key=='m') { 
    beginRecord(PDF, "frame-####.pdf"); 
    record = true;
  }

  if (key=='n') {  
    endRecord();
    record= false;
  }
}

class Ometti {
  float colore;
  float x, y, z, rotazione;// ---------------------------- z axis added here

    float grossore=1;
  int A=-200, B=-200;

  Ometti(float positionX_, float positionY_, float positionZ_, float grossore_, float rotazio) {// ---------------------------- added z here
    z=positionZ_;// ----------------------------
    x=positionX_;
    y=positionY_;
    grossore=grossore_;
    rotazione=rotazio;

  }

  void display() {

   float d = dist(handLeft.x,handLeft.y, handRight.x,handRight.y);
   println(d);
   //TESTING col maxdist x capire perche quello senza kinect si muove bellissimo e 
   // questo fa un po cagare
//float maxDist= dist(0, 0, width/2, height/2);


    if (cambiaRotazioneOmetti==true) {
  
      //kinect map
        rotazione=map(d, 0.01,0.7, 0, 8); //questo se suppone è corretto
        
        //  rotazione=map(d, 0, maxDist, 0, 8);
        //println(d);
    } else if (cambiaRotazioneOmetti==false) {
      rotazione=rotazione;
    }

    
    pushMatrix();
    translate(x+variazioneX, y, z);
    if(movimentoQuality==false){   
    rotate(rotazione);
    }
    else if(movimentoQuality==true&& rotX==true){
       rotateX(rotazione);
    }
    else if(movimentoQuality==true&& rotX==false){
       rotateY(rotazione);
    }

    scale(grossore);
    strokeWeight (2);
    stroke(0);


    rectMode(CENTER);
    //fill(255, 0, 0, 111);
    fill(#2555D6,201); //azul
    stroke( 0);
    strokeWeight(1);
    //  rect(A+200,B+200,170,15);
    translate (170, 15, 0 );//------------------------------------box method instead of a rect

    box(A+220, B+220, 250);//------------------------------------


    translate(A+200, B+200, 10);//------------------------------------sphere method added
    // stroke(mouseX * 2, 0, 210);//added this line for color code
   fill(#F7057E,100);
   stroke(200);
   // sphere(30);
   float grandezza=map(d,0.01,0.7,30,100);
    sphere(grandezza);


    stroke(0);
      strokeWeight(5);
    line(A+190, B+190, 50, A+220, B+190, 50);//------------------------------------

    point(A+215, B+220);
    popMatrix();
  }
}
void drawPosition(SkeletonData _s) 
{
  noStroke();
  fill(0, 100, 255);
  String s1 = str(_s.dwTrackingID);
  text(s1, _s.position.x*width/2, _s.position.y*height/2);
}

void drawSkeleton(SkeletonData _s) 
{

  // Body
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HEAD, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_CENTER, 
  Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, 
  Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_SPINE);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SPINE, 
  Kinect.NUI_SKELETON_POSITION_HIP_CENTER);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HIP_CENTER, 
  Kinect.NUI_SKELETON_POSITION_HIP_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HIP_CENTER, 
  Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HIP_LEFT, 
  Kinect.NUI_SKELETON_POSITION_HIP_RIGHT);

  // Left Arm
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_LEFT, 
  Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_ELBOW_LEFT, 
  Kinect.NUI_SKELETON_POSITION_WRIST_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_WRIST_LEFT, 
  Kinect.NUI_SKELETON_POSITION_HAND_LEFT);

  // Right Arm
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_SHOULDER_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_ELBOW_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_WRIST_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_HAND_RIGHT);

  // Left Leg
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HIP_LEFT, 
  Kinect.NUI_SKELETON_POSITION_KNEE_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_KNEE_LEFT, 
  Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_ANKLE_LEFT, 
  Kinect.NUI_SKELETON_POSITION_FOOT_LEFT);

  // Right Leg
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_HIP_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_KNEE_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT);
  DrawBone(_s, 
  Kinect.NUI_SKELETON_POSITION_ANKLE_RIGHT, 
  Kinect.NUI_SKELETON_POSITION_FOOT_RIGHT);
}

void DrawBone(SkeletonData _s, int _j1, int _j2) 
{
  noFill();
  stroke(255, 255, 0);
  if (_s.skeletonPositionTrackingState[_j1] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED &&
    _s.skeletonPositionTrackingState[_j2] != Kinect.NUI_SKELETON_POSITION_NOT_TRACKED) {
    line(_s.skeletonPositions[_j1].x*width/2, 
    _s.skeletonPositions[_j1].y*height/2, 
    _s.skeletonPositions[_j2].x*width/2, 
    _s.skeletonPositions[_j2].y*height/2);
  }
}

void appearEvent(SkeletonData _s) 
{
  if (_s.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) 
  {
    return;
  }
  synchronized(bodies) {
    bodies.add(_s);
  }
}

void disappearEvent(SkeletonData _s) 
{
  synchronized(bodies) {
    for (int i=bodies.size ()-1; i>=0; i--) 
    {
      if (_s.dwTrackingID == bodies.get(i).dwTrackingID) 
      {
        bodies.remove(i);
      }
    }
  }
}

void moveEvent(SkeletonData _b, SkeletonData _a) 
{
  if (_a.trackingState == Kinect.NUI_SKELETON_NOT_TRACKED) 
  {
    return;
  }
  synchronized(bodies) {
    for (int i=bodies.size ()-1; i>=0; i--) 
    {
      if (_b.dwTrackingID == bodies.get(i).dwTrackingID) 
      {
        bodies.get(i).copy(_a);
        break;
      }
    }
  }
}
Sign In or Register to comment.