Quantcast
Channel: Kinect - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 530

Kinect,RotateY,PopMatrix,PushMatrix

$
0
0

hello guys,here i have 3 examples of the same code,the two of them is with kinect and the other with a static point and the mouse.The final installation will be with the two hands of the kinect.I have one problem with the kinect and one with pushMatrix popMatrix and rotate().I will explain in the code below.What are the problems and the what i want to achieve

First example:here the examples are not rotating(the cubes,the models and the area that influence the models)It works perfect except that for the model to be shown,it has to calibrate first with the kinect because the if statements.

import peasy.*;
import saito.objloader.*;
import SimpleOpenNI.*;
import spout.*;

//PrintWriter output;
OBJModel model ;
OBJModel Smodel ;
OBJModel tmpmodel ;

Spout spout;

PeasyCam cam;

SimpleOpenNI kinect;


float z=0;
float easing = 0.005;
float r;
float k;
int VertCount;
PVector[] Verts;
PVector[] locas;
PVector Mouse;
PVector Mouse2;

void setup()
{
  size(640*3, 480*3, P3D);
  frameRate(30);
  noStroke();

  kinect = new SimpleOpenNI(this);

  kinect.enableDepth();
  kinect.enableUser();

  model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  model.enableDebug();
  model.scale(200);
  model.translateToCenter();

  Smodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  Smodel.enableDebug();
  Smodel.scale(200);
  Smodel.translateToCenter();

  tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  tmpmodel.enableDebug();
  tmpmodel.scale(200);
  tmpmodel.translateToCenter();

  //output = createWriter("positions.txt");

  cam = new PeasyCam(this, width/2, height/2, 0, 2300);

  spout = new Spout(this);

  spout.createSender("Self kinect");
}


