How to set collision detection for multiple rects

edited December 2015 in Kinect

Currently I am making a game using a kinect, which I needs help very urgently.

To explain this game, Users stand in front of the kinect in order to calibrate your body into the skeleton form. Once the body is detected, left, right hand and foot will have floating circles each, which then will be used to place in the boxes displayed on both left and right side. (REFER TO IMAGES ATTACHED) You can use any left, right hand or foot to place any circles into all displayed colored blocks, which then will bring the game to the next round.

So far, I have successfully tested out the collision detection for the top left box by placing println function of("HAHAHAHA..") and it works great. Thinking the rest of 5 boxes will be straight forward to set collision detection on the rest, I've tried 3 boxes on the left side (box1,box2,box3).

I found out that only the first box detects collision and the second and third won't detect them. (done println functions for them as well but it won't trigger when I run the sketch and test it out.

***********I need to collision detect all 6 boxes, not just one box.*******

So far, I have collision detections only under void drawLHand(int userID). So you can just skip to there.

I WOULD REALLY APPRECIATE IF ANYONE HAS A SOLUTION TO THIS ISSUE OR POINT OUT WHAT I DID WRONG!

HERE IS THE CODE:

// User Tracking - show skeleton
import SimpleOpenNI.*;
SimpleOpenNI kinect;
PFont font;
String time = "60";
int t;
int interval = 60;
int stage = 1;

void setup() {
  size(640, 480);
  kinect = new SimpleOpenNI(this);
  kinect.enableDepth();
  // turn on user tracking
  kinect.enableUser();
  font = createFont("Arial", 30);
  PImage depth = kinect.depthImage();
  fill(0);
}

void draw() {
  kinect.update();
  PImage depth = kinect.depthImage();
  image(depth, 0, 0);
  // make a vector of ints to store the list of users
  IntVector userList = new IntVector();
  // write the list of detected users into our vector
  kinect.getUsers(userList);
  // if we found any users
  if (userList.size() > 0) {
    // get the first user
    int userId = userList.get(0);
    // if we’re successfully calibrated
    if (stage == 1) {
      if (kinect.isTrackingSkeleton(userId) == false){
      text("ARE YOU FLEXIBLE ENOUGH?!", width/2 - 100, height/2 - 200);
      text("please calibrate to start the game!", width/2 - 100, height/2);
      }
    }
    if ( kinect.isTrackingSkeleton(userId) == true && stage == 1){
      stage = 2;
    }

    if(stage == 2){
    t = interval-int(millis()/1000);
    time = nf(t , 3);
    fill(255);
    if(t <= 10){
      fill(255,0,0);
    }
    if(t == 0){
      text("GAMEOVER", width/2 - 100, height/2 - 150);
      println("GAME OVER");
      noLoop();
    interval+=60;}
    text(time, width/2, height/2 - 130);

    }

    float dL = 100;
  float dR = 100;

  // check if the skeleton is being tracked
  if (kinect.isTrackingSkeleton(1))
  {   
    drawLHand(1);
    drawRHand(1);
    drawRFoot(1);
    drawLFoot(1);

    // get the distance between joints
    PVector pL = new PVector(-500, 0, 1000);
    PVector pR = new PVector(500, 0, 1000); 

    float handDistanceL = getJointDistance(1, SimpleOpenNI.SKEL_LEFT_HAND, pL);
    float handDistanceR = getJointDistance(1, SimpleOpenNI.SKEL_LEFT_HAND, pR);

    dL = map(handDistanceL, 0, 2000, 0, height);
    dR = map(handDistanceR, 0, 2000, 0, height);
  }

  println(dL + ", " + dR);
    int round = 0;
    int score = 0;
    PImage button1,button1p,button2,button2p,button3,button3p,button4,button4p;
    button1 = loadImage("button1.jpg");
    button1p = loadImage("button1p.jpg");
    button2 = loadImage("button2.jpg");
    button2p = loadImage("button2p.jpg");
    button3 = loadImage("button3.jpg");
    button3p = loadImage("button3p.jpg");
    button4 = loadImage("button4.jpg");
    button4p = loadImage("button4p.jpg");
    if (kinect.isTrackingSkeleton(1) == true){
    round = 1;
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    score += 0;
    text("score:  " + score, 30, 10);
    text("round:  " + round, 30, 40);
    }

    if(round == 2){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4p,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    score += 20;
    }
    if(round == 3){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3p,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    score += 40;
    }
    if(round == 4){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4p,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4p,width/2 + 220, height/2 +76);
    score += 40;
    }
    if(round == 5){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1p,width/2 - 320, height/2 +76);
    image(button4,width/2 + 220, height/2 -190);
    image(button2p,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    score += 60;
    }
    if(round == 6){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4p,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4p,width/2 + 220, height/2 +76);
    score += 60;
    }
    if(round == 7){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1p,width/2 - 320, height/2 +76);
    image(button4p,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4p,width/2 + 220, height/2 +76);
    score += 80;
    }
    if(round == 8){
    image(button1p,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1p,width/2 - 320, height/2 +76);
    image(button4p,width/2 + 220, height/2 -190);
    image(button2p,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    score += 100;
    }

    if ( kinect.isTrackingSkeleton(userId) == false) {
    image(button1,width/2 - 320, height/2 -190);
    image(button3,width/2 - 320, height/2 -57);
    image(button1,width/2 - 320, height/2 +76);
    image(button4,width/2 + 220, height/2 -190);
    image(button2,width/2 + 220, height/2 -57);
    image(button4,width/2 + 220, height/2 +76);
    }
    if ( kinect.isTrackingSkeleton(userId)) {
      drawSkeleton(userId);
      stage = 2;
    }
  }
}

