3D array

edited February 2016 in Programming Questions

Hello, Anyone has an idea how can we create 3D rectangle array in processing in 3D scene?

Thanks for your help

Answers

  • edited February 2016

    Hello!

    yes. But nobody will write it for you.

    Just follow these simple steps.

    remark

    Please distinguish between

    • a graphical 3D grid of rectangles that you display in 3D and

    • a 3D array as a data structure.

    A 3D grid (in the graphical sense) can easily be displayed using a 1D array, since the data structure does not determine the position of the items in 3D space. Just make a 1D array of PVector and use PVector with 3 parameters x,y,z to display the rectangle. When you for-loop over the items each item can be anywhere in space, independently of the position in the 1D array / list.

    (A 3D grid can be displayed using a 3D array of course but this is slightly more complicate. It is only useful when you want neighbours to interact (e.g. 2 adjacent red boxes explode or something) because in a 3D grid it is much easier to check the neighbours of a rectangle in the grid. This would be more difficult (but still doable) in a 1D grid. But let's stick to the 1D array to display a 3D grid.)

    How to fill the 1D array

    To fill the array with the 3D positions (in setup()) you can use 3 nested for-loops i,j,k so that you can determine the x,y,z from them :

    ....new PVector (100+i*distBetweenBoxes, 
        100+j*distBetweenBoxes, 
        100+k*distBetweenBoxes);
    
     index++; 
    

    be aware that you need a var index to use with the array, because in a nested for-loop i,j,k are not valid as an index for the 1D array.....

    nested for-loop means it's a for-loop inside a for-loop inside a for-loop

    this is because we x,y,z in the grid (i,j,k) so we have rows, columns a depth for the grid

    so the outer for-loop says: I want 10 times the inner loop

    and the middle loop says I want 10 times the most inner loop.

    So it's 10x 10x 10x rectangles.

    The outer loop is the columns, then the rows, then each time the depth.

    then display the data in draw()

    Instead of rect() you can use box() - see reference

    https://www.processing.org/reference/

    to position the box or the rect, you'll need pushMatrix, translate and popMatrix

    remark

    (interestingly, point and line are available in 3D but rect is not; since this is so you need pushMatrix, translate and popMatrix and with rect(0,0,10,10); - always use 0,0 here since translate takes care of the position in space. Or use box(10); )

    please post your code and ask concrete questions then

    Best, Chrisir ;-)

  • edited February 2016

    in the tutorials there is a tutorial about arrays and another one about Two-Dimensional Arrays.

    3D arrays are like this, but just one dimension more.

    (arrays are data structures)

    there is also a tutorial about programming in 3D named P3D

    https://processing.org/tutorials/

  • Thanks Chrisir for the informations, i will write the code and post it here, infact i want to create a piano in perspective and play it via Kinect. I already created my piano with rect and loop for, but i didn't use Array for that.

  • edited February 2016

    This is my actual code before using Array:

  • edited February 2016

    int[] blackRect = {1,1,0,1,1,1,0,1};

    color[] couleur = {color(0),color(0),0,color(0),color(0),color(0),0,color(0)};

    void setup(){

    size(1280,750, P3D);

    }

    void draw(){

    background(0);

    translate(width/2, height/2, 30);

    stroke(255);

    noFill();

    box(680, 400, 1000);

    //blackRect

    pushMatrix();

    translate(94, 100, 275);

    rotateX(PI/2);

    int largeur_black = 19;

    for (int x = 0; x < 8; x++)

    {

    fill(couleur[x]);

    noStroke();

    if(blackRect[x] == 1)

    rect(30-(2 * x * largeur_black), 80, largeur_black, 100);

    }

    popMatrix();

    //whiteRect

    pushMatrix();

    translate(103, 100, 150);

    rotateX(PI/2);

    int largeur_white = 38;

    for(int i = 0; i < 9; i++)

    { fill(128);

    stroke(0);

    rect(30-(i*largeur_white), 110, largeur_white, 186);

    }

    popMatrix();

      }    
    
  • Your use of the word "Array" is misleading. You're not looking for a data structure that uses indexes to address one of many elements. You want to draw a 3D box in a 3D space.

    Use the function box().

  • void setup(){
      size(400,400,P3D);
    }
    
    void draw(){
      background(0);
      translate(width/2,height/2);
      lights();
      rotateY(map(mouseX,0,width,-PI,PI));
      noStroke();
      fill(255,0,0);
      box(100);
      fill(0,255,0);
      box(150,20,20);
    }
    
  • Yes, i need to draw my rectangles in Array list because after i can use collision effect easier for each rects separately.

  • edited February 2016

    int[] blackRect = {1,1,0,1,1,1,0,1};

    color[] couleur = {color(0),color(0),0,color(0),color(0),color(0),0,color(0)};

    void setup(){

    size(1280,750, P3D);

    }

    void draw(){

    background(0);

    translate(width/2, height/2, 30);

    stroke(255);

    noFill();

    box(680, 400, 1000);

    //blackRect

    pushMatrix();

    translate(94, 100, 275);

    rotateX(PI/2);

    int largeur_black = 19;

    for (int x = 0; x < 8; x++)

    {

    fill(couleur[x]);

    noStroke();

    if(blackRect[x] == 1)

    rect(30-(2 * x * largeur_black), 80, largeur_black, 100);

    }

    popMatrix();

    //whiteRect

    pushMatrix();

    translate(103, 100, 150);

    rotateX(PI/2);

    int largeur_white = 38;

    for(int i = 0; i < 9; i++)

    { fill(128);

    stroke(0);

    rect(30-(i*largeur_white), 110, largeur_white, 186);

    }

    popMatrix();

    }

  • int[] blackRect = {1, 1, 0, 1, 1, 1, 0, 1};
    String[] blackName = {"ais", "cis", "dis", "eis", "fis", "gis", "b", "cis"};
    String[] blackNote = {"ais", "cis", "dis", "eis", "fis", "gis", "b", "cis"};
    
    String[] whiteName = {"a", "c", "d", "e", "f", "g", "h", "c", "d", "e"};
    
    // this doesn't make much sense: 0 = ? But color(0) is ok.
    color[] couleur = {color(0), color(0), 0, color(0), color(0), color(0), 0, color(0)};
    
    ArrayList<Key> keys = new ArrayList();
    
    
    PGraphics internal;
    boolean showImage=false; 
    
    void setup() {
    
      size(1280, 750, P3D);
    
      print("patience.");
      print("."); 
      print(".");
    
      internal = createGraphics(1280, 750, P3D);
      print(".");
      internal.noSmooth(); 
      internal.beginDraw();
      print(".");
      internal.background(201);
      print(".");  
      internal.translate(width/2, height/2, 30);
    
      // defining all keys  
    
    
      //black Rect
      int largeur_black = 19; // width 
      for (int x = 0; x < 8; x++) {
        // fill(couleur[x]);
        // -1 is for noStroke();
        if (blackRect[x] == 1)
          keys.add ( new Key (30-(2 * x * largeur_black), 110, largeur_black, 140, 
            blackName[x], blackNote[x]+"-Note", 
            couleur[x], -1, 
            new PVector(94, 99, 151) ));
        keys.get(keys.size()-1).display_internal(  color(255, 0, 10*x)  );
        print(".");
      }//for 
    
      // white Rect
      int largeur_white = 38;// width 
      for (int i = 0; i < 9; i++) { 
        // fill(128);
        // stroke(0);
        keys.add ( new Key (30-(i*largeur_white), 110, largeur_white, 186, 
          whiteName[i], "C-Note", 
          color(255), color(0), 
          new PVector(103, 100, 150)));
        keys.get(keys.size()-1).display_internal(  color(255, 255, 10*i)  );
        print(".");
      }//for
    
      internal.endDraw();
    
      println(".");
    }// func 
    
    void mousePressed() {
      // all keys
      for (Key k : keys) {
        if (internal.get(mouseX, mouseY) == k.colKey) {
          // 
          println(k.name
            +" -> "
            +k.note);
        }//if
      }//for
    }//func 
    
    void draw() {
      background(0);
    
      if (showImage) 
      {     
        image(internal, 0, 0);
      } else
      {
        translate(width/2, height/2, 30);
    
        // box
        stroke(111);
        noFill();
        box(680, 400, 1000);
    
        // all keys
        for (Key k : keys) {
          k.display();
        }
      }
    }  
    
    void keyPressed() {
      // toggle 
      showImage=!showImage;
    }
    
    // ===========================================
    // the class - a blueprint for one key 
    
    class Key {
    
      float x;
      float y; 
    
      float w; 
      float h; 
    
      String name;
      String note; 
    
      color colFill;
      color colStroke; 
      color colKey; 
    
      PVector translatePVector;
    
      //constr
      Key(float x_, float y_, 
        float w_, float h_, 
        String name_, 
        String note_, 
        color colFill_, 
        color colStroke_, 
        PVector translatePVector_) {
        //
        x=x_;
        y=y_;
        w=w_;
        h=h_;
    
        name=name_;
        note=note_;
    
        colFill = colFill_;
        colStroke = colStroke_; 
    
        translatePVector = translatePVector_;
      }//constr
    
      void display() {
        pushMatrix();
        translate(translatePVector.x, 
          translatePVector.y, 
          translatePVector.z); 
        rotateX(PI/2);
        fill(colFill);
        if (colStroke==-1) 
          noStroke(); 
        else 
        stroke(colStroke);
        rect(x, y, w, h);
    
        // doesn't work :  
        //translate(x-w/2, 0);
        //box(w, h, 3);
    
        popMatrix();
      }//method
    
      void display_internal( color colKey_ ) {
    
        colKey=colKey_;
    
        internal.pushMatrix();
        internal.translate(translatePVector.x, 
          translatePVector.y, 
          translatePVector.z); 
        internal.rotateX(PI/2);
        internal.fill(colKey);
        internal.noStroke(); 
        internal.stroke(0);
        internal.rect(x, y, w, h);
        internal.popMatrix();
      }//method
      //
    }
    // 
    
  • Answer ✓

    you can "play" with the mouse - it recognizes the key you pressed

    needs a long time at start-up though

  • Wow, it's incredible, thanks for the code Chrisir. Instead of mousePressed i will use my Kinect value, because my character in the screen will play the piano by collision effect.

  • yes.

    it's working with an invisible PGraphics (visible when pressing space bar) that holds unique colors for each key

  • why needs a long time at start-up though ?

    ask one of the gurus.....

    or a bug

  • Now i integrated my character in it and even it take more time to start-up !

  • edited February 2016

    I think maybe is because of the size of my sketch.

  • there are other methods and a lib for 3D picking instead of using PGraphics which seems to be very expensive on the cpu or whatever

    or it's a bug

    try assigning more memory to the sketch in the preferences first of course (at least 5 mb or so)

    picking is the collision of rect with mouse

    it is hard to calculate since most keys are diagonal

    idea: you could cheat and just save the invisible image (hit space bar first and then save - you need to program this when save) to file A

    then save sketch as... with a new name / new version

    then strip the entire PGraphic and just load file A in setup() and use get(mouseX, mouseY) on this

    should be much faster

  • I'll try it, just there is a bit worry about the collision between my character and the keys. Because my character is not above the keys and that makes problem for the collision.

  • edited February 2016

    The actual result at the moment:

    Screen Shot 2016-02-03 at 5.38.59 PM

  • that's very bad.

    0.

    you could connect the dots with joints (arms, body, neck...)

    2.

    then you could rotate it and make it bigger (scale)

    until he's sitting on / in front of the piano

    OR

    2.

    you rotate and scale and translate the piano until it is right under the hands of the body.

    actually the width of the piano must fit the guys hands when he plays the highest and the lowest note on the piano (the finger should hit the right key on the piano)

    3.

    of course you could "cheat"

    I mean you get data and you analyze them which note / key has to be played. Play the note.

    then you move the finger to the key position you want it to be (and not as you have it now probably to the exact coordinates kinect gives you).

    that's cheating because in a way naturally, the character should hit the key automatically. Now you split up your kinect input to generate the tone / note and the display of the character. Maybe this helps.

    I mean, the head and feed still can come from the kinect and the fingers you cheat.

    so you can make it look like it did it right. Maybe.

    Best, Chrisir ;-)

  • edited February 2016

    Well, i want to play it with the foots, but the character is too big, i already resize the sketch from 640,400 to 1280,750, because i wanted to be in big sketch and when i did that my character resized too :/ but the character is not on top...

  • you can make the character smaller with scale.... and leave the rest at it is

    pushMatrix();
    scale(.58); 
    character();
    popMatrix(); 
    

    also use translate to bring him into position

  • I don't think it is possible with scale, because i resized in joint position too, then it's something global.

  • edited February 2016
    import SimpleOpenNI.*;
    
    int[] blackRect = {1, 1, 0, 1, 1, 1, 0, 1};
    
    String[] blackName = {"ais", "cis", "dis", "eis", "fis", "gis", "tis", "lis"};
    
    String[] blackNote = {"ais", "cis", "dis", "eis", "fis", "gis", "tis", "lis"};
    
    String[] whiteName = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
    
    String[] whiteNote = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}; 
    
    
    color[] couleur = {color(0), color(0), 0, color(0), color(0), color(0), 0, color(0)};
    
    ArrayList<Key> keys = new ArrayList();
    
    PGraphics internal;
    boolean showImage=false; 
    
    SimpleOpenNI  myKinect;
    
     PrintWriter output;
    
    //... declaraciones de los vectores ...
    
        PVector rtKnee, leftKnee, rtShoulder, leftShoulder, rtElbow, leftElbow, rtHand, 
    leftHand, rtFoot, leftFoot;
        PVector rtHip, leftHip;
    
    float x_coef;
    float y_coef;
    
    
    void setup() {
    
      myKinect = new SimpleOpenNI(this);
    
          // enable depthMap generation
          if(myKinect.enableDepth() == false)
          {
             println("Can't open the depthMap, maybe the camera is not connected!");
             exit();
             return;
          }
    
          // enable skeleton generation for all joints
          //myKinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
          myKinect.enableUser();
    
          myKinect.setMirror(false);
    
          //Enable the Depth Camera
    
          myKinect.enableDepth();
          int size_x = 1280;
          int size_y = 750;
    
          x_coef = (float) size_x / (float) myKinect.depthWidth();
          y_coef = (float) size_y / (float) myKinect.depthHeight();
    
    
    
    
          smooth();
          size(size_x, size_y, P3D);
    
      print("patience.");
      print("."); 
      print(".");
    
      internal = createGraphics(1280, 750, P3D);
    
      print(".");
    
      internal.noSmooth(); 
      internal.beginDraw();
    
      print(".");
      internal.background(201);
      print(".");  
    
      internal.translate(width/2, height/2, 30);
    
      // defining all keys  
    
    
      //black Rect
      int largeur_black = 19; // width 
    
      for (int x = 0; x < 8; x++) {
    
        // fill(couleur[x]);
        // -1 is for noStroke();
    
        if (blackRect[x] == 1)
          keys.add ( new Key (30-(2 * x * largeur_black), 110, largeur_black, 140, 
            blackName[x], blackNote[x]+"-Note", 
            couleur[x], -1, 
            new PVector(94, 99, 255) ));
        keys.get(keys.size()-1).display_internal(  color(255, 0, 10*x)  );
    
        print(".");
      }//for 
    
      // white Rect
      int largeur_white = 38;// width 
      for (int i = 0; i < 9; i++) { 
        keys.add ( new Key (30-(i*largeur_white), 110, largeur_white, 186, 
          whiteName[i], whiteNote[i]+"-Note", 
          color(128), color(0), 
          new PVector(103, 100, 150)));
        keys.get(keys.size()-1).display_internal(  color(255, 255, 10*i)  );
        print(".");
      }//for
    
      internal.endDraw();
      println(".");
    
    }// func 
    
    void mousePressed() {   // Cette partie ça doit être par la Kinect
      // all keys
      for (Key k : keys) {
    
        if (internal.get(mouseX, mouseY) == k.colKey) {
          // 
          fill(255);
    
          println(k.name
            +" -> "
            +k.note);
    
        }//if
      }//for
    }//func 
    
    void draw() {
    
      background(0);
    
     //Get new data from the Kinect
          myKinect.update();
    
          //Draw camera image on the screenn
        //image(myKinect.rgbImage(),0,0);
           //image(myKinect.depthImage(),0,0);
    
          //Each new person is identified with a number, so draw up to 5 people
          //for(int userId=1; userId<=5; userId++){
    
            for (int userId=1; userId<=1;userId++){
    
              //Check to see if tracking
    
              if(myKinect.isTrackingSkeleton(userId)){
    
                //stroke(255,0,0);
                //strokeWeight(3);
                //drawSkeleton(userId);
    
                getBodyPoints(userId);
    
                //There are 24 possible joints that openNI tracks.  If we can get the point, draw it.    
                for(int bodyPart=1; bodyPart<=24; bodyPart++){
    
                    //get the point as a vector
                    PVector bodyPoint = getBodyPoint(userId, bodyPart);
    
                     //System.out.println("Body point: " + bodyPoint);
    
                    fill(255);
    
                    ellipse(bodyPoint.x, bodyPoint.y, 20, 20);
                 }
    
                 //draw the head bigger -- Demonstrates use of Constant SKEL_HEAD
                 PVector headPoint = getBodyPoint(userId, SimpleOpenNI.SKEL_HEAD);
    
                ellipse(headPoint.x, headPoint.y, 50,50);
    
                 PVector rtHand2D = new PVector(rtHand.x, rtHand.y);
    
                 PVector rtElbow2D = new PVector(rtElbow.x, rtElbow.y);
    
                 PVector rtShoulder2D = new PVector(rtShoulder.x, rtShoulder.y); 
    
                 PVector rtHip2D = new PVector(rtHip.x, rtHip.y);
    
    
                 PVector torsoOrientation = PVector.sub(rtShoulder2D, rtHip2D);
    
                 PVector upperArmOrientation = PVector.sub(rtElbow2D, rtShoulder2D);
    
                 // calcula los ejes contra los cuales queremos medir nuestros ángulos
                 float shoulderAngle = angleOf(rtElbow2D, rtShoulder2D, 
    
    torsoOrientation);
                 float elbowAngle = angleOf(rtHand2D, rtElbow2D, upperArmOrientation);
    
                 PVector ltHand2D = new PVector(leftHand.x, leftHand.y);
    
                 PVector ltElbow2D = new PVector(rtElbow.x, leftElbow.y);
    
                 PVector ltShoulder2D = new PVector(rtShoulder.x, leftShoulder.y); 
    
                 PVector ltHip2D = new PVector(leftHip.x, leftHip.y);
    
    
                 PVector lttorsoOrientation = PVector.sub(ltShoulder2D, ltHip2D);
    
                 float ltshoulderAngle = angleOf(ltElbow2D, ltShoulder2D, lttorsoOrientation);
                 float ltelbowAngle = angleOf(rtHand2D, rtElbow2D, upperArmOrientation);
    
                 rtShoulder.normalize();
    
                 rtElbow.normalize();
    
                 rtHand.normalize();
    
                 leftShoulder.normalize();
    
                 leftElbow.normalize();
    
                 leftHand.normalize();
    
    
    
                 println(
                         "Right Foot: " + (rtFoot.x) + "," + (rtFoot.y) + "," + (rtFoot.z) + "\n" +
                         "left Foot: " + (leftFoot.x) + "," + (leftFoot.y) + "," + (leftFoot.z) + "\n" +
                         "Right hand: " + (rtHand.x) + "," + (rtHand.y) + "," + (rtHand.z) + "\n" +
                         "Letf hand: " + (leftHand.x) + "," + (leftHand.y) + "," + (leftHand.z));
    
    
              }
              else
              {
                //print("Skeleton is not being tracked\n");
              }
    
      if (showImage) 
      {     
        image(internal, 0, 0);
      } else
      {
        translate(width/2, height/2, 30);
    
        // box
        stroke(111);
        noFill();
        box(680, 400, 1000);
    
        // all keys
        for (Key k : keys) {
          k.display();
        }
      }
    }  
    
    
    //println(mouseX,mouseY);
          }
     void keyPressed() {
      // toggle 
      showImage=!showImage;
    }
    
    // ===========================================
    // the class - a blueprint for one key 
    
    class Key {
    
      float x;
    
      float y; 
    
      float w; 
    
      float h; 
    
      String name;
    
      String note; 
    
      color colFill;
    
      color colStroke; 
    
      color colKey; 
    
      PVector translatePVector;
    
      //constr
    
      Key(float x_, float y_, 
        float w_, float h_, 
        String name_, 
        String note_, 
        color colFill_, 
        color colStroke_, 
        PVector translatePVector_) {
    
        //
    
        x=x_;
    
        y=y_;
    
        w=w_;
    
        h=h_;
    
        name=name_;
    
        note=note_;
    
        colFill = colFill_;
    
        colStroke = colStroke_; 
    
        translatePVector = translatePVector_;
      }
    
    //constr
    
      void display() {
    
        pushMatrix();
    
        translate(translatePVector.x, 
    
          translatePVector.y, 
    
          translatePVector.z); 
    
        rotateX(PI/2);
    
        fill(colFill);
    
        if (colStroke==-1) 
    
          noStroke(); 
        else
        stroke(colStroke);
        rect(x, y, w, h);
    
        // doesn't work :  
        //translate(x-w/2, 0);
        //box(w, h, 3);
    
        popMatrix();
    
      }//method
    
      void display_internal( color colKey_ ) {
    
        colKey=colKey_;
    
        internal.pushMatrix();
    
        internal.translate(translatePVector.x, 
    
          translatePVector.y, 
    
          translatePVector.z); 
    
        internal.rotateX(PI/2);
    
        internal.fill(colKey);
    
        internal.noStroke(); 
    
        internal.stroke(0);
    
        internal.rect(x, y, w, h);
    
        internal.popMatrix();
    
      }
    
    //method
      //
    }
    
    // n
    
      PVector getBodyPoint(int user, int bodyPart) {
    
          PVector jointPos=new PVector(), jointPos_Proj=new PVector();
    
          myKinect.getJointPositionSkeleton(user, bodyPart, jointPos);
    
          myKinect.convertRealWorldToProjective(jointPos, jointPos_Proj);
    
          //System.out.println("Parte del cuerpo: " + bodyPart + " Posición " + jointPos);
    
          jointPos_Proj.x *= x_coef;
    
          jointPos_Proj.y *= y_coef;
    
          return jointPos_Proj;
    
        }
    
    
        void getBodyPoints(int userId) {
    
          rtKnee = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_KNEE);
    
          leftKnee = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_KNEE);
    
          rtShoulder = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
    
          leftShoulder = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER);
    
          rtElbow = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
    
          leftElbow = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
    
          rtHand = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_HAND);
    
          leftHand = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_HAND);
    
          rtFoot = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_FOOT);
    
          leftFoot = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_FOOT);
    
          rtHip = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_HIP);
    
          leftHip = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
        }
        ///////////////////
    
    
        // draw the skeleton with the selected joints
        //Simple OpenNI has a method called drawLimb, which simply draws a line
        // between two body points
        /*
        void drawSkeleton(int userId)
        {
    
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
      myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT); 
    
    
        }
     */
        // -----------------------------------------------------------------
        // SimpleOpenNI has a number of event handlers, that are triggered when
        // "User events" occur.
    
        void onNewUser(SimpleOpenNI curContext, int userId)
        {
          println("onNewUser - userId: " + userId);
    
          println("  start pose detection");
    
          myKinect.startTrackingSkeleton(userId);
        }
    
        void onLostUser(int userId)
        {
          println("onLostUser - userId: " + userId);
        }
    
    
        float angleOf(PVector one, PVector two, PVector axis) {
    
          PVector limb = PVector.sub(two, one);
    
          return degrees(PVector.angleBetween(limb, axis));
        }
    
  • Answer ✓

    Edit post. Select code. Press Ctrl + o.

  • (You might have to split the code into two posts if it's too long)

  • edited February 2016

    The problem solved, i share my code with you, but the character can play a whiteRect not the blackOne yet, this will solve later ;)

  • edited February 2016
        import SimpleOpenNI.*;
    
        int[] blackRect = {
          1, 1, 0, 1, 1, 1, 0, 1
        };
        String[] blackName = {
          "ais", "cis", "dis", "eis", "fis", "gis", "tis", "lis"
        };
        String[] blackNote = {
          "ais", "cis", "dis", "eis", "fis", "gis", "tis", "lis"
        };
    
        String[] whiteName = {
          "a", "b", "c", "d", "e", "f", "g", "h", "i"
        };
        String[] whiteNote = {
          "a", "b", "c", "d", "e", "f", "g", "h", "i"
        };
    
    
        color[] couleur = {
          color(0), color(0), 0, color(0), color(0), color(0), 0, color(0)
        };
    
        ArrayList<Key> keys = new ArrayList();
    
    
        PGraphics internal;
        boolean showImage=false; 
    
    
    
        SimpleOpenNI  myKinect;
    
        PrintWriter output;
    
        //... declaraciones de los vectores ...
        PVector rtKnee, leftKnee, rtShoulder, leftShoulder, rtElbow, leftElbow, rtHand, leftHand, rtFoot, leftFoot;
        PVector rtHip, leftHip;
    
        int kinect_size_x = 640;
        int kinect_size_y = 480;
        int wanted_size_x = 1280;
        int wanted_size_y = 750;
        float x_coef;
        float y_coef;
    
        void setup() {
    
          myKinect = new SimpleOpenNI(this);
    
          // enable depthMap generation
          if (myKinect.enableDepth() == false)
          {
            println("Can't open the depthMap, maybe the camera is not connected!");
            exit();
            return;
          }
    
          // enable skeleton generation for all joints
          //myKinect.enableUser(SimpleOpenNI.SKEL_PROFILE_ALL);
          myKinect.enableUser();
    
          myKinect.setMirror(false);
    
          //Enable the Depth Camera
    
          myKinect.enableDepth();
    
    
    
    
          x_coef = (float) wanted_size_x / (float) myKinect.depthWidth();
          y_coef = (float) wanted_size_y / (float) myKinect.depthHeight();
    
    
    
    
    
    
          smooth();
          size(wanted_size_x, wanted_size_y, P3D);
    
          print("patience.");
          print("."); 
          print(".");
    
          internal = createGraphics(1280, 750, P3D);
          print(".");
          internal.noSmooth(); 
          internal.beginDraw();
          print(".");
          internal.background(201);
          print(".");  
          internal.translate(width/2, height/2, 30);
    
          // defining all keys  
    
    
          //black Rect
          int largeur_black = 19; // width 
          for (int x = 0; x < 8; x++) {
            // fill(couleur[x]);
            // -1 is for noStroke();
            if (blackRect[x] == 1)
              keys.add ( new Key (30-(2 * x * largeur_black), 110, largeur_black, 180, 
              blackName[x], blackNote[x]+"-Note", 
              couleur[x], -1, 
              new PVector(94, 99, 255) ));
            keys.get(keys.size()-1).display_internal(  color(255, 0, 10*x)  );
            print(".");
          }//for 
    
          // white Rect
          int largeur_white = 38;// width 
          for (int i = 0; i < 9; i++) { 
            // fill(128);
            // stroke(0);
            keys.add ( new Key (30-(i*largeur_white), 110, largeur_white, 220, 
            whiteName[i], whiteNote[i]+"-Note", 
            color(128), color(0), 
            new PVector(103, 100, 180)));
            keys.get(keys.size()-1).display_internal(  color(255, 255, 10*i)  );
            print(".");
          }//for
    
          internal.endDraw();
    
          println(".");
        }// func 
    
        void mousePressed() {   // Cette partie ça va être par la Kinect
          // all keys
          for (Key k : keys) {
            if (internal.get(mouseX, mouseY) == k.colKey) {
              // 
              fill(255);
              println(k.name
                +" -> "
                +k.note);
            }//if
          }//for
        }//func 
    
        void draw() {
          background(0);
    
          //Get new data from the Kinect
    
          myKinect.update();
    
          //Draw camera image on the screenn
          //image(myKinect.rgbImage(),0,0);
          //image(myKinect.depthImage(),0,0);
    
          //Each new person is identified with a number, so draw up to 5 people
          //for(int userId=1; userId<=5; userId++){
    
          for (int userId=1; userId<=1; userId++) {
            //Check to see if tracking
    
            if (myKinect.isTrackingSkeleton(userId)) {
              //stroke(255,0,0);
              //strokeWeight(3);
              //drawSkeleton(userId);
    
              getBodyPoints(userId);
    
              //There are 24 possible joints that openNI tracks.  If we can get the point, draw it.    
              for (int bodyPart=1; bodyPart<=24; bodyPart++) {
    
                //get the point as a vector
                PVector bodyPoint = getBodyPoint(userId, bodyPart);
                //System.out.println("Body point: " + bodyPoint);
    
                fill(255);
                ellipse(bodyPoint.x, bodyPoint.y, 20, 20);
              }
    
              //draw the head bigger -- Demonstrates use of Constant SKEL_HEAD
              PVector headPoint = getBodyPoint(userId, SimpleOpenNI.SKEL_HEAD);
    
              ellipse(headPoint.x, headPoint.y, 50, 50);
    
              PVector rtHand2D = new PVector(rtHand.x, rtHand.y);
              PVector rtElbow2D = new PVector(rtElbow.x, rtElbow.y);
              PVector rtShoulder2D = new PVector(rtShoulder.x, rtShoulder.y); 
    
              PVector rtHip2D = new PVector(rtHip.x, rtHip.y);
    
    
              PVector torsoOrientation = PVector.sub(rtShoulder2D, rtHip2D);
    
              PVector upperArmOrientation = PVector.sub(rtElbow2D, rtShoulder2D);
    
              // calcula los ejes contra los cuales queremos medir nuestros ángulos
              float shoulderAngle = angleOf(rtElbow2D, rtShoulder2D, torsoOrientation);
              float elbowAngle = angleOf(rtHand2D, rtElbow2D, upperArmOrientation);
    
    
    
              PVector ltHand2D = new PVector(leftHand.x, leftHand.y);
              PVector ltElbow2D = new PVector(rtElbow.x, leftElbow.y);
              PVector ltShoulder2D = new PVector(rtShoulder.x, leftShoulder.y); 
    
              PVector ltHip2D = new PVector(leftHip.x, leftHip.y);
    
    
              PVector lttorsoOrientation = PVector.sub(ltShoulder2D, ltHip2D);
              float ltshoulderAngle = angleOf(ltElbow2D, ltShoulder2D, lttorsoOrientation);
              float ltelbowAngle = angleOf(rtHand2D, rtElbow2D, upperArmOrientation);
    
              rtShoulder.normalize();
              rtElbow.normalize();
              rtHand.normalize();
    
              leftShoulder.normalize();
              leftElbow.normalize();
              leftHand.normalize();
    
    
    
              println(
              "Right Foot: " + (rtFoot.x) + "," + (rtFoot.y) + "," + (rtFoot.z) + "\n"
                + "left Foot: " + (leftFoot.x) + "," + (leftFoot.y) + "," + (leftFoot.z) /* + "\n" */
                //+ "Right hand: " + (rtHand.x) + "," + (rtHand.y) + "," + (rtHand.z) + "\n"
              //+ "Letf hand: " + (leftHand.x) + "," + (leftHand.y) + "," + (leftHand.z)
              );
            } else
            {
              //print("Skeleton is not being tracked\n");
            }
    
            if (showImage) 
            {     
              image(internal, 0, 0);
            } else
            {
              translate(width/2, height/2, 30);
    
              // box
              stroke(111);
              noFill();
              box(680, 400, 1000);
    
              // all keys
              for (Key k : keys) {
                k.display();
              }
            }
          }
    
          FootDetection();
          println("\n");
          //println(mouseX,mouseY);
        }
    
        void keyPressed() {
          // toggle 
          showImage=!showImage;
        }
    
        void FootDetection() {
          if(rtFoot == null || leftFoot == null)
            return;
    
          if (rtFoot.z >0 && rtFoot.z <2000) {
            if (rtFoot.y >570 && rtFoot.y <620) {
              //playing whiteRect - Right Foot
              if (rtFoot.x >0 && rtFoot.x <500) {
                // jouer la note B1
    
                println("Note B1 (RF)");
              } else if (rtFoot.x >500 && rtFoot.x <600) {
                // jouer la note B2
                println("Note B2 (RF)");
              } else if (rtFoot.x >600 && rtFoot.x <700) {
                // jouer la note B3
                println("Note B3 (RF)");
              } else if (rtFoot.x >700 && rtFoot.x <800) {
                // jouer la note B4
                println("Note B4 (RF)");
              } else if (rtFoot.x >800 && rtFoot.x <900) {
                // jouer la note B5
                println("Note B5 (RF)");
              } else if (rtFoot.x >900 && rtFoot.x <1000) {
                // jouer la note B6
                println("Note B6 (RF)");
              } else if (rtFoot.x >1000 && rtFoot.x <1100) {
                // jouer la note B7
                println("Note B7 (RF)");
              } else if (rtFoot.x >1100 && rtFoot.x <1200) {
                // jouer la note B8
                println("Note B8 (RF)");
              } else if (rtFoot.x >1200 && rtFoot.x <1280) {
                // jouer la note B9
                println("Note B9 (RF)");
              }
    
            }
          }
    
          if (leftFoot.z >0 && leftFoot.z <2000) {
            if (leftFoot.y >570 && leftFoot.y <620) {
              //playing whiteRect - Left Foot
              if (leftFoot.x >0 && leftFoot.x < 397) {
                // jouer la note B1
                println("Note B1 (LF)");
              } else if (leftFoot.x >397 && leftFoot.x < 466) {
                // jouer la note B2
                println("Note B2 (LF)");
              } else if (leftFoot.x >466 && leftFoot.x < 536) {
                // jouer la note B3
                println("Note B3 (LF)");
              } else if (leftFoot.x >536 && leftFoot.x < 606) {
                // jouer la note B4
                println("Note B4 (LF)");
              } else if (leftFoot.x >606 && leftFoot.x < 674) {
                // jouer la note B5
                println("Note B5 (LF)");
              } else if (leftFoot.x >674 && leftFoot.x < 742) {
                // jouer la note B6
                println("Note B6 (LF)");
              } else if (leftFoot.x >742 && leftFoot.x < 812) {
                // jouer la note B7
                println("Note B7 (LF)");
              } else if (leftFoot.x >812 && leftFoot.x < 882) {
                // jouer la note B8
                println("Note B8 (LF)");
              } else if (leftFoot.x >882 && leftFoot.x < 948) {
                // jouer la note B9
                println("Note B9 (LF)");
              }
            }
          }
        }
    
        PVector getBodyPoint(int user, int bodyPart) {
    
          PVector jointPos=new PVector(), jointPos_Proj=new PVector();
          myKinect.getJointPositionSkeleton(user, bodyPart, jointPos);
          myKinect.convertRealWorldToProjective(jointPos, jointPos_Proj);
          //System.out.println("Parte del cuerpo: " + bodyPart + " Posición " + jointPos);
          //jointPos_Proj.x *= x_coef;
          //jointPos_Proj.y *= y_coef;
          jointPos_Proj.x = (jointPos_Proj.x* 1.1) + ((wanted_size_x-kinect_size_x)/2 / 1.1);
          jointPos_Proj.y += (wanted_size_y-kinect_size_y)/2 - 100;
    
          return jointPos_Proj;
        }
    
    
    
        void getBodyPoints(int userId) {
    
    
          rtKnee = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_KNEE);
    
          leftKnee = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_KNEE);
    
          rtShoulder = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
    
          leftShoulder = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER);
    
          rtElbow = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
    
          leftElbow = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_ELBOW);
    
          rtHand = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_HAND);
    
          leftHand = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_HAND);
    
          rtFoot = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_FOOT);
    
          leftFoot = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_FOOT);
    
          rtHip = getBodyPoint(userId, SimpleOpenNI.SKEL_RIGHT_HIP);
    
          leftHip = getBodyPoint(userId, SimpleOpenNI.SKEL_LEFT_HIP);
        }
        ///////////////////
    
    
        // draw the skeleton with the selected joints
        //Simple OpenNI has a method called drawLimb, which simply draws a line
        // between two body points
    
        void drawSkeleton(int userId)
        {
    
    
    
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);
    
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
          myKinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
        }
    
    
    
        // -----------------------------------------------------------------
        // SimpleOpenNI has a number of event handlers, that are triggered when
        // "User events" occur.
    
        void onNewUser(SimpleOpenNI curContext, int userId)
        {
    
          println("onNewUser - userId: " + userId);
          println("  start pose detection");
    
          myKinect.startTrackingSkeleton(userId);
        }
    
        void onLostUser(int userId)
        {
          println("onLostUser - userId: " + userId);
        }
    
    
        float angleOf(PVector one, PVector two, PVector axis) {
          PVector limb = PVector.sub(two, one);
          return degrees(PVector.angleBetween(limb, axis));
        }
    
        // ===========================================
        // the class - a blueprint for one key 
    
        class Key {
    
          float x;
          float y; 
    
          float w; 
          float h; 
    
          String name;
          String note; 
    
          color colFill;
          color colStroke; 
          color colKey; 
    
          PVector translatePVector;
    
          //constr
          Key(float x_, float y_, 
            float w_, float h_, 
            String name_, 
            String note_, 
            color colFill_, 
            color colStroke_, 
            PVector translatePVector_) {
            //
            x=x_;
            y=y_;
            w=w_;
            h=h_;
    
            name=name_;
            note=note_;
    
            colFill = colFill_;
            colStroke = colStroke_; 
    
            translatePVector = translatePVector_;
          }//constr
    
          void display() {
            pushMatrix();
            translate(translatePVector.x, 
              translatePVector.y, 
              translatePVector.z); 
            rotateX(PI/2);
            fill(colFill);
            if (colStroke==-1) 
              noStroke(); 
            else
            stroke(colStroke);
            rect(x, y, w, h);
    
    
    
            popMatrix();
          }
    
          void display_internal( color colKey_ ) {
    
            colKey=colKey_;
    
            internal.pushMatrix();
            internal.translate(translatePVector.x, 
              translatePVector.y, 
              translatePVector.z); 
            internal.rotateX(PI/2);
            internal.fill(colKey);
            internal.noStroke(); 
            internal.stroke(0);
            internal.rect(x, y, w, h);
            internal.popMatrix();
          }//method
          //
        }
        // n
    
Sign In or Register to comment.