void draw()
{
  background(0);
  lights();

  kinect.update();
  IntVector userList = new IntVector();
  kinect.getUsers(userList);

  int VertCount = model.getVertexCount ();
  Verts = new PVector[VertCount];
  locas = new PVector[VertCount];
  r =300;
  //k = k + 0.01;

  cam.setMouseControlled(false);


  if (userList.size() > 0) {

    textSize(300);
    text("Start1", width/2-150, height/2-150);
    fill(0, 102, 153);

    int userId = userList.get(0);

    if ( kinect.isTrackingSkeleton(userId)) {

      textSize(300);
      text("Start2", width/2-150, height/2-150);
      fill(0, 102, 153);

      PVector rightHand = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);

      PVector convertedRightHand = new PVector();
      kinect.convertRealWorldToProjective(rightHand, convertedRightHand);

      PVector leftHand = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, leftHand);

      PVector convertedLeftHand = new PVector();
      kinect.convertRealWorldToProjective(leftHand, convertedLeftHand);


      PVector rightShoulder = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, rightShoulder);

      PVector convertedRightShoulder = new PVector();
      kinect.convertRealWorldToProjective(rightShoulder, convertedRightShoulder);


      PVector leftShoulder = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, leftShoulder);

      PVector convertedleftShoulder = new PVector();
      kinect.convertRealWorldToProjective(leftShoulder, convertedleftShoulder);


      //output.println(" This is the firstPose "+"rightHand "+rightHand+" leftHand "+leftHand);



      float rightHandZ =  map(rightHand.z, 5500, 7500, 1100, 1500);
      float ConrightHandZ = map(rightHandZ, 1100, 1500, 0, 1440);

      float leftHandZ =map(leftHand.z, 5500, 7500, 1100, 1500);
      float ConleftHandZ = map(leftHandZ, 1100, 1500, 0, 1440);

      Mouse = new PVector(rightHand.x, -rightHand.y, z);
      Mouse2 = new PVector(leftHand.x, -leftHand.y, z);


      println("rightHand "+rightHand.z+"leftHand "+leftHand.z);
      //println(frameCount);

      pushMatrix();

      translate(width/2, height/2, 0);
      rotateY(k);


      for (int i = 0; i < VertCount; i++) {
        //PVector orgv = model.getVertex(i);

        locas[i]= model.getVertex(i);
        Verts[i]= Smodel.getVertex(i);


        //PVector tmpv = new PVector();


        if (frameCount> 100) {



          float randX = noise(randomGaussian());
          float randY = noise(randomGaussian());
          float randZ = noise(randomGaussian());

          PVector Ran = new PVector(randX, randY, randZ);

          //float norX = abs(cos(k)) * randX;
          //float norY = abs(cos(k)) * randY;
          //float norZ = abs(cos(k)) * randZ;

          if ((Verts[i].y > Mouse.y  - r/2 && Verts[i].y < Mouse.y  + r/2 && Verts[i].x > Mouse.x  - r/2 && Verts[i].x < Mouse.x  + r/2 && Verts[i].z > Mouse.z  - 1920/2 && Verts[i].z <  Mouse.z  + 1920/2)||(Verts[i].y > Mouse2.y  - r/2 && Verts[i].y < Mouse2.y  + r/2 && Verts[i].x > Mouse2.x  - r/2 && Verts[i].x < Mouse2.x  + r/2 && Verts[i].z > Mouse2.z  - 1920/2 && Verts[i].z <  Mouse2.z  + 1920/2)) {
            tmpmodel.setVertex(i, locas[i].x, locas[i].y, locas[i].z);
          } else {

            Verts[i].x+=Ran.x;
            Verts[i].y+=Ran.y;
            Verts[i].z+=Ran.z;

            if (Verts[i].x > width/2 ) {
              Verts[i].x=-width/2;
            } else if (Verts[i].x < -width/2) {
              Verts[i].x=width/2;
            }
            if (Verts[i].y > height/2 ) {
              Verts[i].y=-height/2;
            } else if (Verts[i].y < -height/2) {
              Verts[i].y=height/2;
            }

            if (Verts[i].z < -720 ) {
              Verts[i].z=800/2;
            } else if ( Verts[i].z > 720) {
              Verts[i].z=-800/2;
            }
            tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
          }
        }
        // output.println("Verts " + Verts[i] + " locas " +locas[i]);
      }

      pushMatrix();
      rotateY(-k); //-----------------HERE
      translate(Mouse.x, Mouse.y, Mouse.z);
      rotateY(k); //-------------AND HERE
      noFill();
      stroke(255);
      strokeWeight(3);
      box(r, r, 1920);
      popMatrix();


      pushMatrix();
      rotateY(-k); //-----------------HERE
      translate(Mouse2.x, Mouse2.y, Mouse2.z);
      rotateY(k); //-------------AND HERE
      noFill();
      stroke(255);
      strokeWeight(3);
      box(r, r, 1920);
      popMatrix();


      noStroke();

      tmpmodel.draw();

      popMatrix();



      pushMatrix();
      translate(width/2, height/2, 0);
      rotateY(k);
      noFill();
      stroke(255);
      strokeWeight(7);
      box(width, height, 1920);
      popMatrix();
      //output.flush(); // Writes the remaining data to the file
      //output.close(); // Finishes the file
      //saveFrame();
      //println(z);
    }
  }

  spout.sendTexture();
}


void onNewUser(SimpleOpenNI kinect, int userID) {


  kinect.startTrackingSkeleton(userID);
}

//startTracking is alterd
void onEndCalibration(int userId, boolean successful) {
  if (successful) {
    println(" User calibrated !!!");
    kinect.startTrackingSkeleton(userId);
  } else {
    println("  Failed to calibrate user !!!");
    kinect.startTrackingSkeleton(userId);
  }
}

//There is aproblem here,deleted the pose



void keyPressed() {
  if (keyCode == UP) {
    z++;
  }
  if (keyCode == DOWN) {
    z--;
  }
}

Second Example: Here is the same example with above,but i tried to move out the kinect calibration from the general animation of the model,which is difined by RanX,RanY,RanZ and Nor.I tried to solve the problem with a boolean.The problem is that when the Kinect calibrates the user the animation stops.