// draw the skeleton with the selected joints
void drawSkeleton(int userId)
{  
  // draw limbs  
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_HEAD, SimpleOpenNI.SKEL_NECK);

  kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_LEFT_SHOULDER);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_LEFT_ELBOW);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_ELBOW, SimpleOpenNI.SKEL_LEFT_HAND);

  kinect.drawLimb(userId, SimpleOpenNI.SKEL_NECK, SimpleOpenNI.SKEL_RIGHT_SHOULDER);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_RIGHT_ELBOW);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_ELBOW, SimpleOpenNI.SKEL_RIGHT_HAND);

  kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_SHOULDER, SimpleOpenNI.SKEL_TORSO);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_SHOULDER, SimpleOpenNI.SKEL_TORSO);

  kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_LEFT_HIP);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_HIP, SimpleOpenNI.SKEL_LEFT_KNEE);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_LEFT_KNEE, SimpleOpenNI.SKEL_LEFT_FOOT);

  kinect.drawLimb(userId, SimpleOpenNI.SKEL_TORSO, SimpleOpenNI.SKEL_RIGHT_HIP);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_HIP, SimpleOpenNI.SKEL_RIGHT_KNEE);
  kinect.drawLimb(userId, SimpleOpenNI.SKEL_RIGHT_KNEE, SimpleOpenNI.SKEL_RIGHT_FOOT);
}


// Event-based Methods

void onNewUser(SimpleOpenNI curContext, int userId)
{
  println("onNewUser - userId: " + userId);
  println("\tstart tracking skeleton");

  curContext.startTrackingSkeleton(userId);
}

void onLostUser(SimpleOpenNI curContext, int userId)
{
  println("onLostUser - userId: " + userId);
}

//REMINDER: THESE NEEDS TO BE TWEAKED FOR FINAL PROJECT**///////////////////////////
  //rect(box1left,box1top,box1right,box1bottom);
  int box1top = 50;
  int box1bottom = 188;
  int box1left = 0;
  int box1right = 100;
  int box2top = 238;
  int box2bottom = 188;
  int box2left = 0;
  int box2right = 100; 
  int box3top = 426;  
  int box3bottom = 188; 
  int box3left = 0;  
  int box3right = 100; 
  int box4top;  
  int box4bottom;  
  int box4left;  
  int box4right; 
  int box5top; 
  int box5bottom; 
  int box5left;  
  int box5right; 
  int box6top; 
  int box6bottom;  
  int box6left;  
  int box6right; 
  ////////////////////////////////////////////////////////////////////////////////////
void drawRHand( int userID) {
  PVector leftHand = new PVector();
  float confidence = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_HAND, leftHand);
  PVector convertedLeftHand = new PVector();
  kinect.convertRealWorldToProjective(leftHand, convertedLeftHand);
  fill(255,0,0);
  float ellipseSizee = map(convertedLeftHand.z, 700, 2500, 50, 1);
 ellipse(convertedLeftHand.x, convertedLeftHand.y, ellipseSizee, ellipseSizee);
}