import peasy.*;
import saito.objloader.*;
import SimpleOpenNI.*;
import spout.*;

//PrintWriter output;
OBJModel model ;
OBJModel Smodel ;
OBJModel tmpmodel ;

Spout spout;

PeasyCam cam;

SimpleOpenNI kinect;


float z=0;
float easing = 0.005;
float r;
float k;
int VertCount;
PVector[] Verts;
PVector[] locas;
PVector Mouse;
PVector Mouse2;
PVector rightHand;
PVector convertedRightHand;
PVector leftHand;
PVector convertedLeftHand;
PVector rightShoulder;
PVector convertedRightShoulder;
PVector leftShoulder;
PVector convertedleftShoulder;
float rightHandZ;
float ConrightHandZ;
float leftHandZ;
float ConleftHandZ;

`boolean Kin = false;`

void setup()
{
  size(640*3, 480*3, P3D);
  frameRate(30);
  noStroke();

  kinect = new SimpleOpenNI(this);

  kinect.enableDepth();
  kinect.enableUser();

  model = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  model.enableDebug();
  model.scale(200);
  model.translateToCenter();


  Smodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  Smodel.enableDebug();
  Smodel.scale(200);
  Smodel.translateToCenter();


  tmpmodel = new OBJModel(this, "Model2.obj", "absolute", TRIANGLES);
  tmpmodel.enableDebug();
  tmpmodel.scale(200);
  tmpmodel.translateToCenter();

  //output = createWriter("positions.txt");

  cam = new PeasyCam(this, width/2, height/2, 0, 2300);


  spout = new Spout(this);

  spout.createSender("Self kinect");
}



void draw()
{
  background(0);
  lights();

  kinect.update();
  IntVector userList = new IntVector();
  kinect.getUsers(userList);

  int VertCount = model.getVertexCount ();
  Verts = new PVector[VertCount];
  locas = new PVector[VertCount];
  r =300;
  //k = k + 0.01;




  cam.setMouseControlled(false);







  if (userList.size() > 0) {

    textSize(300);
    text("Start1", width/2-150, height/2-150);
    fill(0, 102, 153);

    int userId = userList.get(0);

    if ( kinect.isTrackingSkeleton(userId)) {
      `Kin=true;`
      textSize(300);
      text("Start2", width/2-150, height/2-150);
      fill(0, 102, 153);

      rightHand = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);

      convertedRightHand = new PVector();
      kinect.convertRealWorldToProjective(rightHand, convertedRightHand);

      leftHand = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_HAND, leftHand);

      convertedLeftHand = new PVector();
      kinect.convertRealWorldToProjective(leftHand, convertedLeftHand);


      rightShoulder = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, rightShoulder);

      convertedRightShoulder = new PVector();
      kinect.convertRealWorldToProjective(rightShoulder, convertedRightShoulder);


      leftShoulder = new PVector();
      kinect.getJointPositionSkeleton(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, leftShoulder);

      convertedleftShoulder = new PVector();
      kinect.convertRealWorldToProjective(leftShoulder, convertedleftShoulder);


      //output.println(" This is the firstPose "+"rightHand "+rightHand+" leftHand "+leftHand);



      rightHandZ =  map(rightHand.z, 5500, 7500, 1100, 1500);
      ConrightHandZ = map(rightHandZ, 1100, 1500, 0, 1440);

      leftHandZ =map(leftHand.z, 5500, 7500, 1100, 1500);
      ConleftHandZ = map(leftHandZ, 1100, 1500, 0, 1440);

      Mouse = new PVector(rightHand.x, -rightHand.y, z);
      Mouse2 = new PVector(leftHand.x, -leftHand.y, z);

      pushMatrix();
      translate(width/2, height/2, 0);
      pushMatrix();
      rotateY(-k); //-----------------HERE
      translate(Mouse.x, Mouse.y, Mouse.z);
      rotateY(k); //-------------AND HERE
      noFill();
      stroke(255);
      strokeWeight(3);
      box(r, r, 1920);
      popMatrix();


      pushMatrix();
      rotateY(-k); //-----------------HERE
      translate(Mouse2.x, Mouse2.y, Mouse2.z);
      rotateY(k); //-------------AND HERE
      noFill();
      stroke(255);
      strokeWeight(3);
      box(r, r, 1920);
      popMatrix();
      popMatrix();
    }
  }


  //println("rightHand "+rightHand.z+"leftHand "+leftHand.z);
  //println(frameCount);




  pushMatrix();




  translate(width/2, height/2, 0);
  rotateY(k);


  for (int i = 0; i < VertCount; i++) {
    //PVector orgv = model.getVertex(i);

    locas[i]= model.getVertex(i);
    Verts[i]= Smodel.getVertex(i);


    //PVector tmpv = new PVector();


    if (frameCount> 10) {



      float randX = noise(randomGaussian());
      float randY = noise(randomGaussian());
      float randZ = noise(randomGaussian());

      PVector Ran = new PVector(randX, randY, randZ);

      //float norX = abs(cos(k)) * randX;
      //float norY = abs(cos(k)) * randY;
      //float norZ = abs(cos(k)) * randZ;








     ` if (Kin==true) {`
        if ((Verts[i].y > Mouse.y  - r/2 && Verts[i].y < Mouse.y  + r/2 && Verts[i].x > Mouse.x  - r/2 && Verts[i].x < Mouse.x  + r/2 && Verts[i].z > Mouse.z  - 1920/2 && Verts[i].z <  Mouse.z  + 1920/2)||(Verts[i].y > Mouse2.y  - r/2 && Verts[i].y < Mouse2.y  + r/2 && Verts[i].x > Mouse2.x  - r/2 && Verts[i].x < Mouse2.x  + r/2 && Verts[i].z > Mouse2.z  - 1920/2 && Verts[i].z <  Mouse2.z  + 1920/2)) {
          tmpmodel.setVertex(i, locas[i].x, locas[i].y, locas[i].z);
        }
     ` } else if (Kin==true||Kin==false) {   `



        Verts[i].x+=Ran.x;
        Verts[i].y+=Ran.y;
        Verts[i].z+=Ran.z;

        if (Verts[i].x > width/2 ) {
          Verts[i].x=-width/2;
        } else if (Verts[i].x < -width/2) {
          Verts[i].x=width/2;
        }
        if (Verts[i].y > height/2 ) {
          Verts[i].y=-height/2;
        } else if (Verts[i].y < -height/2) {
          Verts[i].y=height/2;
        }

        if (Verts[i].z < -720 ) {
          Verts[i].z=800/2;
        } else if ( Verts[i].z > 720) {
          Verts[i].z=-800/2;
        }
        tmpmodel.setVertex(i, Verts[i].x, Verts[i].y, Verts[i].z);
      }
    }
    // output.println("Verts " + Verts[i] + " locas " +locas[i]);
  }




  noStroke();

  tmpmodel.draw();

  popMatrix();



  pushMatrix();
  translate(width/2, height/2, 0);
  rotateY(k);
  noFill();
  stroke(255);
  strokeWeight(7);
  box(width, height, 1920);
  popMatrix();
  //output.flush(); // Writes the remaining data to the file
  //output.close(); // Finishes the file
  //saveFrame();
  //println(z);
  if (userList.size() < 0) {
    Kin=false;
  }

  spout.sendTexture();
  println(Kin);
}


void onNewUser(SimpleOpenNI kinect, int userID) {


  kinect.startTrackingSkeleton(userID);
}

//startTracking is alterd
void onEndCalibration(int userId, boolean successful) {
  if (successful) {
    println(" User calibrated !!!");
    kinect.startTrackingSkeleton(userId);
  } else {
    println("  Failed to calibrate user !!!");
    kinect.startTrackingSkeleton(userId);
  }
}

//There is aproblem here,deleted the pose



void keyPressed() {
  if (keyCode == UP) {
    z++;
  }
  if (keyCode == DOWN) {
    z--;
  }
}

Viewing all articles
Browse latest Browse all 530

Trending Articles