void drawLHand(int userID) {
  rectMode(CORNERS);
  PImage button1,button1p,button2,button2p,button3,button3p,button4,button4p;

  // make a vector to store the left hand
  PVector rightHand = new PVector();
  // put the position of the left hand into that vector
  float confidencee = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_HAND, rightHand);
  // convert the detected hand position to "projective" coordinates that will match the depth image
  PVector convertedRightHand = new PVector();
  kinect.convertRealWorldToProjective(rightHand, convertedRightHand);
  // and display it
  fill(255, 100, 0);

  float ellipseSize = map(convertedRightHand.z, 700, 2500, 50, 1);
    button1 = loadImage("button1.jpg");
    button1p = loadImage("button1p.jpg");
    button2 = loadImage("button2.jpg");
    button2p = loadImage("button2p.jpg");
    button3 = loadImage("button3.jpg");
    button3p = loadImage("button3p.jpg");
    button4 = loadImage("button4.jpg");
    button4p = loadImage("button4p.jpg");

  ellipse(convertedRightHand.x, convertedRightHand.y, ellipseSize, ellipseSize);
  rect(box1left,box1top,box1right,box1bottom);
  rect(box2left,box2top,box2right,box2bottom);
  rect(box3left,box3top,box3right,box3bottom);


  if((convertedRightHand.x > box1left) && (convertedRightHand.x < box1right) && (convertedRightHand.y > box1top) && (convertedRightHand.y < box1bottom)){
    image(button1,width/2 - 320, height/2 -190);
    println("HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHAHHAHAHAHAHAHAHAHHAHA");
  }
  else{
    image(button1p,width/2 - 320, height/2 -190);
  }
    if((convertedRightHand.x > box2left) && (convertedRightHand.x < box2right) && (convertedRightHand.y > box2top) && (convertedRightHand.y < box2bottom)){
    image(button3,width/2 - 320, height/2 -57);
    println("HEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHEHHEHEHEHEHEHEHEHEHEH");
  }
  else{
    image(button3p,width/2 - 320, height/2 -57);
  }
    if((convertedRightHand.x > box3left) && (convertedRightHand.x < box3right) && (convertedRightHand.y > box3top) && (convertedRightHand.y < box3bottom)){
    image(button1,width/2 - 320, height/2 +76);
    println("HIHIHIIHIHIHIHIHHIHIHIHIHIHIHIHIHIHIHIHIHIHIIIHIHIHIHIHIHIHIHIHHIHIHIHIHIHIHHHIHIHIHIHIHHIHIHIHHIHIHIHIHIHIH");
  }
  else{
    image(button1p,width/2 - 320, height/2 +76);
  }
}

void drawRFoot( int userID) {
  PVector leftFoot = new PVector();
  float confidence = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_RIGHT_FOOT, leftFoot);
  PVector convertedLeftFoot = new PVector();
  kinect.convertRealWorldToProjective(leftFoot, convertedLeftFoot);
  fill(255,0,0);
  float ellipseSizeee = map(convertedLeftFoot.z, 700, 2500, 50, 1);
 ellipse(convertedLeftFoot.x, convertedLeftFoot.y, ellipseSizeee, ellipseSizeee);
}

void drawLFoot( int userID) {
  PVector rightFoot = new PVector();
  float confidence = kinect.getJointPositionSkeleton(userID, SimpleOpenNI.SKEL_LEFT_FOOT, rightFoot);
  PVector convertedRightFoot = new PVector();
  kinect.convertRealWorldToProjective(rightFoot, convertedRightFoot);
  fill(255,0,0);
  float ellipseSizeee = map(convertedRightFoot.z, 700, 2500, 50, 1);
 ellipse(convertedRightFoot.x, convertedRightFoot.y, ellipseSizeee, ellipseSizeee);
}


// prints out the distance between any two joints 
float getJointDistance(int userId, int joint1Id, PVector v)
{
  float d = 0;   // to store final distance value

  // two PVectors to hold the position of two joints
  PVector joint1 = new PVector();

  // get 3D position of both joints
  kinect.getJointPositionSkeleton(userId, joint1Id, joint1);

  d = distance3D(joint1, v);    // calculate the distance between the two joints

  return d;
}


// calculate the distance between any two points in 3D space and return it as a float
float distance3D(PVector point1, PVector point2)
{
  float diff_x, diff_y, diff_z;    // to store differences along x, y and z axes
  float distance;                  // to store final distance value

    // calculate the difference between the two points
  diff_x = point1.x - point2.x;
  diff_y = point1.y - point2.y;
  diff_z = point1.z - point2.z;

  // calculate the Euclidean distance between the two points
  distance = sqrt(pow(diff_x, 2)+pow(diff_y, 2)+pow(diff_z, 2)); 

  return distance;  // return the distance as a float
}

return distance; // return the distance as a float }1 2

Tagged:
Sign In or Register to comment.